Debian Codegraph @

query.go

raw
1 package json
2
3 import (
4 "github.com/goccy/go-json/internal/encoder"
5 )
6
7 type (
8 // FieldQuery you can dynamically filter the fields in the structure by creating a FieldQuery,
9 // adding it to context.Context using SetFieldQueryToContext and then passing it to MarshalContext.
10 // This is a type-safe operation, so it is faster than filtering using map[string]interface{}.
11 FieldQuery = encoder.FieldQuery
12 FieldQueryString = encoder.FieldQueryString
13 )
14
15 var (
16 // FieldQueryFromContext get current FieldQuery from context.Context.
17 FieldQueryFromContext = encoder.FieldQueryFromContext
18 // SetFieldQueryToContext set current FieldQuery to context.Context.
19 SetFieldQueryToContext = encoder.SetFieldQueryToContext
20 )
21
22 // BuildFieldQuery builds FieldQuery by fieldName or sub field query.
23 // First, specify the field name that you want to keep in structure type.
24 // If the field you want to keep is a structure type, by creating a sub field query using BuildSubFieldQuery,
25 // you can select the fields you want to keep in the structure.
26 // This description can be written recursively.
27 func BuildFieldQuery(fields ...FieldQueryString) (*FieldQuery, error) {
28 query, err := Marshal(fields)
29 if err != nil {
30 return nil, err
31 }
32 return FieldQueryString(query).Build()
33 }
34
35 // BuildSubFieldQuery builds sub field query.
36 func BuildSubFieldQuery(name string) *SubFieldQuery {
37 return &SubFieldQuery{name: name}
38 }
39
40 type SubFieldQuery struct {
41 name string
42 }
43
44 func (q *SubFieldQuery) Fields(fields ...FieldQueryString) FieldQueryString {
45 query, _ := Marshal(map[string][]FieldQueryString{q.name: fields})
46 return FieldQueryString(query)
47 }