Documentation
¶
Overview ¶
Package client builds requests in parse's grammar from Go: filters, sort orders, list parameters, merge-patch members and JSON Patch operations. It imports only the standard library, so an SDK built on it depends on no server code.
A catalog of typed fields per resource, usually generated, names the fields and fixes their operand types, so a misspelt field or an operand of the wrong type does not compile:
var Devices = struct {
Name client.SortableText
Enabled client.Field[bool]
LastSeen client.SortableTime
}{client.NewSortableText("name"), client.NewField[bool]("enabled"), client.NewSortableTime("last_seen")}
q, err := client.ListParams{
Filter: client.And(Devices.Name.Like("pump-%"), Devices.LastSeen.Gte(client.Ago(24*time.Hour))),
Sort: client.Sort{Devices.LastSeen.Desc()},
}.Values()
The field types follow the operators each value type can declare; the server still refuses an operator a field does not allow. The client refuses only what the grammar cannot express: an Or of nothing, Not of the empty filter, and a relative time finer than a second.
Index ¶
- Variables
- func Pointer(tokens ...string) string
- type Document
- type Elements
- type Field
- type Filter
- type Geo
- type ListParams
- type Moment
- type Nullable
- type Operation
- type Order
- type Ordered
- type Point
- type Reference
- type Relation
- type Shape
- type Sort
- type SortKey
- type SortableField
- type SortableOrdered
- type SortableText
- type SortableTime
- type Text
- type Time
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEmptyOr reports an Or of no filters. A disjunction of nothing // matches no row, which the grammar cannot express. ErrEmptyOr = errors.New("client: Or needs at least one filter") // ErrNegatedEmpty reports Not of the empty filter, which would match no // row; the grammar cannot express that either. ErrNegatedEmpty = errors.New("client: Not of the empty filter matches nothing") )
var ErrSubSecond = errors.New("client: a relative time is a whole number of seconds")
ErrSubSecond reports a relative Moment that is not a whole number of seconds, the server's finest relative unit.
Functions ¶
Types ¶
type Document ¶
type Document[V any] struct { // contains filtered or unexported fields }
Document is a JSON document field whose values have type V.
func NewDocument ¶
NewDocument returns the document field named name.
func (Document[V]) Contains ¶
Contains matches rows whose document contains v: every member v encodes, at any depth, is in the document with the same value.
type Elements ¶
Elements is a JSON array of objects whose element members the server declares; Some and None test one element against a filter over them.
func NewElements ¶
NewElements returns the element array named name.
type Field ¶
type Field[V any] struct { // contains filtered or unexported fields }
Field is a filterable field whose values have type V, compared for equality, membership and null: a key, an enum, a boolean.
type Filter ¶
type Filter struct {
// contains filtered or unexported fields
}
Filter is a filter expression in parse's request grammar, sent as a list's filter parameter. The zero value matches every row. Filters are immutable: field methods build conditions, and And, Or and Not combine them.
A filter the grammar cannot express (an Or of nothing, Not of the empty filter, a sub-second relative time) carries its error, and encoding it returns that error.
func And ¶
And matches the rows every filter matches; And of no filters is the empty filter. Nested Ands are flattened, so a filter built up in a loop stays within the server's nesting depth.
func Or ¶
Or matches the rows any filter matches. An Or holding the empty filter matches every row; an Or of no filters is ErrEmptyOr. Nested Ors are flattened, as And's are.
func (Filter) MarshalJSON ¶
MarshalJSON encodes f as a filter document: {} for the empty filter.
type Geo ¶
type Geo struct {
// contains filtered or unexported fields
}
Geo is a geographic field.
func (Geo) Intersects ¶
Intersects matches rows whose geometry intersects s.
type ListParams ¶
type ListParams struct {
Filter Filter
Sort Sort
// Cursor is the end_cursor of the page before the one to fetch; empty
// fetches the first page.
Cursor string
// PageSize is the number of rows per page; 0 is the server's default.
PageSize int
// IncludeTotal asks for the number of matching rows.
IncludeTotal bool
// IncludeDeleted lists soft-deleted rows too.
IncludeDeleted bool
// Search is free text matched against the resource's search fields.
Search string
// Facets names the fields to count values of.
Facets []string
// Include names the relations to return with each row.
Include []string
// Fields names the members each row returns.
Fields []string
}
ListParams is one list request: what parse's Resource.ParseList reads from a query string. Zero members are left out, so the server applies its defaults.
type Moment ¶
type Moment struct {
// contains filtered or unexported fields
}
Moment is a timestamp operand: an absolute time (At), or a time relative to the server's clock (Now, Ago, FromNow), which the server resolves when it reads the request, so the client's clock never skews the comparison.
type Nullable ¶
type Nullable[T any] struct { // contains filtered or unexported fields }
Nullable is a member of a merge patch (RFC 7386) struct. The zero value is absent and, with the omitzero tag option, leaves the field alone; Null clears it; Set writes a value.
type DevicePatch struct {
Name client.Nullable[string] `json:"name,omitzero"`
Note client.Nullable[string] `json:"note,omitzero"`
}
body, err := json.Marshal(DevicePatch{Name: client.Set("pump"), Note: client.Null[string]()})
// {"name":"pump","note":null}
func (Nullable[T]) MarshalJSON ¶
MarshalJSON encodes a set value, and null otherwise. An absent member is left out by its struct's omitzero option, never encoded.
func (*Nullable[T]) UnmarshalJSON ¶
UnmarshalJSON reads null as Null and any other value as Set.
type Operation ¶
type Operation struct {
// contains filtered or unexported fields
}
Operation is one JSON Patch (RFC 6902) operation; a []Operation is a JSON Patch document. Build one with Add, Remove, Replace, Move, Copy, Test or a Relation's membership methods.
func (Operation) MarshalJSON ¶
MarshalJSON encodes the operation with the members its op takes: value for add, replace and test (null included) and for a relation's remove, from for move and copy.
type Order ¶
type Order struct {
// contains filtered or unexported fields
}
Order is one sort key and its direction, from a SortKey's Asc or Desc.
type Ordered ¶
Ordered is a field whose values have an order: a number, a decimal (any type that marshals as its exact text, such as shopspring's), a timestamp.
func NewOrdered ¶
NewOrdered returns the ordered field named name.
type Reference ¶
Reference is a to-one (belongs-to) relation whose target key has type K. Is and IsNot filter by the target's fields.
func NewReference ¶
NewReference returns the reference named name.
type Relation ¶
Relation is a to-many relation, has-many or many-to-many, whose target keys have type K. Its key comparisons (Eq, In, Null, ...) ask whether a related row has that key; Some and None filter by the target's fields.
func NewRelation ¶
NewRelation returns the relation named name.
func (Relation[K]) None ¶
None matches rows with no related row that f matches; the empty filter matches rows with no related row.
func (Relation[K]) Remove ¶
Remove removes keys from the relation's members, as a JSON Patch operation. Unlike Remove of a path, it carries the keys.
type Shape ¶
type Shape struct {
// contains filtered or unexported fields
}
Shape is a region: a Box or a Polygon.
func (Shape) MarshalJSON ¶
MarshalJSON encodes the shape as the server reads it.
type SortKey ¶
type SortKey struct {
// contains filtered or unexported fields
}
SortKey sorts by a field. A sort-only field is a SortKey; the Sortable field types embed one.
func NewSortKey ¶
NewSortKey returns the sort key of the field named name.
type SortableField ¶
SortableField is a Field the server also sorts by.
func NewSortableField ¶
func NewSortableField[V any](name string) SortableField[V]
NewSortableField returns the sortable field named name.
type SortableOrdered ¶
SortableOrdered is an Ordered field the server also sorts by.
func NewSortableOrdered ¶
func NewSortableOrdered[V any](name string) SortableOrdered[V]
NewSortableOrdered returns the sortable ordered field named name.
type SortableText ¶
SortableText is a Text field the server also sorts by.
func NewSortableText ¶
func NewSortableText(name string) SortableText
NewSortableText returns the sortable text field named name.
type SortableTime ¶
type SortableTime = SortableOrdered[Moment]
SortableTime is a Time field the server also sorts by.
func NewSortableTime ¶
func NewSortableTime(name string) SortableTime
NewSortableTime returns the sortable timestamp field named name.