From 4a257c1a34309faab2b18b96af486724fcee167f Mon Sep 17 00:00:00 2001 From: Tony Chen Date: Sat, 12 Jun 2021 11:52:48 +0800 Subject: [PATCH] Revert "add json test (#1031)" (#1033) This reverts commit 6ca3c788ddb486fa9f0c2607501c8c78dcbf52a7. --- encoding/json/json.go | 5 ++- encoding/json/json_test.go | 80 -------------------------------------- 2 files changed, 3 insertions(+), 82 deletions(-) delete mode 100644 encoding/json/json_test.go diff --git a/encoding/json/json.go b/encoding/json/json.go index 91f4131cc..826b688e6 100644 --- a/encoding/json/json.go +++ b/encoding/json/json.go @@ -39,14 +39,15 @@ func (codec) Marshal(v interface{}) ([]byte, error) { func (codec) Unmarshal(data []byte, v interface{}) error { rv := reflect.ValueOf(v) - if rv.Kind() == reflect.Ptr { + for rv.Kind() == reflect.Ptr { if rv.IsNil() { rv.Set(reflect.New(rv.Type().Elem())) } + rv = rv.Elem() } if m, ok := v.(proto.Message); ok { return UnmarshalOptions.Unmarshal(data, m) - } else if m, ok := reflect.Indirect(rv).Interface().(proto.Message); ok { + } else if m, ok := reflect.Indirect(reflect.ValueOf(v)).Interface().(proto.Message); ok { return UnmarshalOptions.Unmarshal(data, m) } return json.Unmarshal(data, v) diff --git a/encoding/json/json_test.go b/encoding/json/json_test.go deleted file mode 100644 index 06d80ee2f..000000000 --- a/encoding/json/json_test.go +++ /dev/null @@ -1,80 +0,0 @@ -package json - -import ( - "bytes" - "strings" - "testing" -) - -type testEmbed struct { - Level1a int `json:"a"` - Level1b int `json:"b"` - Level1c int `json:"c"` -} - -type testMessage struct { - Field1 string `json:"a"` - Field2 string `json:"b"` - Field3 string `json:"c"` - Embed *testEmbed `json:"embed,omitempty"` -} - -func TestJSON_Marshal(t *testing.T) { - tests := []struct { - input interface{} - expect string - }{ - { - input: &testMessage{}, - expect: `{"a":"","b":"","c":""}`, - }, - { - input: &testMessage{Field1: "a", Field2: "b", Field3: "c"}, - expect: `{"a":"a","b":"b","c":"c"}`, - }, - } - for _, v := range tests { - data, err := (codec{}).Marshal(v.input) - if err != nil { - t.Errorf("marshal(%#v): %s", v.input, err) - } - if got, want := string(data), v.expect; got != want { - if strings.Contains(want, "\n") { - t.Errorf("marshal(%#v):\nHAVE:\n%s\nWANT:\n%s", v.input, got, want) - } else { - t.Errorf("marshal(%#v):\nhave %#q\nwant %#q", v.input, got, want) - } - } - } -} - -func TestJSON_Unmarshal(t *testing.T) { - p := &testMessage{} - tests := []struct { - input string - expect interface{} - }{ - { - input: `{"a":"","b":"","c":""}`, - expect: &testMessage{}, - }, - { - input: `{"a":"a","b":"b","c":"c"}`, - expect: &p, - }, - } - for _, v := range tests { - want := []byte(v.input) - err := (codec{}).Unmarshal(want, &v.expect) - if err != nil { - t.Errorf("marshal(%#v): %s", v.input, err) - } - got, err := codec{}.Marshal(v.expect) - if err != nil { - t.Errorf("marshal(%#v): %s", v.input, err) - } - if !bytes.Equal(got, want) { - t.Errorf("marshal(%#v):\nhave %#q\nwant %#q", v.input, got, want) - } - } -}