Documentation
¶
Overview ¶
Package query parses and validates declarative published queries (D2).
Index ¶
Constants ¶
const ErrInputInvalid = "query.input_invalid"
ErrInputInvalid is the code reported (HTTP 400) for bad call input.
const ErrInvalidDefinition = "query.invalid_definition"
ErrInvalidDefinition is the code reported for load-time failures.
const ErrTooManyGroups = "query.too_many_groups"
ErrTooManyGroups is the code reported when an aggregate query yields more than limit groups.
Variables ¶
This section is empty.
Functions ¶
func Encode ¶
func Encode(defs []*Definition) ([]json.RawMessage, error)
Encode serialises definitions for the tenant config snapshot.
Types ¶
type Aggregate ¶
type Aggregate struct {
Name string `yaml:"name" json:"name,omitempty"`
Fn string `yaml:"fn" json:"fn,omitempty"`
Field string `yaml:"field" json:"field,omitempty"`
}
Aggregate is one output column computed over the grouped rows.
type Definition ¶
type Definition struct {
Name string `yaml:"name" json:"name,omitempty"`
ModuleOwnerID string `yaml:"module_owner_id" json:"module_owner_id,omitempty"`
Capability string `yaml:"capability" json:"capability,omitempty"`
Operation string `yaml:"operation" json:"operation,omitempty"`
Source string `yaml:"source" json:"source,omitempty"`
Via string `yaml:"via" json:"via,omitempty"`
Permission string `yaml:"permission" json:"permission,omitempty"`
Scope string `yaml:"scope" json:"scope,omitempty"`
OwnerField string `yaml:"owner_field" json:"owner_field,omitempty"`
Input map[string]InputSpec `yaml:"input" json:"input,omitempty"`
Filters []Filter `yaml:"filters" json:"filters,omitempty"`
Fields []string `yaml:"fields" json:"fields,omitempty"`
GroupBy []string `yaml:"group_by" json:"group_by,omitempty"`
Aggregates []Aggregate `yaml:"aggregates" json:"aggregates,omitempty"`
Sort []Sort `yaml:"sort" json:"sort,omitempty"`
Limit int `yaml:"limit" json:"limit,omitempty"`
OfflineEligible bool `yaml:"offline_eligible" json:"offline_eligible,omitempty"`
Presentation map[string]string `yaml:"presentation" json:"presentation,omitempty"`
}
Definition is a query shipped in queries/*.yaml.
func Decode ¶
func Decode(raws []json.RawMessage, reg *doctype.Registry) ([]*Definition, error)
Decode parses and validates snapshot queries against reg (JSON is valid YAML).
func LoadDir ¶
func LoadDir(dir string, reg *doctype.Registry) ([]*Definition, error)
LoadDir parses every yaml file in dir; a missing dir yields none.
func Parse ¶
func Parse(raw []byte, reg *doctype.Registry) (*Definition, error)
Parse decodes strictly (unknown keys rejected) and validates against reg.
func (*Definition) CatalogRef ¶
func (d *Definition) CatalogRef() string
CatalogRef is owner~capability~operation.
func (*Definition) OutputFields ¶
func (d *Definition) OutputFields() []string
OutputFields lists the row keys a run returns: fields, or group_by then aggregate names.
func (*Definition) ParentDocType ¶
func (d *Definition) ParentDocType() string
ParentDocType returns the parent DocType of a child source ("" otherwise).
func (*Definition) RequiredInputs ¶
func (d *Definition) RequiredInputs() []string
RequiredInputs lists the inputs a call must supply: those a non-optional filter reads and that declare no default. It mirrors what the executor rejects, so the published schema and the runtime agree.
type Filter ¶
type Filter struct {
Field string `yaml:"field" json:"field,omitempty"`
Op string `yaml:"op" json:"op,omitempty"`
Value any `yaml:"value" json:"value,omitempty"`
Any []Filter `yaml:"any" json:"any,omitempty"`
Optional bool `yaml:"optional" json:"optional,omitempty"`
}
Filter is one entry of the AND list: a leaf (Field/Op/Value) or an OR group (Any).
type InputError ¶
InputError is a call the caller got wrong: a missing required input, an empty "in" list, a cursor that does not match the sort. Callers report it as a validation failure, never as a server fault.
func (*InputError) Error ¶
func (e *InputError) Error() string
type InputSpec ¶
type InputSpec struct {
Type string `yaml:"type" json:"type,omitempty"`
MaxLength int `yaml:"max_length" json:"max_length,omitempty"`
Enum []string `yaml:"enum" json:"enum,omitempty"`
// Default stands in when the caller omits the input: a literal of the
// input's type, or $today, $now or $actor.
Default any `yaml:"default" json:"default,omitempty"`
}
InputSpec describes one call input.
type Request ¶
type Request struct {
Input map[string]any
Actor string
Now time.Time
FullRead bool // role may read all rows: owner_field is not applied
ScopeField string // DocType business-scope column; empty for tenant scope
ScopeValue any // verified BusinessScope value
Cursor []any // sort-column values + name of the last row seen
Limit int // page size the caller asked for; 0 means the definition's limit
}
Request carries one call's verified context. Permission checks happen before the executor; it applies scope and owner_field row limits.