Signed-off-by: Jiyong Huang <huangjy@emqx.io>
@@ -37,7 +37,7 @@ func TestEncode(t *testing.T) {
"id": 1,
"age": 1,
},
- e: "field email not found",
+ r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x00},
}, {
m: map[string]interface{}{
"name": "test",
@@ -73,6 +73,7 @@ func TestDecode(t *testing.T) {
"id": int64(1),
"email": "Dddd",
+ "code": []interface{}{},
r: []byte{0x0a, 0x04, 0x74, 0x65, 0x73, 0x74, 0x10, 0x01, 0x1a, 0x04, 0x44, 0x64, 0x64, 0x64},
@@ -66,7 +66,11 @@ func (fc *FieldConverter) encodeMap(im *desc.MessageDescriptor, i interface{}) (
for _, field := range fields {
v, ok := m[field.GetName()]
if !ok {
- return nil, fmt.Errorf("field %s not found", field.GetName())
+ if field.IsRequired() {
+ return nil, fmt.Errorf("field %s not found", field.GetName())
+ } else {
+ v = field.GetDefaultValue()
+ }
}
fv, err := fc.EncodeField(field, v)
if err != nil {
@@ -78,7 +78,7 @@ func TestRegistry(t *testing.T) {
expectedSchema := &Info{
Type: "protobuf",
Name: "test1",
- Content: "syntax = \"proto3\";message Person {string name = 1;int32 id = 2;string email = 3;}",
+ Content: "syntax = \"proto2\";message Person {required string name = 1;optional int32 id = 2;optional string email = 3;repeated ListOfDoubles code = 4;}message ListOfDoubles {repeated double doubles=1;}",
FilePath: filepath.Join(etcDir, "test1.proto"),
gottenSchema, err := GetSchema("protobuf", "test1")
@@ -1 +1 @@
-syntax = "proto3";message Person {string name = 1;int32 id = 2;string email = 3;}
+syntax = "proto2";message Person {required string name = 1;optional int32 id = 2;optional string email = 3;repeated ListOfDoubles code = 4;}message ListOfDoubles {repeated double doubles=1;}
@@ -828,6 +828,10 @@ func ToTypedSlice(input interface{}, conv func(interface{}, Strictness) (interfa
if s.Kind() != reflect.Slice {
return nil, fmt.Errorf("cannot convert %[1]T(%[1]v) to %s slice)", input, eleType)
+ if s.Len() == 0 {
+ result := reflect.MakeSlice(reflect.TypeOf([]interface{}{}), s.Len(), s.Len())
+ return result.Interface(), nil
ele, err := conv(s.Index(0).Interface(), sn)
et := reflect.TypeOf(ele)
if err != nil || et == nil {