diff --git a/color/color_core.go b/color/color_core.go index 61f2c16..d415afc 100644 --- a/color/color_core.go +++ b/color/color_core.go @@ -57,6 +57,21 @@ type Formatter struct { r io.Reader } +func strToObject(all []byte) (interface{}, error) { + + var obj map[string]interface{} + if err := json.Unmarshal(all, &obj); err != nil { + var arr []interface{} + if err = json.Unmarshal(all, &arr); err != nil { + return nil, err + } + + return arr, nil + } + + return obj, nil +} + // NewFormatEncoder 着色json/yaml/xml构造函数 func NewFormatEncoder(r io.Reader, openColor bool, bodyType BodyType) *Formatter { // 如果颜色没打开,或者bodyType为txt @@ -69,11 +84,12 @@ func NewFormatEncoder(r io.Reader, openColor bool, bodyType BodyType) *Formatter return nil } - var obj map[string]interface{} - + var data interface{} switch bodyType { case JSONType: - err = json.Unmarshal(all, &obj) + if data, err = strToObject(all); err != nil { + return nil + } //todo xmlType and yamlType case XMLType: case YAMLType: @@ -95,7 +111,7 @@ func NewFormatEncoder(r io.Reader, openColor bool, bodyType BodyType) *Formatter r: r, } - all, _ = f.Marshal(obj) + all, _ = f.Marshal(data) f.r = bytes.NewReader(all) return f diff --git a/color/color_core_test.go b/color/color_core_test.go index e75ace5..fb96f38 100644 --- a/color/color_core_test.go +++ b/color/color_core_test.go @@ -2,6 +2,7 @@ package color import ( "bytes" + "fmt" "io" "io/ioutil" "strings" @@ -25,8 +26,8 @@ func Test_ColorCore_NewFormat_Nil(t *testing.T) { {strings.NewReader("xxx"), true, JSONType}, } - for _, d := range data { - assert.Nil(t, NewFormatEncoder(d.r, d.openColor, d.bodyType)) + for index, d := range data { + assert.Nil(t, NewFormatEncoder(d.r, d.openColor, d.bodyType), fmt.Sprintf("fail index:%d", index)) } } diff --git a/dataflow/debug_test.go b/dataflow/debug_test.go index ff4b8fa..369f559 100644 --- a/dataflow/debug_test.go +++ b/dataflow/debug_test.go @@ -141,7 +141,7 @@ func TestDebug_Debug(t *testing.T) { assert.NotEqual(t, bytes.Index(buf.Bytes(), []byte(rspVal)), -1) return err }(), - // json + // object json func() error { buf.Reset() key := "testkeyjson" @@ -153,6 +153,18 @@ func TestDebug_Debug(t *testing.T) { assert.NotEqual(t, bytes.Index(buf.Bytes(), []byte(rspVal)), -1) return err }(), + // array json + func() error { + buf.Reset() + key := "testkeyjson" + val := "testvaluejson" + err := New().POST(ts.URL).SetJSON(core.A{key, val}).Debug(dbug).Do() + + assert.NotEqual(t, bytes.Index(buf.Bytes(), []byte(key)), -1, core.BytesToString(buf.Bytes())) + assert.NotEqual(t, bytes.Index(buf.Bytes(), []byte(val)), -1) + assert.NotEqual(t, bytes.Index(buf.Bytes(), []byte(rspVal)), -1) + return err + }(), // body func() error { buf.Reset() diff --git a/go.mod b/go.mod index 212dbbe..795997d 100644 --- a/go.mod +++ b/go.mod @@ -17,13 +17,22 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/assert/v2 v2.0.1 // indirect github.com/golang/protobuf v1.5.0 // indirect + github.com/google/go-cmp v0.5.5 // indirect + github.com/google/gofuzz v1.0.0 // indirect github.com/json-iterator/go v1.1.9 // indirect github.com/leodido/go-urn v1.2.0 // indirect github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.1.0 // indirect + github.com/ugorji/go v1.1.7 // indirect github.com/ugorji/go/codec v1.1.7 // indirect golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect + golang.org/x/text v0.3.2 // indirect + golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e // indirect + golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect + gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 // indirect )