From c614774f2b29492753216a6922a84d9381ed74ed Mon Sep 17 00:00:00 2001 From: Tracy Li Date: Wed, 28 Aug 2019 15:38:10 -0500 Subject: [PATCH] Fix issue that check if complex object or not. --- types.go | 15 ++++++++++++--- types_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 types_test.go diff --git a/types.go b/types.go index 6b61fb9..52ec08d 100644 --- a/types.go +++ b/types.go @@ -71,12 +71,21 @@ func GetComplexObjectInfo(val interface{}) (interface{}, string, bool) { if val == "" { return nil, "", false } else { - complexObject := &legacyData.ComplexObject{} - err := json.Unmarshal([]byte(t), complexObject) + var complexMap map[string]interface{} + err := json.Unmarshal([]byte(t), &complexMap) if err != nil { return nil, "", false } - return complexObject.Value, complexObject.Metadata, true + + v, hasVal := complexMap["value"] + mdI, hasMd := complexMap["metadata"] + md := "" + if hasMd { + md, hasMd = mdI.(string) + } + if hasVal && hasMd { + return v, md, true + } } case map[string]interface{}: v, hasVal := t["value"] diff --git a/types_test.go b/types_test.go new file mode 100644 index 0000000..b5744cd --- /dev/null +++ b/types_test.go @@ -0,0 +1,30 @@ +package legacybridge + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestGetComplexObjectInfo(t *testing.T) { + + cpx := `{"value":"123", "metadata":"medatat"}` + v, metadata, ok := GetComplexObjectInfo(cpx) + assert.True(t, ok) + assert.Equal(t, "medatat", metadata) + assert.Equal(t, "123", v) + + cpx = `{"value":"123"}` + v, metadata, ok = GetComplexObjectInfo(cpx) + assert.False(t, ok) + + cpx = `{"metadata":"123"}` + v, metadata, ok = GetComplexObjectInfo(cpx) + assert.False(t, ok) + + cpx = `{"value":"", "metadata":""}` + v, metadata, ok = GetComplexObjectInfo(cpx) + assert.True(t, ok) + assert.Equal(t, "", metadata) + assert.Equal(t, "", v) + +}