Documentation
¶
Overview ¶
Package query provides to extract the element from a Go value.
ParseString parses a query string and returns the query which extracts the value.
q, err := query.ParseString(`$.key[0].key['key']`) v, err := q.Extract(target)
Query Syntax ¶
The query syntax understood by this package when parsing is as follows.
$ the root element
.key extracts by a key of map or field name of struct ("." can be omitted if the head of query)
['key'] same as the ".key" (if the key contains "\" or "'", these characters must be escaped like "\\", "\'")
[0] extracts by a index of array or slice
Index ¶
- func IsCaseInsensitive(ctx context.Context) bool
- type ExtractFunc
- type Extractor
- type Index
- type IndexExtractor
- type IndexExtractorContext
- type Key
- type KeyExtractor
- type KeyExtractorContext
- type Option
- func CaseInsensitive() Option
- func CustomExtractFunc(f func(ExtractFunc) ExtractFunc) Option
- func CustomIsInlineStructFieldFunc(f func(reflect.StructField) bool) Option
- func CustomStructFieldNameGetter(f func(f reflect.StructField) string) Optiondeprecated
- func ExtractByStructTag(tagNames ...string) Option
- type Query
- func (q Query) Append(es ...Extractor) *Query
- func (q *Query) Extract(target interface{}) (interface{}, error)
- func (q *Query) ExtractContext(ctx context.Context, target interface{}) (interface{}, error)
- func (q *Query) Extractors() []Extractor
- func (q Query) Index(i int) *Query
- func (q Query) Key(k string) *Query
- func (q Query) Root() *Query
- func (q *Query) String() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsCaseInsensitive ¶ added in v1.3.0
IsCaseInsensitive reports whether case-insensitive querying is enabled or not.
Types ¶
type ExtractFunc ¶ added in v1.2.0
ExtractFunc represents a function to extracts a value.
type Index ¶
type Index struct {
// contains filtered or unexported fields
}
Index represents an extractor to access the value by index.
func (*Index) Extract ¶
Extract extracts the value from v by index. It reports whether the index is found and returns the found value.
If v implements the IndexExtractor interface, this method extracts by calling v.ExtractByIndex.
func (*Index) ExtractContext ¶ added in v1.4.0
ExtractContext extracts the value from v by index, passing ctx to a context-aware extractor if v implements one.
If v implements the IndexExtractorContext interface, this method extracts by calling v.ExtractByIndex with ctx; otherwise it falls back to IndexExtractor.
type IndexExtractor ¶
IndexExtractor is the interface that wraps the ExtractByIndex method.
ExtractByIndex extracts the value by index. It reports whether the index is found and returns the found value.
type IndexExtractorContext ¶ added in v1.4.0
IndexExtractorContext is the interface that wraps the ExtractByIndex method.
ExtractByIndex extracts the value by index. It reports whether the index is found and returns the found value.
type Key ¶
type Key struct {
// contains filtered or unexported fields
}
Key represents an extractor to access the value by key.
func (*Key) Extract ¶
Extract extracts the value from v by key. It reports whether the key is found and returns the found value.
If v implements the KeyExtractor interface, this method extracts by calling v.ExtractByKey.
func (*Key) ExtractContext ¶ added in v1.4.0
ExtractContext extracts the value from v by key, passing ctx (extended with the query options) to a context-aware extractor if v implements one.
If v implements the KeyExtractorContext interface, this method extracts by calling v.ExtractByKey with ctx; otherwise it falls back to KeyExtractor.
type KeyExtractor ¶
KeyExtractor is the interface that wraps the ExtractByKey method.
ExtractByKey extracts the value by key. It reports whether the key is found and returns the found value.
Example ¶
package main
import (
"fmt"
"github.com/zoncoen/query-go"
)
type orderedMap struct {
elems []*elem
}
type elem struct {
k, v interface{}
}
func (m *orderedMap) ExtractByKey(key string) (interface{}, bool) {
for _, e := range m.elems {
if k, ok := e.k.(string); ok {
if k == key {
return e.v, true
}
}
}
return nil, false
}
func main() {
q := query.New().Key("key")
v, _ := q.Extract(&orderedMap{
elems: []*elem{{k: "key", v: "value"}},
})
fmt.Println(v)
}
Output: value
type KeyExtractorContext ¶ added in v1.3.0
KeyExtractorContext is the interface that wraps the ExtractByKey method.
ExtractByKey extracts the value by key. It reports whether the key is found and returns the found value.
type Option ¶
type Option func(*Query)
Option represents an option for Query.
func CaseInsensitive ¶ added in v1.2.0
func CaseInsensitive() Option
CaseInsensitive returns the Option to match case insensitivity.
Example ¶
package main
import (
"fmt"
"github.com/zoncoen/query-go"
)
// Person represents a person.
type Person struct {
Name string `json:"name,omitempty"`
}
func main() {
person := Person{
Name: "Alice",
}
q := query.New(query.CaseInsensitive()).Key("NAME")
name, _ := q.Extract(person)
fmt.Println(name)
}
Output: Alice
func CustomExtractFunc ¶ added in v1.2.0
func CustomExtractFunc(f func(ExtractFunc) ExtractFunc) Option
CustomExtractFunc returns the Option to customize the behavior of extractors.
Example ¶
package main
import (
"fmt"
"reflect"
"github.com/zoncoen/query-go"
)
// Person represents a person.
type Person struct {
Name string `json:"name,omitempty"`
}
func main() {
person := Person{
Name: "Alice",
}
q := query.New(
query.CustomExtractFunc(func(f query.ExtractFunc) query.ExtractFunc {
return func(v reflect.Value) (reflect.Value, bool) {
return reflect.ValueOf("Bob"), true
}
}),
).Key("name")
name, _ := q.Extract(person)
fmt.Println(name)
}
Output: Bob
func CustomIsInlineStructFieldFunc ¶ added in v1.3.0
func CustomIsInlineStructFieldFunc(f func(reflect.StructField) bool) Option
CustomIsInlineStructFieldFunc returns the Option to customize the behavior of extractors.
func CustomStructFieldNameGetter
deprecated
func CustomStructFieldNameGetter(f func(f reflect.StructField) string) Option
CustomStructFieldNameGetter returns the Option to set f as custom function which gets struct field name. f is called by Key.Extract to get struct field name, if the target value is a struct.
Deprecated: Use CustomExtractFunc instead.
Example ¶
package main
import (
"fmt"
"reflect"
"strings"
"github.com/zoncoen/query-go"
)
// Person represents a person.
type Person struct {
Name string `json:"name,omitempty"`
}
// getFieldNameByJSONTag returns the JSON field tag as field name if exists.
func getFieldNameByJSONTag(field reflect.StructField) string {
tag, ok := field.Tag.Lookup("json")
if ok {
strs := strings.Split(tag, ",")
return strs[0]
}
return field.Name
}
func main() {
person := Person{
Name: "Alice",
}
q := query.New(
query.CustomStructFieldNameGetter(getFieldNameByJSONTag),
).Key("name")
name, _ := q.Extract(person)
fmt.Println(name)
}
Output: Alice
func ExtractByStructTag ¶ added in v1.2.0
ExtractByStructTag returns the Option to allow extracting by struct tag.
Example ¶
package main
import (
"fmt"
"github.com/zoncoen/query-go"
)
// Person represents a person.
type Person struct {
Name string `json:"name,omitempty"`
}
func main() {
person := Person{
Name: "Alice",
}
q := query.New(query.ExtractByStructTag("json")).Key("name")
name, _ := q.Extract(person)
fmt.Println(name)
}
Output: Alice
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query represents a query to extract the element from a value.
func ParseString ¶
ParseString parses a query string s and returns the corresponding Query.
Example ¶
package main
import (
"fmt"
"github.com/zoncoen/query-go"
)
type S struct {
Maps []map[string]map[string]string
}
func main() {
q, err := query.ParseString(`$.Maps[0].key['.key\'']`)
if err == nil {
v, _ := q.Extract(&S{
Maps: []map[string]map[string]string{
{"key": map[string]string{
".key'": "value",
}},
},
})
fmt.Println(v)
}
}
Output: value
func (*Query) ExtractContext ¶ added in v1.4.0
ExtractContext extracts the value by q from target, passing ctx to each extractor that supports it (e.g. a value implementing KeyExtractorContext or IndexExtractorContext). Extractors that are not context-aware behave exactly as in Extract.
func (*Query) Extractors ¶ added in v1.1.0
Extractors returns query extractors of q.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package ast declares the types used to represent syntax trees.
|
Package ast declares the types used to represent syntax trees. |
|
extractor
|
|
|
protobuf
module
|
|
|
yaml
module
|
|
|
Package parser implements a parser for a query string.
|
Package parser implements a parser for a query string. |
|
Package token defines constants representing the lexical tokens.
|
Package token defines constants representing the lexical tokens. |