query

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: AGPL-3.0, AGPL-3.0-or-later Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package query parses and validates declarative published queries (D2).

Index

Constants

View Source
const ErrInputInvalid = "query.input_invalid"

ErrInputInvalid is the code reported (HTTP 400) for bad call input.

View Source
const ErrInvalidDefinition = "query.invalid_definition"

ErrInvalidDefinition is the code reported for load-time failures.

View Source
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

type InputError struct {
	Field string
	Msg   string
}

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 Page

type Page struct {
	Rows []map[string]any
	Next []any
}

Page is one result page. Next is nil on the last page.

func Run

func Run(ctx context.Context, conn *sql.DB, dialect db.Dialect, d *Definition, req Request) (*Page, error)

Run executes the query and returns one page. Values are returned as the driver's exact text for byte-typed columns (decimals stay exact).

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.

type Sort

type Sort struct {
	Field string `yaml:"field" json:"field,omitempty"`
	Dir   string `yaml:"dir" json:"dir,omitempty"`
}

Sort is one ordering key.

func Build

func Build(d *Definition, dialect db.Dialect, req Request) (string, []any, []Sort, error)

Build returns the dialect-bound SQL and args for one page (limit+1 rows). The sort list always ends with name for a stable keyset; the last len(sort) selected columns are cursor values.

Jump to

Keyboard shortcuts

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