Skip to content

Commit

Permalink
add: Map Slice 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
v_llxjliu committed Jun 5, 2024
1 parent 8e460b1 commit 561aa2f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
70 changes: 70 additions & 0 deletions example/with_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"encoding/json"
"fmt"
"github.com/liu-cn/json-filter/filter"
"testing"
)

func TestMapWithValue(t *testing.T) {
type F struct {
A string `json:"a,select(a)"`
}
//filterMap:=filter.Select("a",&F{A: "a"}).(filter.Filter).Map()
filterMap := filter.SelectMarshal("a", &F{A: "a"}).Map() //跟上面写法等价
filterMap["b"] = "b"
filterMap["cc"] = struct {
CC string
}{
CC: "CC",
}

marshal, err := json.Marshal(filterMap)
if err != nil {
panic(err)
}

fmt.Println(string(marshal))
//{
// "a": "a",
// "b": "b",
// "cc": {"CC": "CC"}
//}

}

func TestSliceWithValue(t *testing.T) {
type F struct {
A string `json:"a,select(a)"`
}

list := []F{
F{A: "a"},
}

//slices:=filter.Select("a",&F{A: "a"}).(filter.Filter).Slice()
slices := filter.SelectMarshal("a", list).Slice() //跟上面写法等价

slices = append(slices, F{A: "b"})
slices = append(slices, F{A: "c"})

marshal, err := json.Marshal(slices)
if err != nil {
panic(err)
}

fmt.Println(string(marshal))
//[
// {
// "a": "a"
// },
// {
// "a": "b"
// },
// {
// "a": "c"
// }
//]

}
23 changes: 19 additions & 4 deletions filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ func EnableCache(enable bool) {
enableCache = enable
}

// Deprecated
// SelectMarshal 不建议使用,第一个参数填你结构体select标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的select标签里标注的有该场景那么该字段会被选中。
// SelectMarshal 跟Select方法等价,只是这个会返回具体对象,第一个参数填你结构体select标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的select标签里标注的有该场景那么该字段会被选中。
func SelectMarshal(selectScene string, el interface{}) Filter {
return jsonFilter(selectScene, el, true)
}

// Deprecated
// OmitMarshal 不建议使用,第一个参数填你结构体omit标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的omit标签里标注的有该场景那么该字段会被过滤掉
// OmitMarshal 跟Omit方法等价,只是这个会返回具体对象,第一个参数填你结构体omit标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的omit标签里标注的有该场景那么该字段会被过滤掉
func OmitMarshal(omitScene string, el interface{}) Filter {
return jsonFilter(omitScene, el, false)
}
Expand Down Expand Up @@ -83,6 +81,23 @@ func (f Filter) String() string {
return json
}

// Map 过滤后的map结构
func (f Filter) Map() map[string]interface{} {
return f.node.Map()
}

// Slice 过滤后的切片结构
func (f Filter) Slice() []interface{} {
slices := make([]interface{}, 0, len(f.node.Children))
for i := 0; i < len(f.node.Children); i++ {
v, ok := f.node.Children[i].GetValue()
if ok {
slices = append(slices, v)
}
}
return slices
}

//// SelectCache 直接返回过滤后的数据结构,它可以被json.Marshal解析,直接打印会以过滤后的json字符串展示
//func SelectCache(selectScene string, el interface{}) interface{} {
// return jsonFilterCache(selectScene, el, true)
Expand Down

0 comments on commit 561aa2f

Please sign in to comment.