fix(encoding/form): encode optional value (#2155)

* fix form encode optional value

* fix test

Co-authored-by: chenzhihui <chenzhihui@bilibili.com>
pull/2119/head
Tony Chen 2 years ago committed by GitHub
parent 5df19c47d8
commit 0bcdb8d59c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      encoding/form/form_test.go
  2. 56
      encoding/form/proto_encode.go
  3. 84
      internal/testdata/binding/test.pb.go
  4. 6
      internal/testdata/binding/test.proto
  5. 2
      transport/http/binding/encode_test.go

@ -204,3 +204,17 @@ func TestEncodeFieldMask(t *testing.T) {
t.Log(v)
}
}
func TestOptional(t *testing.T) {
v := int32(100)
req := &bdtest.HelloRequest{
Name: "foo",
Sub: &bdtest.Sub{Name: "bar"},
OptInt32: &v,
}
if v, _ := EncodeValues(req); v.Encode() != "name=foo&optInt32=100&sub.naming=bar" {
t.Errorf("got %s", v.Encode())
} else {
t.Log(v)
}
}

@ -26,11 +26,12 @@ func EncodeValues(msg proto.Message) (url.Values, error) {
return u, nil
}
func encodeByField(u url.Values, path string, v protoreflect.Message) error {
for i := 0; i < v.Descriptor().Fields().Len(); i++ {
fd := v.Descriptor().Fields().Get(i)
var key string
var newPath string
func encodeByField(u url.Values, path string, m protoreflect.Message) (finalErr error) {
m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
var (
key string
newPath string
)
if fd.HasJSONName() {
key = fd.JSONName()
} else {
@ -41,53 +42,55 @@ func encodeByField(u url.Values, path string, v protoreflect.Message) error {
} else {
newPath = path + "." + key
}
if of := fd.ContainingOneof(); of != nil {
if f := v.WhichOneof(of); f != nil {
if f := m.WhichOneof(of); f != nil {
if f != fd {
continue
return true
}
}
}
switch {
case fd.IsList():
if v.Get(fd).List().Len() > 0 {
list, err := encodeRepeatedField(fd, v.Get(fd).List())
if v.List().Len() > 0 {
list, err := encodeRepeatedField(fd, v.List())
if err != nil {
return err
finalErr = err
return false
}
u[newPath] = list
}
case fd.IsMap():
if v.Get(fd).Map().Len() > 0 {
m, err := encodeMapField(fd, v.Get(fd).Map())
if v.Map().Len() > 0 {
m, err := encodeMapField(fd, v.Map())
if err != nil {
return err
finalErr = err
return false
}
for k, value := range m {
u[fmt.Sprintf("%s[%s]", newPath, k)] = []string{value}
}
}
case (fd.Kind() == protoreflect.MessageKind) || (fd.Kind() == protoreflect.GroupKind):
value, err := encodeMessage(fd.Message(), v.Get(fd))
value, err := encodeMessage(fd.Message(), v)
if err == nil {
u[newPath] = []string{value}
continue
return true
}
err = encodeByField(u, newPath, v.Get(fd).Message())
if err != nil {
return err
if err = encodeByField(u, newPath, v.Message()); err != nil {
finalErr = err
return false
}
default:
value, err := EncodeField(fd, v.Get(fd))
value, err := EncodeField(fd, v)
if err != nil {
return err
finalErr = err
return false
}
u[newPath] = []string{value}
}
}
return nil
return true
})
return
}
func encodeRepeatedField(fieldDescriptor protoreflect.FieldDescriptor, list protoreflect.List) ([]string, error) {
@ -99,7 +102,6 @@ func encodeRepeatedField(fieldDescriptor protoreflect.FieldDescriptor, list prot
}
values = append(values, value)
}
return values, nil
}
@ -139,7 +141,7 @@ func EncodeField(fieldDescriptor protoreflect.FieldDescriptor, value protoreflec
case protoreflect.MessageKind, protoreflect.GroupKind:
return encodeMessage(fieldDescriptor.Message(), value)
default:
return fmt.Sprintf("%v", value.Interface()), nil
return fmt.Sprint(value.Interface()), nil
}
}
@ -158,7 +160,7 @@ func encodeMessage(msgDescriptor protoreflect.MessageDescriptor, value protorefl
"google.protobuf.UInt64Value", "google.protobuf.UInt32Value", "google.protobuf.BoolValue", "google.protobuf.StringValue":
fd := msgDescriptor.Fields()
v := value.Message().Get(fd.ByName(protoreflect.Name("value")))
return fmt.Sprintf("%v", v.Interface()), nil
return fmt.Sprint(v.Interface()), nil
case fieldMaskFullName:
m, ok := value.Message().Interface().(*fieldmaskpb.FieldMask)
if !ok || m == nil {

@ -30,6 +30,10 @@ type HelloRequest struct {
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
Sub *Sub `protobuf:"bytes,2,opt,name=sub,proto3" json:"sub,omitempty"`
UpdateMask *fieldmaskpb.FieldMask `protobuf:"bytes,3,opt,name=update_mask,json=updateMask,proto3" json:"update_mask,omitempty"`
OptInt32 *int32 `protobuf:"varint,4,opt,name=opt_int32,json=optInt32,proto3,oneof" json:"opt_int32,omitempty"`
OptInt64 *int64 `protobuf:"varint,5,opt,name=opt_int64,json=optInt64,proto3,oneof" json:"opt_int64,omitempty"`
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"`
}
func (x *HelloRequest) Reset() {
@ -85,6 +89,34 @@ func (x *HelloRequest) GetUpdateMask() *fieldmaskpb.FieldMask {
return nil
}
func (x *HelloRequest) GetOptInt32() int32 {
if x != nil && x.OptInt32 != nil {
return *x.OptInt32
}
return 0
}
func (x *HelloRequest) GetOptInt64() int64 {
if x != nil && x.OptInt64 != nil {
return *x.OptInt64
}
return 0
}
func (x *HelloRequest) GetOptString() string {
if x != nil && x.OptString != nil {
return *x.OptString
}
return ""
}
func (x *HelloRequest) GetSubField() *Sub {
if x != nil {
return x.SubField
}
return nil
}
type Sub struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@ -138,20 +170,32 @@ 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, 0x7f, 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, 0x69,
0x6e, 0x67, 0x2e, 0x53, 0x75, 0x62, 0x52, 0x03, 0x73, 0x75, 0x62, 0x12, 0x3b, 0x0a, 0x0b, 0x75,
0x70, 0x64, 0x61, 0x74, 0x65, 0x5f, 0x6d, 0x61, 0x73, 0x6b, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b,
0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62,
0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4d, 0x61, 0x73, 0x6b, 0x52, 0x0a, 0x75, 0x70,
0x64, 0x61, 0x74, 0x65, 0x4d, 0x61, 0x73, 0x6b, 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,
0x6b, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xbc, 0x02, 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,
0x69, 0x6e, 0x67, 0x2e, 0x53, 0x75, 0x62, 0x52, 0x03, 0x73, 0x75, 0x62, 0x12, 0x3b, 0x0a, 0x0b,
0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x5f, 0x6d, 0x61, 0x73, 0x6b, 0x18, 0x03, 0x20, 0x01, 0x28,
0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4d, 0x61, 0x73, 0x6b, 0x52, 0x0a, 0x75,
0x70, 0x64, 0x61, 0x74, 0x65, 0x4d, 0x61, 0x73, 0x6b, 0x12, 0x20, 0x0a, 0x09, 0x6f, 0x70, 0x74,
0x5f, 0x69, 0x6e, 0x74, 0x33, 0x32, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x48, 0x00, 0x52, 0x08,
0x6f, 0x70, 0x74, 0x49, 0x6e, 0x74, 0x33, 0x32, 0x88, 0x01, 0x01, 0x12, 0x20, 0x0a, 0x09, 0x6f,
0x70, 0x74, 0x5f, 0x69, 0x6e, 0x74, 0x36, 0x34, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x48, 0x01,
0x52, 0x08, 0x6f, 0x70, 0x74, 0x49, 0x6e, 0x74, 0x36, 0x34, 0x88, 0x01, 0x01, 0x12, 0x22, 0x0a,
0x0a, 0x6f, 0x70, 0x74, 0x5f, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x18, 0x06, 0x20, 0x01, 0x28,
0x09, 0x48, 0x02, 0x52, 0x09, 0x6f, 0x70, 0x74, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x88, 0x01,
0x01, 0x12, 0x28, 0x0a, 0x08, 0x73, 0x75, 0x62, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x18, 0x07, 0x20,
0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x62, 0x69, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x2e, 0x53, 0x75,
0x62, 0x52, 0x08, 0x73, 0x75, 0x62, 0x46, 0x69, 0x65, 0x6c, 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,
}
var (
@ -175,11 +219,12 @@ var file_test_proto_goTypes = []interface{}{
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
2, // [2:2] is the sub-list for method output_type
2, // [2:2] is the sub-list for method input_type
2, // [2:2] is the sub-list for extension type_name
2, // [2:2] is the sub-list for extension extendee
0, // [0:2] is the sub-list for field type_name
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
}
func init() { file_test_proto_init() }
@ -213,6 +258,7 @@ func file_test_proto_init() {
}
}
}
file_test_proto_msgTypes[0].OneofWrappers = []interface{}{}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{

@ -9,8 +9,12 @@ option go_package = "github.com/go-kratos/kratos/transport/binding";
// The request message containing the user's name.
message HelloRequest {
string name = 1;
Sub sub = 2;
Sub sub = 2;
google.protobuf.FieldMask update_mask = 3;
optional int32 opt_int32 = 4;
optional int64 opt_int64 = 5;
optional string opt_string = 6;
Sub subField = 7;
}
message Sub{

@ -44,7 +44,7 @@ func TestProtoPath(t *testing.T) {
Sub: &binding.Sub{Name: "kratos"},
}, true)
fmt.Println(url)
if url != `http://helloworld.Greeter/helloworld/go/sub?sub.naming=kratos&updateMask=` {
if url != `http://helloworld.Greeter/helloworld/go/sub?sub.naming=kratos` {
t.Fatalf("proto path not expected!actual: %s ", url)
}

Loading…
Cancel
Save