Documentation
¶
Overview ¶
Package lql implements the Lockd Query Language (LQL) selector and mutation syntax for JSON documents.
Selectors ¶
LQL selectors are declarative predicates over JSON documents. They compile to a Selector AST and can be evaluated with Matches.
Basic forms:
eq{field=/status,value=open}
prefix{field=/owner,value=team-}
range{field=/progress,gt=10}
in{field=/state,any=["queued","running"]}
exists{/metadata/etag}
Logical composition:
and.eq{field=/status,value=open},and.range{field=/progress,gte=50}
or.eq{field=/region,value=us},or.eq{field=/region,value=eu}
not.eq{field=/state,value=disabled}
Shorthand:
/status="open" /status!=closed /progress>=50
Arrays:
/devices/0/status="online" /items/2/sku="ABC-123"
Wildcards (selector paths):
- any child value of an object (objects only; arrays do not match) [] any element of an array (arrays only; objects do not match) ** any child (object value or array element) ... recursive descent (any depth, objects or arrays)
Type mismatches do not match (e.g. [] on an object). Bracket sugar expands "/items[]/sku" to "/items/[]/sku".
/labels/*="production" /items[]/sku="ABC-123" /items/**/sku="ABC-123" /items/.../sku="ABC-123"
Example:
sel, _ := lql.ParseSelectorString(`/status="open",/progress>=50`)
ok := lql.Matches(sel, map[string]any{
"status": "open",
"progress": 72,
})
Mutations ¶
Mutations mutate JSON objects in-place using JSON Pointer paths.
/state/status=running # set /state/retries++ # increment by 1 /state/retries=+3 # add 3 rm:/state/legacy # delete time:/state/updated=NOW # RFC3339 timestamp
Mutations support the same wildcard semantics as selectors. When a wildcard path segment is used, missing paths under matched nodes are skipped.
Brace shorthand applies a set of nested mutations under a prefix:
/state{/owner="alice",/note="hi"}
Example:
doc := map[string]any{"state": map[string]any{"retries": 1}}
_ = lql.Mutate(doc, "/state/retries=+2", "/state/status=running")
The package is intentionally small and dependency-free so it can be embedded in CLIs and services that need LQL parsing or evaluation.
Index ¶
- func ApplyMutations(doc map[string]any, muts []Mutation) error
- func Matches(sel Selector, doc map[string]any) bool
- func MatchesValue(sel Selector, value any) bool
- func Mutate(doc map[string]any, exprs ...string) error
- func MutateWithTime(doc map[string]any, now time.Time, exprs ...string) error
- type InTerm
- type Mutation
- type MutationKind
- type RangeTerm
- type Selector
- type Term
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyMutations ¶
ApplyMutations mutates the provided JSON object in-place according to muts.
func MatchesValue ¶
MatchesValue reports whether value satisfies sel. Only JSON objects match.
Types ¶
type Mutation ¶
type Mutation struct {
Path []string
Kind MutationKind
Value any
Delta float64
}
Mutation describes a parsed mutation expression.
func Mutations ¶
Mutations parses variadic mutation expressions (each of which may contain comma/newline separated clauses) using the provided timestamp for time: operands.
func ParseMutations ¶
ParseMutations parses CLI-style mutation expressions into Mutation structs. Paths follow JSON Pointer semantics (`/foo/bar`), so literal dots or spaces in keys require no extra quoting. Brace shorthand (`/foo{/bar=1,/baz=2}`), rm:/time: prefixes, and ++/--/+=/-= increment forms are supported.
type MutationKind ¶
type MutationKind int
MutationKind identifies the operation applied to a JSON path.
const ( // MutationSet assigns a value to the target path. MutationSet MutationKind = iota // MutationIncrement adds/subtracts a numeric delta to the target path. MutationIncrement // MutationRemove deletes the target path. MutationRemove )
type RangeTerm ¶
type RangeTerm struct {
Field string `json:"field"`
GTE *float64 `json:"gte,omitempty"`
GT *float64 `json:"gt,omitempty"`
LTE *float64 `json:"lte,omitempty"`
LT *float64 `json:"lt,omitempty"`
}
RangeTerm captures numeric/timestamp bounds.
type Selector ¶
type Selector struct {
And []Selector `json:"and,omitempty"`
Or []Selector `json:"or,omitempty"`
Not *Selector `json:"not,omitempty"`
Eq *Term `json:"eq,omitempty"`
Prefix *Term `json:"prefix,omitempty"`
Range *RangeTerm `json:"range,omitempty"`
In *InTerm `json:"in,omitempty"`
Exists string `json:"exists,omitempty"`
}
Selector represents the recursive selector AST.
func ParseSelectorString ¶
ParseSelectorString parses a comma/newline separated selector expression string (LQL). When multiple expressions are provided, non-explicit clauses are combined with AND.
func ParseSelectorStringOr ¶ added in v0.4.0
ParseSelectorStringOr parses a comma/newline separated selector expression string (LQL). When multiple expressions are provided, non-explicit clauses are combined with OR.
Example ¶
sel, err := ParseSelectorStringOr(`/status="ok",/msg="done"`)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(!sel.IsEmpty(), len(sel.Or))
Output: true 2
func ParseSelectorStrings ¶ added in v0.3.0
ParseSelectorStrings parses each selector expression and combines them with AND.
Example ¶
sel, err := ParseSelectorStrings([]string{`/status="ok"`, `/msg="done"`})
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(!sel.IsEmpty(), len(sel.And))
Output: true 2
func ParseSelectorStringsOr ¶ added in v0.3.0
ParseSelectorStringsOr parses each selector expression and combines them with OR.
Example ¶
sel, err := ParseSelectorStringsOr([]string{`/status="ok"`, `/msg="done"`})
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(!sel.IsEmpty(), len(sel.Or))
Output: true 2
func ParseSelectorValues ¶
ParseSelectorValues scans URL parameters for selector expressions and returns the AST.
func (*Selector) UnmarshalJSON ¶
UnmarshalJSON ensures empty selectors stay zeroed without nil pointers.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
lql
command
Command lql evaluates LQL selectors and applies LQL mutations over JSON input.
|
Command lql evaluates LQL selectors and applies LQL mutations over JSON input. |
|
Package jsonpointer implements RFC 6901 JSON Pointer utilities.
|
Package jsonpointer implements RFC 6901 JSON Pointer utilities. |