github.com/3JoB/go-json@v0.10.4/query.go (about) 1 package json 2 3 import ( 4 "github.com/3JoB/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 13 FieldQueryString = encoder.FieldQueryString 14 ) 15 16 var ( 17 // FieldQueryFromContext get current FieldQuery from context.Context. 18 FieldQueryFromContext = encoder.FieldQueryFromContext 19 20 // SetFieldQueryToContext set current FieldQuery to context.Context. 21 SetFieldQueryToContext = encoder.SetFieldQueryToContext 22 ) 23 24 // BuildFieldQuery builds FieldQuery by fieldName or sub field query. 25 // First, specify the field name that you want to keep in structure type. 26 // If the field you want to keep is a structure type, by creating a sub field query using BuildSubFieldQuery, 27 // you can select the fields you want to keep in the structure. 28 // This description can be written recursively. 29 func BuildFieldQuery(fields ...FieldQueryString) (*FieldQuery, error) { 30 query, err := Marshal(fields) 31 if err != nil { 32 return nil, err 33 } 34 return FieldQueryString(query).Build() 35 } 36 37 // BuildSubFieldQuery builds sub field query. 38 func BuildSubFieldQuery(name string) *SubFieldQuery { 39 return &SubFieldQuery{name: name} 40 } 41 42 type SubFieldQuery struct { 43 name string 44 } 45 46 func (q *SubFieldQuery) Fields(fields ...FieldQueryString) FieldQueryString { 47 query, _ := Marshal(map[string][]FieldQueryString{q.name: fields}) 48 return FieldQueryString(query) 49 }