filter

package
v0.0.0-...-8e460b1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2023 License: MIT Imports: 7 Imported by: 4

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnableCache

func EnableCache(enable bool)

EnableCache 决定是否启用缓存,默认开启(强烈建议,除非万一缓存模式下出现bug,可以关闭缓存退回曾经的无缓存过滤模式),开启缓存后会有30%-40%的性能提升,开启缓存并没有副作用,只是会让结构体的字段tag常驻内存减少tag字符串处理操作

func EqualJSON

func EqualJSON(jsonStr1, jsonStr2 string, moreJson ...string) (bool, error)

EqualJSON 判断两个或者多个json字符串是否等价(有相同的键值,不同的顺序)

func Omit

func Omit(omitScene string, el interface{}) interface{}

Omit 直接返回过滤后的数据结构,它可以被json.Marshal解析,直接打印会以过滤后的json字符串展示

Example
articles := ExampleOmitArticles{Like: 100, Collect: 20, Tags: []ExampleOmitTag{{"icon", "foo"}, {"icon", "bar"}}}
article := Omit("article", &articles) //尽量传指针,不传指针func选择器中的用指针接收的方法无法被调用
profile := Omit("profile", &articles)
articleJSON, _ := json.Marshal(article)
fmt.Println(string(articleJSON))
fmt.Println(profile) //可以直接打印,打印会直接输出过滤后的json
Output:

{"hot":120,"tags":[{"name":"foo"},{"name":"bar"}]}
{"hot":120,"tags":[{"icon":"icon"},{"icon":"icon"}]}

func Select

func Select(selectScene string, el interface{}) interface{}

Select 直接返回过滤后的数据结构,它可以被json.Marshal解析,直接打印会以过滤后的json字符串展示

Example
type (
	Tag struct {
		Icon string `json:"icon,select(article)"`
		Name string `json:"name,select(profile)"`
	}
	User struct {
		Age  int     `json:"age,select(article|profile)"`
		ID   int     `json:"id,select($any)"`                        //$any表示任何场景都选中该字段
		Name *string `json:"name,omitempty,select(article|profile)"` //为nil忽略
		Tags []Tag   `json:"tags,select(article|profile)"`
	}
)
name := "小北"
user := User{ID: 1, Name: &name, Age: 21, Tags: []Tag{{"icon", "foo"}, {"icon", "bar"}}}
article := Select("article", &user) //尽量传指针
null := Select("null", &user)
//user.Name = nil
profile := Select("profile", &user)
articleJSON, _ := json.Marshal(article)
fmt.Println(string(articleJSON))
fmt.Println(profile) //可以直接打印,打印会直接输出过滤后的json
fmt.Println(null)
Output:

{"age":21,"id":1,"name":"小北","tags":[{"icon":"icon"},{"icon":"icon"}]}
{"age":21,"id":1,"name":"小北","tags":[{"name":"foo"},{"name":"bar"}]}
{"id":1}

func SetJSONMarshal

func SetJSONMarshal(fn jsonMarshalFunc)

SetJSONMarshal 默认使用官方的json.Marshal 解析过滤后的数据结构,可以自定义json解析方法 比如换成字节的github.com/bytedance/sonic 这个进行json解析, filter.SetJSONMarshal(sonic.Marshal)就可以了,但是sonic(速度是官方的2-3倍)这个库好像需要go 1.15以上,可以根据自己的需要选择自己需要的json解析库 如果对性能要求没有那么高没必要换,这个方法在任意位置调用一次就可以了

Types

type Filter

type Filter struct {
	// contains filtered or unexported fields
}

func OmitMarshal

func OmitMarshal(omitScene string, el interface{}) Filter

Deprecated OmitMarshal 不建议使用,第一个参数填你结构体omit标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的omit标签里标注的有该场景那么该字段会被过滤掉

func SelectMarshal

func SelectMarshal(selectScene string, el interface{}) Filter

Deprecated SelectMarshal 不建议使用,第一个参数填你结构体select标签里的场景,第二个参数是你需要过滤的结构体对象,如果字段的select标签里标注的有该场景那么该字段会被选中。

func (Filter) Interface

func (f Filter) Interface() interface{}

Interface 解析为过滤后待json序列化的map[string]interface{}

Example
fmt.Println(Omit("", InterfaceTest{Data: map[string]interface{}{
	"1": 1,
}}))
fmt.Println(Omit("", InterfaceTest{Data: map[string]interface{}{
	"1": 1,
	"2": map[string]interface{}{
		"3": 3,
	},
}}))
Output:

{"data":{"1":1}}
{"data":{"1":1,"2":{"3":3}}}

func (Filter) JSON

func (f Filter) JSON() (string, error)

JSON 获取解析过滤后的json字符串,如果中途有错误会返回错误

func (Filter) MarshalJSON

func (f Filter) MarshalJSON() ([]byte, error)

func (Filter) MastMarshalJSON

func (f Filter) MastMarshalJSON() []byte

Deprecated

func (Filter) MustJSON

func (f Filter) MustJSON() string

MustJSON 获取解析过滤后的json字符串,如果中途有错误会panic

func (Filter) MustMarshalJSON

func (f Filter) MustMarshalJSON() []byte

func (Filter) String

func (f Filter) String() string

String fmt.Println() 打印时输出json字符串

type Time

type Time time.Time

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

func (Time) String

func (t Time) String() string

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL