Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoogle-ink committed Oct 23, 2024
1 parent 07f104b commit 1584c70
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
20 changes: 20 additions & 0 deletions body_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (bm BodyMap) GetString(key string) string {
return v
}

// Deprecated: Use GetAny instead.
// 获取原始参数
func (bm BodyMap) GetInterface(key string) any {
if bm == nil {
Expand All @@ -70,6 +71,25 @@ func (bm BodyMap) GetInterface(key string) any {
return bm[key]
}

// 获取原始参数
func (bm BodyMap) GetAny(key string) any {
if bm == nil {
return nil
}
return bm[key]
}

// 解析到结构体指针
func (bm BodyMap) Decode(key string, ptr any) error {
if bm == nil {
return errors.New("BodyMap is nil")
}
if err := json.Unmarshal([]byte(bm.GetString(key)), ptr); err != nil {
return err
}
return nil
}

// 删除参数
func (bm BodyMap) Remove(key string) {
delete(bm, key)
Expand Down
19 changes: 19 additions & 0 deletions body_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,22 @@ func TestBodyUnmarshal(t *testing.T) {
}
xlog.Debug("bm:", bm)
}

func TestSetSlice(t *testing.T) {
xlog.SetLevel(xlog.DebugLevel)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
var us []*User
for i := 0; i < 3; i++ {
us = append(us, &User{
Name: "Jerry",
Age: i,
})
}
bm := make(BodyMap)
bm.Set("slice", us)
jb := bm.JsonBody()
xlog.Debug("bm: ", jb)
}

0 comments on commit 1584c70

Please sign in to comment.