修复地址栏数组对象参数

pull/2805/head
熊闯 2 years ago
parent d92ad95503
commit a3bf572505
  1. 19
      encoding/form/proto_encode.go
  2. 73
      internal/testdata/binding/test.pb.go
  3. 2
      internal/testdata/binding/test.proto
  4. 12
      transport/http/binding/encode_test.go

@ -64,14 +64,11 @@ func encodeByField(u url.Values, path string, m protoreflect.Message, forceTextN
switch {
case fd.IsList():
if v.List().Len() > 0 {
list, err := encodeRepeatedField(fd, v.List())
err := encodeRepeatedField(fd, v.List(), u, newPath, forceTextName)
if err != nil {
finalErr = err
return false
}
for _, item := range list {
u.Add(newPath, item)
}
}
case fd.IsMap():
if v.Map().Len() > 0 {
@ -107,16 +104,18 @@ func encodeByField(u url.Values, path string, m protoreflect.Message, forceTextN
return
}
func encodeRepeatedField(fieldDescriptor protoreflect.FieldDescriptor, list protoreflect.List) ([]string, error) {
var values []string
func encodeRepeatedField(fieldDescriptor protoreflect.FieldDescriptor, list protoreflect.List, u url.Values, newPath string, forceTextName bool) error {
for i := 0; i < list.Len(); i++ {
value, err := EncodeField(fieldDescriptor, list.Get(i))
if err != nil {
return nil, err
if err == nil {
u.Add(newPath, value)
} else {
if err = encodeByField(u, fmt.Sprintf("%s[%d]", newPath, i), list.Get(i).Message(), forceTextName); err != nil {
return err
}
}
values = append(values, value)
}
return values, nil
return nil
}
func encodeMapField(fieldDescriptor protoreflect.FieldDescriptor, mp protoreflect.Map) (map[string]string, error) {

@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.0
// protoc v3.17.3
// protoc-gen-go v1.30.0
// protoc v3.21.12
// source: test.proto
package binding
@ -35,6 +35,8 @@ type HelloRequest struct {
OptString *string `protobuf:"bytes,6,opt,name=opt_string,json=optString,proto3,oneof" json:"opt_string,omitempty"`
SubField *Sub `protobuf:"bytes,7,opt,name=subField,proto3" json:"subField,omitempty"`
TestRepeated []string `protobuf:"bytes,8,rep,name=test_repeated,proto3" json:"test_repeated,omitempty"`
SubRepeated []*Sub `protobuf:"bytes,9,rep,name=sub_repeated,json=subRepeated,proto3" json:"sub_repeated,omitempty"`
SubMap map[string]*Sub `protobuf:"bytes,10,rep,name=sub_map,json=subMap,proto3" json:"sub_map,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
}
func (x *HelloRequest) Reset() {
@ -125,6 +127,20 @@ func (x *HelloRequest) GetTestRepeated() []string {
return nil
}
func (x *HelloRequest) GetSubRepeated() []*Sub {
if x != nil {
return x.SubRepeated
}
return nil
}
func (x *HelloRequest) GetSubMap() map[string]*Sub {
if x != nil {
return x.SubMap
}
return nil
}
type Sub struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@ -178,7 +194,7 @@ var file_test_proto_rawDesc = []byte{
0x0a, 0x0a, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x62, 0x69,
0x6e, 0x64, 0x69, 0x6e, 0x67, 0x1a, 0x20, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x5f, 0x6d, 0x61, 0x73,
0x6b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xe2, 0x02, 0x0a, 0x0c, 0x48, 0x65, 0x6c, 0x6c,
0x6b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x98, 0x04, 0x0a, 0x0c, 0x48, 0x65, 0x6c, 0x6c,
0x6f, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65,
0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1e, 0x0a, 0x03,
0x73, 0x75, 0x62, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x62, 0x69, 0x6e, 0x64,
@ -198,15 +214,26 @@ var file_test_proto_rawDesc = []byte{
0x62, 0x52, 0x08, 0x73, 0x75, 0x62, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x12, 0x24, 0x0a, 0x0d, 0x74,
0x65, 0x73, 0x74, 0x5f, 0x72, 0x65, 0x70, 0x65, 0x61, 0x74, 0x65, 0x64, 0x18, 0x08, 0x20, 0x03,
0x28, 0x09, 0x52, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x72, 0x65, 0x70, 0x65, 0x61, 0x74, 0x65,
0x64, 0x42, 0x0c, 0x0a, 0x0a, 0x5f, 0x6f, 0x70, 0x74, 0x5f, 0x69, 0x6e, 0x74, 0x33, 0x32, 0x42,
0x0c, 0x0a, 0x0a, 0x5f, 0x6f, 0x70, 0x74, 0x5f, 0x69, 0x6e, 0x74, 0x36, 0x34, 0x42, 0x0d, 0x0a,
0x0b, 0x5f, 0x6f, 0x70, 0x74, 0x5f, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x22, 0x1b, 0x0a, 0x03,
0x53, 0x75, 0x62, 0x12, 0x14, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x06, 0x6e, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x42, 0x2f, 0x5a, 0x2d, 0x67, 0x69, 0x74,
0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x6f, 0x2d, 0x6b, 0x72, 0x61, 0x74, 0x6f,
0x73, 0x2f, 0x6b, 0x72, 0x61, 0x74, 0x6f, 0x73, 0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f,
0x72, 0x74, 0x2f, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x33,
0x64, 0x12, 0x2f, 0x0a, 0x0c, 0x73, 0x75, 0x62, 0x5f, 0x72, 0x65, 0x70, 0x65, 0x61, 0x74, 0x65,
0x64, 0x18, 0x09, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e,
0x67, 0x2e, 0x53, 0x75, 0x62, 0x52, 0x0b, 0x73, 0x75, 0x62, 0x52, 0x65, 0x70, 0x65, 0x61, 0x74,
0x65, 0x64, 0x12, 0x3a, 0x0a, 0x07, 0x73, 0x75, 0x62, 0x5f, 0x6d, 0x61, 0x70, 0x18, 0x0a, 0x20,
0x03, 0x28, 0x0b, 0x32, 0x21, 0x2e, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x2e, 0x48, 0x65,
0x6c, 0x6c, 0x6f, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x2e, 0x53, 0x75, 0x62, 0x4d, 0x61,
0x70, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x06, 0x73, 0x75, 0x62, 0x4d, 0x61, 0x70, 0x1a, 0x47,
0x0a, 0x0b, 0x53, 0x75, 0x62, 0x4d, 0x61, 0x70, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a,
0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12,
0x22, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c,
0x2e, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x2e, 0x53, 0x75, 0x62, 0x52, 0x05, 0x76, 0x61,
0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x42, 0x0c, 0x0a, 0x0a, 0x5f, 0x6f, 0x70, 0x74, 0x5f,
0x69, 0x6e, 0x74, 0x33, 0x32, 0x42, 0x0c, 0x0a, 0x0a, 0x5f, 0x6f, 0x70, 0x74, 0x5f, 0x69, 0x6e,
0x74, 0x36, 0x34, 0x42, 0x0d, 0x0a, 0x0b, 0x5f, 0x6f, 0x70, 0x74, 0x5f, 0x73, 0x74, 0x72, 0x69,
0x6e, 0x67, 0x22, 0x1b, 0x0a, 0x03, 0x53, 0x75, 0x62, 0x12, 0x14, 0x0a, 0x04, 0x6e, 0x61, 0x6d,
0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6e, 0x61, 0x6d, 0x69, 0x6e, 0x67, 0x42,
0x2f, 0x5a, 0x2d, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x67, 0x6f,
0x2d, 0x6b, 0x72, 0x61, 0x74, 0x6f, 0x73, 0x2f, 0x6b, 0x72, 0x61, 0x74, 0x6f, 0x73, 0x2f, 0x74,
0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2f, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67,
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@ -221,21 +248,25 @@ func file_test_proto_rawDescGZIP() []byte {
return file_test_proto_rawDescData
}
var file_test_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_test_proto_msgTypes = make([]protoimpl.MessageInfo, 3)
var file_test_proto_goTypes = []interface{}{
(*HelloRequest)(nil), // 0: binding.HelloRequest
(*Sub)(nil), // 1: binding.Sub
(*fieldmaskpb.FieldMask)(nil), // 2: google.protobuf.FieldMask
nil, // 2: binding.HelloRequest.SubMapEntry
(*fieldmaskpb.FieldMask)(nil), // 3: google.protobuf.FieldMask
}
var file_test_proto_depIdxs = []int32{
1, // 0: binding.HelloRequest.sub:type_name -> binding.Sub
2, // 1: binding.HelloRequest.update_mask:type_name -> google.protobuf.FieldMask
3, // 1: binding.HelloRequest.update_mask:type_name -> google.protobuf.FieldMask
1, // 2: binding.HelloRequest.subField:type_name -> binding.Sub
3, // [3:3] is the sub-list for method output_type
3, // [3:3] is the sub-list for method input_type
3, // [3:3] is the sub-list for extension type_name
3, // [3:3] is the sub-list for extension extendee
0, // [0:3] is the sub-list for field type_name
1, // 3: binding.HelloRequest.sub_repeated:type_name -> binding.Sub
2, // 4: binding.HelloRequest.sub_map:type_name -> binding.HelloRequest.SubMapEntry
1, // 5: binding.HelloRequest.SubMapEntry.value:type_name -> binding.Sub
6, // [6:6] is the sub-list for method output_type
6, // [6:6] is the sub-list for method input_type
6, // [6:6] is the sub-list for extension type_name
6, // [6:6] is the sub-list for extension extendee
0, // [0:6] is the sub-list for field type_name
}
func init() { file_test_proto_init() }
@ -276,7 +307,7 @@ func file_test_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_test_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumMessages: 3,
NumExtensions: 0,
NumServices: 0,
},

@ -16,6 +16,8 @@ message HelloRequest {
optional string opt_string = 6;
Sub subField = 7;
repeated string test_repeated = 8 [json_name = "test_repeated"];
repeated Sub sub_repeated = 9;
map<string, Sub> sub_map = 10;
}
message Sub{

@ -15,6 +15,18 @@ func TestEncodeURL(t *testing.T) {
needQuery bool
want string
}{
{
pathTemplate: "http://helloworld.Greeter/helloworld/{name}/sub/{sub.naming}",
request: &binding.HelloRequest{Name: "test", Sub: &binding.Sub{Name: "2233!!!!"}, SubRepeated: []*binding.Sub{{Name: "2233!!!!"}}},
needQuery: false,
want: "http://helloworld.Greeter/helloworld/test/sub/2233!!!!",
},
{
pathTemplate: "http://helloworld.Greeter/helloworld/{name}/sub/{sub.naming}",
request: &binding.HelloRequest{Name: "test", Sub: &binding.Sub{Name: "2233!!!!"}, TestRepeated: []string{"xxx", "xx"}},
needQuery: false,
want: "http://helloworld.Greeter/helloworld/test/sub/2233!!!!",
},
{
pathTemplate: "http://helloworld.Greeter/helloworld/{name}/sub/{sub.naming}",
request: &binding.HelloRequest{Name: "test", Sub: &binding.Sub{Name: "2233!!!!"}},

Loading…
Cancel
Save