Skip to content

Commit

Permalink
修改329 (#330)
Browse files Browse the repository at this point in the history
* 修改329

* 修复单元测试用例
  • Loading branch information
guonaihong authored Apr 4, 2022
1 parent 24b2222 commit aa01ed1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
24 changes: 20 additions & 4 deletions color/color_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions color/color_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package color

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"strings"
Expand All @@ -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))
}
}

Expand Down
14 changes: 13 additions & 1 deletion dataflow/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit aa01ed1

Please sign in to comment.