From ef3038593b202edbad40e0a5400eb33915e63f52 Mon Sep 17 00:00:00 2001 From: Tao Wen Date: Sat, 17 Feb 2018 22:33:09 +0800 Subject: [PATCH] check nil for interface{} --- feature_reflect_native.go | 4 ++-- type_tests/struct_test.go | 4 ++-- type_tests/type_test.go | 11 +++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/feature_reflect_native.go b/feature_reflect_native.go index 57c388c..9a6150a 100644 --- a/feature_reflect_native.go +++ b/feature_reflect_native.go @@ -599,7 +599,7 @@ type marshalerEncoder struct { func (encoder *marshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) { obj := encoder.valType.UnsafeIndirect(ptr) - if obj == nil { + if reflect2.IsNil(obj) { stream.WriteNil() return } @@ -623,7 +623,7 @@ type textMarshalerEncoder struct { func (encoder *textMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) { obj := encoder.valType.UnsafeIndirect(ptr) - if obj == nil { + if reflect2.IsNil(obj) { stream.WriteNil() return } diff --git a/type_tests/struct_test.go b/type_tests/struct_test.go index b25fc7b..25162fe 100644 --- a/type_tests/struct_test.go +++ b/type_tests/struct_test.go @@ -251,8 +251,8 @@ func init() { })(nil), (*struct { TF1 struct { - F1 withTime - F2 *withTime + F2 int + F1 *withTime } })(nil), (*DeeplyNested)(nil), diff --git a/type_tests/type_test.go b/type_tests/type_test.go index f615e19..c4fb435 100644 --- a/type_tests/type_test.go +++ b/type_tests/type_test.go @@ -15,7 +15,18 @@ import ( var testCases []interface{} var asymmetricTestCases [][2]interface{} +type selectedSymmetricCase struct { + testCase interface{} +} + func Test_symmetric(t *testing.T) { + for _, testCase := range testCases { + selectedSymmetricCase, found := testCase.(selectedSymmetricCase) + if found { + testCases = []interface{}{selectedSymmetricCase.testCase} + break + } + } for _, testCase := range testCases { valType := reflect.TypeOf(testCase).Elem() t.Run(valType.String(), func(t *testing.T) {