本文整理汇总了Golang中encoding/json.Indent函数的典型用法代码### 示例。如果您正苦于以下问题:Golang Indent函数的具体用法?Golang Indent怎么用?Golang Indent使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。
在下文中一共展示了Indent函数的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。
示例1: printDebugOnfail
func (t *Test) printDebugOnfail() {
if t.failed && t.PrintDebugOnFail {
fmt.Println("\033[1;36mDEBUG REQUEST\033[0m")
// request
fmt.Printf(" \033[1;33mURL\033[0m: %s\n", t.Request.URL.String())
fmt.Printf(" \033[1;33mHeaders\033[0m: %+v\n", t.request.Header)
fmt.Printf(" \033[1;33mBody\033[0m: ")
if t.Request.BodyString != "" {
fmt.Println(t.Request.BodyString)
} else if t.Request.BodyJson != nil {
b, err := json.Marshal(t.Request.BodyJson)
if err != nil {
t.fail(err)
return
}
if t.PrintJsonIndented {
var out bytes.Buffer
if err = json.Indent(&out, b, "\t", " "); err != nil {
fmt.Println(err)
} else {
fmt.Println(out.String())
}
} else {
fmt.Printf("%s\n", b)
}
} else {
fmt.Println("")
}
//response
if t.Response != nil {
fmt.Println("\033[1;36mDEBUG RESPONSE\033[0m")
if t.response != nil {
fmt.Printf(" \033[1;33mHeaders\033[0m: %+v\n", t.response.Header)
fmt.Printf(" \033[1;33mStatus code\033[0m: %+v\n", t.response.StatusCode)
fmt.Printf(" \033[1;33mStatus\033[0m: %+v\n", t.response.Status)
if t.Response.body != nil {
defaultBodyPrint := false
fmt.Printf(" \033[1;33mBody\033[0m: ")
if strings.Contains(strings.ToLower(t.Response.contentType), "application/json") {
if t.PrintJsonIndented {
var out bytes.Buffer
if err := json.Indent(&out, t.Response.body, "\t", " "); err != nil {
fmt.Println(err)
} else {
fmt.Println(out.String())
}
} else {
defaultBodyPrint = true
}
}
if defaultBodyPrint {
fmt.Printf("%s\n", t.Response.body)
}
}
} else {
fmt.Println(" \033[0;31mno response\033[0m")
}
}
}
}
开发者ID:Djuke,项目名称:httpapitester,代码行数:60,代码来源:test.go
示例2: AssertJSONBody
func AssertJSONBody(tb testing.TB, exp, act interface{}) {
red := ansi.ColorCode("red+h:black")
green := ansi.ColorCode("green+h:black")
yellow := ansi.ColorFunc("yellow+h")
reset := ansi.ColorCode("reset")
var actBuf bytes.Buffer
err := json.Indent(&actBuf, []byte(act.(string)), "", " ")
if err != nil {
fmt.Println(red, "Invalid json: ", act, reset)
}
act = string(actBuf.Bytes())
var expBuf bytes.Buffer
err = json.Indent(&expBuf, []byte(exp.(string)), "", " ")
if err != nil {
fmt.Println(red, "Invalid json: ", exp, reset)
}
exp = string(expBuf.Bytes())
if !reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1)
fmt.Println(yellow(fmt.Sprintf("%s:%d", filepath.Base(file), line)))
fmt.Println(green, "Expected: ", exp, reset)
fmt.Println(red, " Got: ", act, reset)
tb.FailNow()
}
}
开发者ID:ustrajunior,项目名称:minion,代码行数:30,代码来源:tst.go
示例3: MatchJSON
func MatchJSON(actual, want interface{}) (success bool, message string, err error) {
actualString, aok := toString(actual)
expectedString, eok := toString(want)
if aok && eok {
abuf := new(bytes.Buffer)
ebuf := new(bytes.Buffer)
if err := json.Indent(abuf, []byte(actualString), "", " "); err != nil {
return false, "", err
}
if err := json.Indent(ebuf, []byte(expectedString), "", " "); err != nil {
return false, "", err
}
var aval interface{}
var eval interface{}
json.Unmarshal([]byte(actualString), &aval)
json.Unmarshal([]byte(expectedString), &eval)
if reflect.DeepEqual(aval, eval) {
return true, fmt.Sprintf("%s not to match JSON of %s", abuf.String(), ebuf.String()), nil
} else {
return false, fmt.Sprintf("%s to match JSON of %s", abuf.String(), ebuf.String()), nil
}
} else {
return false, "", fmt.Errorf("MatchJSONMatcher matcher requires a string or stringer. Got:\n%#v", actual)
}
return false, "", nil
}
开发者ID:sguzwf,项目名称:mertiwiki,代码行数:32,代码来源:harness.go
示例4: Match
func (matcher *MatchJSONMatcher) Match(actual interface{}) (success bool, message string, err error) {
actualString, aok := toString(actual)
expectedString, eok := toString(matcher.JSONToMatch)
if aok && eok {
abuf := new(bytes.Buffer)
ebuf := new(bytes.Buffer)
if err := json.Indent(abuf, []byte(actualString), "", " "); err != nil {
return false, "", err
}
if err := json.Indent(ebuf, []byte(expectedString), "", " "); err != nil {
return false, "", err
}
var aval interface{}
var eval interface{}
json.Unmarshal([]byte(actualString), &aval)
json.Unmarshal([]byte(expectedString), &eval)
if reflect.DeepEqual(aval, eval) {
return true, format.Message(abuf.String(), "not to match JSON of", ebuf.String()), nil
} else {
return false, format.Message(abuf.String(), "to match JSON of", ebuf.String()), nil
}
} else {
return false, "", fmt.Errorf("MatchJSONMatcher matcher requires a string or stringer. Got:\n%s", format.Object(actual, 1))
}
return false, "", nil
}
开发者ID:pivotal,项目名称:gumshoe,代码行数:32,代码来源:match_json_matcher.go
示例5: ContainsJSON
func (a *assertionGroup) ContainsJSON(s interface{}) *assertionGroup {
js, err := json.Marshal(s)
if err != nil {
log.Fatal(err)
}
var expected bytes.Buffer
err = json.Indent(&expected, js, "", "\t")
if err != nil {
log.Fatal(err)
}
var actual bytes.Buffer
err = json.Indent(&actual, a.response.BodyBytes, "", "\t")
if err != nil {
log.Fatal(err)
}
if string(actual.Bytes()[:]) != string(expected.Bytes()[:]) {
msg := fmt.Sprintf(`JSON body does not match:
%s
expected:
%s`, string(actual.Bytes()[:]), string(expected.Bytes()[:]))
a.Errorf(msg)
}
return a
}
开发者ID:vsco,项目名称:http-test,代码行数:31,代码来源:assert_response.go
最后编辑: kuteng 文档更新时间: 2021-08-23 19:14 作者:kuteng