Help us shape the future of Go! Take the 2026 Go Developer Survey

query

package
v0.26.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package query defines Ridu's transport-independent query language.

The same query model is shared by access filters, local operations, the HTTP API, generated clients, and storage adapters.

Index

Constants

View Source
const (
	// MaxPathSegments bounds recursive query compilation and schema traversal.
	// Ridu's operation engine supports substantially shallower authored models,
	// leaving this ceiling as a fail-safe rather than a practical restriction.
	MaxPathSegments = 64
	// MaxPathBytes bounds user-supplied filter/sort/population path allocation.
	MaxPathBytes = 4096
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Comparison

type Comparison struct {
	Path     Path
	Operator Operator
	Value    Value
}

Comparison is a read-only snapshot of a comparison expression.

type Direction

type Direction string

Direction controls one stable sort term.

const (
	Ascending  Direction = "asc"
	Descending Direction = "desc"
)

type Expression

type Expression interface {
	Kind() ExpressionKind
	Node() Node
	// contains filtered or unexported methods
}

Expression is an immutable query expression. Implementations are sealed so every transport and store sees the same finite query language.

func And

func And(conditions ...Expression) Expression

And matches documents that satisfy every condition. With one condition it returns that condition. Like Equal, it panics when given no conditions or a nil one; check the length of a list built from input before calling it.

func Compare

func Compare(path Path, operator Operator, value Value) (Expression, error)

Compare constructs a validated field comparison from an operator chosen at run time, such as one read from a request. It returns an error where the helpers below panic.

func Contains

func Contains[P FieldPath](path P, value string) Expression

Contains matches documents whose text field contains value, ignoring case.

func Equal

func Equal[P FieldPath, V Operand](path P, value V) Expression

Equal matches documents whose field equals value, as in Equal("status", "published") or Equal("parent", query.Null()).

func Exists added in v0.6.0

func Exists[P FieldPath](path P, exists bool) Expression

Exists matches documents that have a value for the field when exists is true, and documents without one when it is false.

func GreaterThan

func GreaterThan[P FieldPath, V Ordered](path P, value V) Expression

GreaterThan matches documents whose field is greater than value.

func GreaterThanEqual

func GreaterThanEqual[P FieldPath, V Ordered](path P, value V) Expression

GreaterThanEqual matches documents whose field is at least value.

func In

func In[P FieldPath, V Operand](path P, values ...V) Expression

In matches documents whose field equals one of values, as in In("status", "draft", "published"). With no values it matches nothing.

func LessThan

func LessThan[P FieldPath, V Ordered](path P, value V) Expression

LessThan matches documents whose field is less than value.

func LessThanEqual

func LessThanEqual[P FieldPath, V Ordered](path P, value V) Expression

LessThanEqual matches documents whose field is at most value.

func Like

func Like[P FieldPath](path P, value string) Expression

Like matches documents whose text field contains every word of value, ignoring case and word order.

func Not

func Not(condition Expression) Expression

Not matches documents that do not satisfy condition. It panics when condition is nil.

func NotEqual

func NotEqual[P FieldPath, V Operand](path P, value V) Expression

NotEqual matches documents whose field differs from value.

func Or

func Or(conditions ...Expression) Expression

Or matches documents that satisfy at least one condition. With one condition it returns that condition. It panics when given no conditions or a nil one.

type ExpressionKind

type ExpressionKind string

ExpressionKind identifies the shape of an expression node.

const (
	ExpressionComparison ExpressionKind = "comparison"
	ExpressionAnd        ExpressionKind = "and"
	ExpressionOr         ExpressionKind = "or"
	ExpressionNot        ExpressionKind = "not"
)

type FieldPath added in v0.6.0

type FieldPath interface {
	~string | Path
}

FieldPath is a field in a filter or sort: a dot-separated name written in your code, such as "title" or "seo.title", or a Path. A malformed name panics, like regexp.MustCompile; use ParsePath for names from user input.

type Node

type Node struct {
	Kind       ExpressionKind
	Comparison *Comparison
	Children   []Node
}

Node is a detached recursive snapshot for adapters and protocol encoders. Mutating a Node never mutates the originating Expression.

type Operand added in v0.6.0

type Operand interface {
	~string | ~bool | ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 | Value
}

Operand is a value compared with a field: a string, boolean, integer, or float, including named types such as a string enum, or a Value built with String, Number, Boolean, Null, Reference, or List.

type Operator

type Operator string

Operator identifies a field comparison.

const (
	OperatorEqual            Operator = "equal"
	OperatorNotEqual         Operator = "not_equal"
	OperatorIn               Operator = "in"
	OperatorExists           Operator = "exists"
	OperatorGreaterThan      Operator = "greater_than"
	OperatorGreaterThanEqual Operator = "greater_than_equal"
	OperatorLessThan         Operator = "less_than"
	OperatorLessThanEqual    Operator = "less_than_equal"
	OperatorContains         Operator = "contains"
	OperatorLike             Operator = "like"
)

type Ordered added in v0.6.0

type Ordered interface {
	~string | ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 | Value
}

Ordered is an Operand that greater-than and less-than comparisons accept: strings and numbers, or a Value holding one.

type Path

type Path struct {
	// contains filtered or unexported fields
}

Path is an immutable, validated document field path. Its canonical string form joins segments with a dot, for example "seo.title". The exact single-segment paths "_status" and "_revision" address Ridu's finite version metadata; arbitrary underscore-prefixed or nested system paths remain invalid.

func Field added in v0.5.0

func Field(segments ...string) Path

Field names a field by its segments, such as Field("seo", "title"). Like regexp.MustCompile, it panics when a name is malformed. Filter helpers also accept the dot-separated string "seo.title" directly; use Field when you need a Path value, such as for ListOptions.Select.

func NewPath

func NewPath(segments ...string) (Path, error)

NewPath validates and constructs a field path from segments computed at run time, such as names read from a schema. For a name written in your code, pass the string to a filter helper instead.

func ParsePath

func ParsePath(value string) (Path, error)

ParsePath validates and constructs a path from its dot-separated form. Use it for names that come from user input, where a malformed name is an error.

func (Path) AppendSegments added in v0.15.0

func (path Path) AppendSegments(segments []string) []string

AppendSegments appends the path segments to segments.

func (Path) Len added in v0.15.0

func (path Path) Len() int

Len returns the number of path segments.

func (Path) MarshalJSON

func (path Path) MarshalJSON() ([]byte, error)

MarshalJSON encodes a path in its canonical dot-separated form.

func (Path) Segment added in v0.15.0

func (path Path) Segment(index int) string

Segment returns segment index without copying the path.

func (Path) Segments

func (path Path) Segments() []string

Segments returns a copy of the path segments.

func (Path) String

func (path Path) String() string

func (*Path) UnmarshalJSON

func (path *Path) UnmarshalJSON(encoded []byte) error

UnmarshalJSON validates the canonical string before replacing the path.

type Population

type Population struct {
	Path  Path
	Depth int
	// Select is nil for the complete populated target. A non-nil empty slice
	// returns only document metadata.
	Select []Path
}

Population asks a store to replace one relationship ID with its projected target document. Depth is bounded by the operation engine.

type Sort

type Sort struct {
	Path      Path
	Direction Direction
}

Sort is one validated ordered field term. Stores append ID as a stable tiebreaker when callers do not provide it explicitly.

func Asc added in v0.6.0

func Asc[P FieldPath](path P) Sort

Asc sorts by a field from lowest to highest, as in Asc("title").

func Desc added in v0.6.0

func Desc[P FieldPath](path P) Sort

Desc sorts by a field from highest to lowest, as in Desc("createdAt").

func NewSort

func NewSort(path Path, direction Direction) (Sort, error)

NewSort validates a sort term whose direction is chosen at run time, such as one read from a request. Use Asc or Desc for a sort written in your code.

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value is an immutable query operand. Explicit constructors prevent arbitrary application values from leaking into the public query contract.

func Boolean

func Boolean(value bool) Value

Boolean constructs a boolean operand.

func DateTime added in v0.6.0

func DateTime(t time.Time) Value

DateTime converts t to the text a date-and-time field stores, so it can be compared with that field or with createdAt and updatedAt, as in GreaterThan("publishedAt", DateTime(time.Now())). Date-only fields store "2006-01-02"; compare them with t.Format(time.DateOnly).

func List

func List(values ...Value) Value

List constructs a copied list operand.

func Null

func Null() Value

Null constructs a null operand.

func Number

func Number(value float64) Value

Number constructs a number operand.

func Reference added in v0.15.0

func Reference(relationTo, id string) Value

Reference constructs a polymorphic relationship candidate: document id in collection relationTo. It equals a stored {relationTo, id} reference, as in In("subjects", Reference("posts", postID)).

func String

func String(value string) Value

String constructs a string operand.

func (Value) BooleanValue

func (value Value) BooleanValue() (bool, bool)

BooleanValue returns the boolean and true when this is a boolean operand.

func (Value) Kind

func (value Value) Kind() ValueKind

Kind returns the operand's discriminant.

func (Value) MarshalJSON

func (value Value) MarshalJSON() ([]byte, error)

func (Value) NumberValue

func (value Value) NumberValue() (float64, bool)

NumberValue returns the number and true when this is a number operand.

func (Value) ReferenceValue added in v0.15.0

func (value Value) ReferenceValue() (relationTo, id string, ok bool)

ReferenceValue returns the collection slug, document ID, and true when this is a reference operand.

func (Value) StringValue

func (value Value) StringValue() (string, bool)

StringValue returns the string and true when this is a string operand.

func (Value) Values

func (value Value) Values() []Value

Values returns a deep copy for a list operand.

type ValueKind

type ValueKind string

ValueKind identifies the concrete scalar, reference, or list represented by a Value.

const (
	ValueString  ValueKind = "string"
	ValueNumber  ValueKind = "number"
	ValueBoolean ValueKind = "boolean"
	ValueNull    ValueKind = "null"
	ValueList    ValueKind = "list"
	// ValueReference names one document among a polymorphic relationship's
	// target collections, in the relationship's {relationTo, id} value shape.
	ValueReference ValueKind = "reference"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL