Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoogle-ink committed Aug 2, 2024
1 parent 4ef1f54 commit 922c5c1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
17 changes: 17 additions & 0 deletions body_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ func (bm BodyMap) JsonBody() (jb string) {
return jb
}

// unmarshal JSON bytes in BodyMap
func (bm BodyMap) Unmarshal(jsonBs []byte) error {
if err := json.Unmarshal(jsonBs, &bm); err != nil {
return err
}
return nil
}

// unmarshal JSON string in BodyMap

func (bm BodyMap) UnmarshalString(jsonStr string) error {
if err := json.Unmarshal([]byte(jsonStr), &bm); err != nil {
return err
}
return nil
}

func (bm BodyMap) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
if len(bm) == 0 {
return nil
Expand Down
18 changes: 15 additions & 3 deletions body_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestBodyMapSetBodyMap(t *testing.T) {
xlog.Level = xlog.DebugLevel
xlog.SetLevel(xlog.DebugLevel)
bm := make(BodyMap)
// 1、配合map使用
sceneInfo := make(map[string]map[string]string)
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestBodyMapSetBodyMap(t *testing.T) {
}

func TestBodyMapMarshal(t *testing.T) {
xlog.Level = xlog.DebugLevel
xlog.SetLevel(xlog.DebugLevel)
bm := make(BodyMap)
bm.Set("4key", "4value").
Set("6key", "6value").
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestBodyMapMarshal(t *testing.T) {
}

func TestBodyMapMarshalSlice(t *testing.T) {
xlog.Level = xlog.DebugLevel
xlog.SetLevel(xlog.DebugLevel)
type Receiver struct {
Type string `json:"type"`
Account string `json:"account"`
Expand Down Expand Up @@ -133,3 +133,15 @@ func TestBodyMapMarshalSlice(t *testing.T) {
bss, _ := xml.Marshal(bm)
xlog.Debug("body:", string(bss))
}

func TestBodyUnmarshal(t *testing.T) {
xlog.SetLevel(xlog.DebugLevel)
jsonStr := `{"name":"jerry","age":18}`
bm := make(BodyMap)
err := bm.UnmarshalString(jsonStr)
if err != nil {
xlog.Error(err)
return
}
xlog.Debug("bm:", bm)
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/go-pay/bm

go 1.20
go 1.21

require github.com/go-pay/xlog v0.0.2
require github.com/go-pay/xlog v0.0.3
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/go-pay/xlog v0.0.2 h1:kUg5X8/5VZAPDg1J5eGjA3MG0/H5kK6Ew0dW/Bycsws=
github.com/go-pay/xlog v0.0.2/go.mod h1:DbjMADPK4+Sjxj28ekK9goqn4zmyY4hql/zRiab+S9E=
github.com/go-pay/xlog v0.0.3 h1:avyMhCL/JgBHreoGx/am/kHxfs1udDOAeVqbmzP/Yes=
github.com/go-pay/xlog v0.0.3/go.mod h1:mH47xbobrdsSHWsmFtSF5agWbMHFP+tK0ZbVCk5OAEw=

0 comments on commit 922c5c1

Please sign in to comment.