Skip to content

Commit

Permalink
go support AssertAsMap
Browse files Browse the repository at this point in the history
  • Loading branch information
wenzuochao committed Feb 11, 2020
1 parent f367afc commit 40c994f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions golang/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ func ToBytes(a string) []byte {
return []byte(a)
}

func AssertAsMap(a interface{}) map[string]interface{} {
byt, _ := json.Marshal(a)
res := make(map[string]interface{})
err := json.Unmarshal(byt, &res)
if err == nil {
return res
}
return res
}

func ParseJSON(a string) interface{} {
tmp := make(map[string]interface{})
err := json.Unmarshal([]byte(a), &tmp)
Expand Down
8 changes: 8 additions & 0 deletions golang/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,11 @@ func Test_StringifyMapValue(t *testing.T) {
utils.AssertEqual(t, "10", out["num"])
utils.AssertEqual(t, `{"test":"ok"}`, out["json"])
}

func Test_AssertAsMap(t *testing.T) {
in := map[string]string{
"num": "10",
}
out := AssertAsMap(in)
utils.AssertEqual(t, "10", out["num"].(string))
}

0 comments on commit 40c994f

Please sign in to comment.