Преглед на файлове

#63 fix embed struct at last

Tao Wen преди 8 години
родител
ревизия
d7ea1acd3f
променени са 2 файла, в които са добавени 21 реда и са изтрити 0 реда
  1. 2 0
      feature_reflect_extension.go
  2. 19 0
      jsoniter_object_test.go

+ 2 - 0
feature_reflect_extension.go

@@ -203,6 +203,8 @@ func describeStruct(cfg *frozenConfig, typ reflect.Type) (*StructDescriptor, err
 					return nil, err
 				}
 				for _, binding := range structDescriptor.Fields {
+					binding.Encoder = &structFieldEncoder{&field, binding.Encoder, false}
+					binding.Decoder = &structFieldDecoder{&field, binding.Decoder}
 					anonymousBindings = append(anonymousBindings, binding)
 				}
 			} else if field.Type.Kind() == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct {

+ 19 - 0
jsoniter_object_test.go

@@ -407,6 +407,25 @@ func Test_shadow_struct_field(t *testing.T) {
 	should.Contains(output, `"max_age":20`)
 }
 
+func Test_embed_at_last(t *testing.T) {
+	type Base struct {
+		Type string `json:"type"`
+	}
+
+	type Struct struct {
+		Field string `json:"field"`
+		FieldType string `json:"field_type"`
+		Base
+	}
+	should := require.New(t)
+	s := Struct{Field: "field", FieldType: "field_type", Base: Base{"type"}}
+	output, err := MarshalToString(s)
+	should.Nil(err)
+	should.Contains(output, `"type":"type"`)
+	should.Contains(output, `"field":"field"`)
+	should.Contains(output, `"field_type":"field_type"`)
+}
+
 func Test_decode_nested(t *testing.T) {
 	type StructOfString struct {
 		Field1 string