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 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
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.
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 Key ¶
type Key struct {
// contains filtered or unexported fields
}
Key represents an extractor to access the value by key.
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 ¶
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 ¶
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 ¶
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 ¶
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) Extractors ¶ added in v1.1.0
Extractors returns query extractors of q.
Source Files ¶
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. |