From 532bbefda844dd63ca8e883e7ee938c70f4c1b8d Mon Sep 17 00:00:00 2001 From: dd Date: Fri, 14 Oct 2016 11:45:40 +0300 Subject: [PATCH] Fix unmarshaling struct with interface field which is a pointer to a struct (part 2: test) --- tests/basic_test.go | 12 ++++++++++++ tests/data.go | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/tests/basic_test.go b/tests/basic_test.go index 3e994ea..1685a17 100644 --- a/tests/basic_test.go +++ b/tests/basic_test.go @@ -156,3 +156,15 @@ func TestUnderflowArray(t *testing.T) { t.Errorf("Unmarshal(%v) = %+v; want %+v", arrayUnderflowString, a, arrayUnderflowValue) } } + +func TestUnmarshalStructWithEmbeddedPtrStruct(t *testing.T) { + var s = StructWithInterface{Field2: &EmbeddedStruct{}} + var err error + err = easyjson.Unmarshal([]byte(structWithInterfaceString), &s) + if err != nil { + t.Errorf("easyjson.Unmarshal() error: %v", err) + } + if !reflect.DeepEqual(s, structWithInterfaceValueFilled) { + t.Errorf("easyjson.Unmarshal() = %#v; want %#v", s, structWithInterfaceValueFilled) + } +} diff --git a/tests/data.go b/tests/data.go index 1716617..baaa633 100644 --- a/tests/data.go +++ b/tests/data.go @@ -624,3 +624,17 @@ type RequiredOptionalStruct struct { FirstName string `json:"first_name,required"` Lastname string `json:"last_name"` } + +type StructWithInterface struct { + Field1 int `json:"f1"` + Field2 interface{} `json:"f2"` + Field3 string `json:"f3"` +} + +type EmbeddedStruct struct { + Field1 int `json:"f1"` + Field2 string `json:"f2"` +} + +var structWithInterfaceString = `{"f1":1,"f2":{"f1":11,"f2":"22"},"f3":"3"}` +var structWithInterfaceValueFilled = StructWithInterface{1, &EmbeddedStruct{11, "22"}, "3"}