Documentation
¶
Overview ¶
Package apiquery compiles declared API query capabilities into immutable, transport-neutral plans.
Index ¶
- type AuthorizeFunc
- type Bounds
- type Capability
- type CapabilityKind
- type CompileOptions
- type Constraint
- type CursorDecoder
- type CursorDirection
- type CursorState
- type Direction
- type ErrorCode
- type FieldDefinition
- type FilterDefinition
- type FilterExpr
- type Logic
- type NullOrder
- type Operator
- type Optional
- type PageMode
- type PageRequest
- type PaginationDefinition
- type Plan
- func (p *Plan) Canonical() ([]byte, error)
- func (p *Plan) Cost() int
- func (p *Plan) Cursor() *CursorState
- func (p *Plan) ExecutionFields() []string
- func (p *Plan) Filter() *FilterExpr
- func (p *Plan) Includes() []string
- func (p *Plan) MandatoryConstraints() []Constraint
- func (p *Plan) Page() PageRequest
- func (p *Plan) Resource() string
- func (p *Plan) ResponseFields() []string
- func (p *Plan) SchemaRevision() string
- func (p *Plan) Sorts() []SortTerm
- type Predicate
- type RelationshipDefinition
- type Request
- type Schema
- type SchemaConfig
- type SortDefinition
- type SortTerm
- type Value
- type ValueType
- type Violation
- type Violations
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthorizeFunc ¶
type AuthorizeFunc func(context.Context, Capability) bool
AuthorizeFunc decides whether a declared capability is available to the current principal. It runs only after the schema declaration is found.
type Bounds ¶
type Bounds struct {
MaxRequestBytes int
MaxFields int
MaxIncludes int
MaxIncludeDepth int
MaxFilterDepth int
MaxFilterNodes int
MaxValues int
MaxMembership int
MaxStringBytes int
MaxSorts int
MaxPageSize int
MaxCursorBytes int
MaxCanonicalBytes int
MaxErrors int
MaxCost int
}
Bounds contains hard compilation and resource limits. Zero values receive conservative defaults rather than disabling a bound.
type Capability ¶
type Capability struct {
Kind CapabilityKind
Name string
}
Capability describes a declared element to an application authorizer.
type CapabilityKind ¶
type CapabilityKind string
CapabilityKind identifies an authorization decision without exposing schema internals through failures.
const ( CapabilityField CapabilityKind = "field" CapabilityFilter CapabilityKind = "filter" CapabilitySort CapabilityKind = "sort" CapabilityRelationship CapabilityKind = "relationship" )
type CompileOptions ¶
type CompileOptions struct {
Authorize AuthorizeFunc
MandatoryConstraints []Constraint
CursorDecoder CursorDecoder
}
CompileOptions supplies request-scoped policy without mutating the schema.
type Constraint ¶
type Constraint struct {
Name string `json:"name"`
Value Value `json:"value"`
Protected bool `json:"protected"`
}
Constraint is a server-owned mandatory equality predicate. Transport input cannot create, remove, or replace constraints.
type CursorDecoder ¶
type CursorDecoder interface {
DecodeCursor(context.Context, string, string, []SortTerm) (CursorState, error)
}
CursorDecoder authenticates and decodes one opaque cursor for an exact schema revision and ordered sort contract.
type CursorDirection ¶
type CursorDirection string
CursorDirection identifies the seek direction authenticated by a cursor.
const ( CursorForward CursorDirection = "forward" CursorBackward CursorDirection = "backward" )
type CursorState ¶
type CursorState struct {
Direction CursorDirection `json:"direction"`
Positions []Value `json:"positions"`
Policy string `json:"policy,omitempty"`
}
CursorState is the authenticated, typed cursor state retained by a plan. Positions are sensitive application data and must not be logged.
type ErrorCode ¶
type ErrorCode string
ErrorCode is a stable machine-readable query failure category.
const ( CodeInvalidElement ErrorCode = "invalid_element" CodeUnsupported ErrorCode = "unsupported_operation" CodeConflict ErrorCode = "conflict" CodeAuthorization ErrorCode = "authorization_rejected" CodeCostLimit ErrorCode = "cost_limit" CodeCursorFailure ErrorCode = "cursor_failure" CodeVersionMismatch ErrorCode = "version_mismatch" CodeLimitExceeded ErrorCode = "limit_exceeded" )
type FieldDefinition ¶
type FieldDefinition struct {
Name string
Type ValueType
Default bool
Required bool
Deprecated bool
Cost int
}
FieldDefinition declares one selectable field.
type FilterDefinition ¶
type FilterDefinition struct {
Name string
Type ValueType
Operators []Operator
Protected bool
Deprecated bool
Nullable bool
AllowEmpty bool
Cost int
}
FilterDefinition declares the only operations accepted for one filter.
type FilterExpr ¶
type FilterExpr struct {
Predicate *Predicate `json:"predicate,omitempty"`
Logic Logic `json:"logic,omitempty"`
Children []FilterExpr `json:"children,omitempty"`
}
FilterExpr is either one Predicate or one logical group.
type Operator ¶
type Operator string
Operator is a declared, typed predicate operation.
const ( OpEqual Operator = "eq" OpNotEqual Operator = "neq" OpLess Operator = "lt" OpLessOrEqual Operator = "lte" OpGreater Operator = "gt" OpGreaterOrEqual Operator = "gte" OpIn Operator = "in" OpNotIn Operator = "not_in" OpBetween Operator = "between" OpIsNull Operator = "is_null" OpContains Operator = "contains" OpStartsWith Operator = "starts_with" OpEndsWith Operator = "ends_with" )
type Optional ¶
type Optional[T any] struct { // contains filtered or unexported fields }
Optional distinguishes an absent request component from an explicitly supplied zero or empty value.
type PageRequest ¶
type PageRequest struct {
Mode PageMode `json:"mode"`
Size int `json:"size,omitempty"`
After string `json:"after,omitempty"`
Before string `json:"before,omitempty"`
Offset int `json:"offset,omitempty"`
}
PageRequest describes bounded pagination without decoding a cursor.
type PaginationDefinition ¶
PaginationDefinition explicitly enables bounded page modes.
type Plan ¶
type Plan struct {
// contains filtered or unexported fields
}
Plan is an immutable, reviewed query description. Accessors return copies so adapters cannot mutate a plan after compilation.
func Compile ¶
func Compile(ctx context.Context, schema *Schema, request Request, options CompileOptions) (*Plan, error)
Compile validates and snapshots a request into an immutable plan.
Example ¶
package main
import (
"context"
"fmt"
apiquery "github.com/faustbrian/go-api-query"
)
func main() {
schema, err := apiquery.NewSchema(apiquery.SchemaConfig{
Resource: "orders", Revision: "orders-v1",
Fields: []apiquery.FieldDefinition{
{Name: "id", Type: apiquery.TypeString, Required: true},
{Name: "status", Type: apiquery.TypeString, Default: true},
},
})
if err != nil {
fmt.Println("schema error")
return
}
plan, err := apiquery.Compile(context.Background(), schema, apiquery.Request{},
apiquery.CompileOptions{})
if err != nil {
fmt.Println("compile error")
return
}
fmt.Println(plan.Resource(), plan.SchemaRevision(), plan.ResponseFields(), plan.ExecutionFields())
}
Output: orders orders-v1 [status] [id status]
func (*Plan) Canonical ¶
Canonical returns deterministic JSON suitable for cache keys, signing, and equality tests. It contains no maps and preserves semantically ordered lists.
func (*Plan) Cursor ¶
func (p *Plan) Cursor() *CursorState
Cursor returns a defensive copy of authenticated cursor state, or nil for a first cursor page and non-cursor plans.
func (*Plan) ExecutionFields ¶
ExecutionFields returns response fields plus required server-only fields.
func (*Plan) Filter ¶
func (p *Plan) Filter() *FilterExpr
Filter returns a deep copy of the reviewed filter expression.
func (*Plan) MandatoryConstraints ¶
func (p *Plan) MandatoryConstraints() []Constraint
MandatoryConstraints returns server-owned predicates that persistence adapters must always compose with client filters.
func (*Plan) ResponseFields ¶
ResponseFields returns only fields authorized for response projection.
func (*Plan) SchemaRevision ¶
SchemaRevision reports the exact schema revision used to compile the plan.
type Predicate ¶
type Predicate struct {
Name string `json:"name"`
Operator Operator `json:"operator"`
Values []Value `json:"values"`
}
Predicate is a typed filter leaf.
type RelationshipDefinition ¶
type RelationshipDefinition struct {
Name string
Resource string
Cost int
Relationships []RelationshipDefinition
}
RelationshipDefinition declares one includable edge.
type Request ¶
type Request struct {
SchemaRevision Optional[string]
Fields Optional[[]string]
Includes Optional[[]string]
Filter *FilterExpr
Sorts Optional[[]SortTerm]
Page PageRequest
}
Request is the transport-neutral query input.
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
Schema is an immutable server-declared query capability set.
func NewSchema ¶
func NewSchema(config SchemaConfig) (*Schema, error)
NewSchema validates and defensively copies a declared schema.
type SchemaConfig ¶
type SchemaConfig struct {
Resource string
Revision string
Fields []FieldDefinition
Filters []FilterDefinition
Sorts []SortDefinition
Relationships []RelationshipDefinition
DefaultSort []SortTerm
AllowedLogic []Logic
Pagination PaginationDefinition
Bounds Bounds
}
SchemaConfig is mutable caller input used only while constructing a Schema.
type SortDefinition ¶
SortDefinition declares one sortable field and its ordering role.
type SortTerm ¶
type SortTerm struct {
Name string `json:"name"`
Direction Direction `json:"direction"`
Nulls NullOrder `json:"nulls,omitempty"`
}
SortTerm is one ordered sort component.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is a closed, typed query value. It deliberately cannot hold arbitrary Go values or persistence expressions.
func BytesValue ¶
BytesValue constructs a value from a defensive copy represented as base64.
func FloatValue ¶
FloatValue constructs a finite floating-point value.
func NullValue ¶
func NullValue() Value
NullValue constructs an explicit null cursor position. Null is not a valid declared field type, filter value, or equality constraint.
func (Value) MarshalJSON ¶
MarshalJSON emits a deterministic typed representation.
func (Value) String ¶
String reports the canonical textual value. Protected values should not be placed in diagnostics by callers.
func (*Value) UnmarshalJSON ¶
UnmarshalJSON accepts only the canonical closed representation emitted by MarshalJSON.
type ValueType ¶
type ValueType string
ValueType identifies the wire and comparison semantics of a typed value.
type Violation ¶
type Violation struct {
Code ErrorCode `json:"code"`
Path string `json:"path"`
Message string `json:"message"`
}
Violation is one sanitized failure at a stable request path.
type Violations ¶
type Violations struct {
// contains filtered or unexported fields
}
Violations aggregates a bounded set of query failures.
func (*Violations) Error ¶
func (v *Violations) Error() string
Error returns a stable, sanitized summary.
func (*Violations) Items ¶
func (v *Violations) Items() []Violation
Items returns a defensive copy of the structured failures.
Directories
¶
| Path | Synopsis |
|---|---|
|
adapters
|
|
|
http
Package apiqueryhttp parses the repository's bounded conventional HTTP query representation.
|
Package apiqueryhttp parses the repository's bounded conventional HTTP query representation. |
|
jsonapi
Package apiqueryjsonapi maps authoritative go-jsonapi query values into the transport-neutral request model.
|
Package apiqueryjsonapi maps authoritative go-jsonapi query values into the transport-neutral request model. |
|
jsonrpc
Package apiqueryjsonrpc parses bounded JSON-RPC query parameters and exposes a caller-owned OpenRPC descriptor.
|
Package apiqueryjsonrpc parses bounded JSON-RPC query parameters and exposes a caller-owned OpenRPC descriptor. |
|
postgres
Package apiquerypostgres compiles reviewed plans into allowlisted PostgreSQL statement fragments.
|
Package apiquerypostgres compiles reviewed plans into allowlisted PostgreSQL statement fragments. |
|
validation
Package apiqueryvalidation projects API Query failures into immutable go-validation reports.
|
Package apiqueryvalidation projects API Query failures into immutable go-validation reports. |
|
Package apiqueryhttp strictly parses conventional HTTP query strings into transport-neutral API query requests.
|
Package apiqueryhttp strictly parses conventional HTTP query strings into transport-neutral API query requests. |
|
Package apiqueryjsonapi composes parsed jsonapi queries with apiquery.
|
Package apiqueryjsonapi composes parsed jsonapi queries with apiquery. |
|
Package apiquerypgx translates reviewed plans into bounded PostgreSQL query primitives.
|
Package apiquerypgx translates reviewed plans into bounded PostgreSQL query primitives. |
|
Package apiqueryrpc parses bounded JSON-RPC query parameters and describes their OpenRPC content without compiling or executing a query.
|
Package apiqueryrpc parses bounded JSON-RPC query parameters and describes their OpenRPC content without compiling or executing a query. |
|
Package apiquerytest provides builders, fixtures, assertions, and conformance suites for API query consumers and adapters.
|
Package apiquerytest provides builders, fixtures, assertions, and conformance suites for API query consumers and adapters. |
|
Package apiqueryvalidation projects query failures into validation reports without exposing rejected values or unsafe causes.
|
Package apiqueryvalidation projects query failures into validation reports without exposing rejected values or unsafe causes. |
|
Package cursor encrypts, authenticates, versions, and bounds typed cursor positions without coupling them to a transport or database.
|
Package cursor encrypts, authenticates, versions, and bounds typed cursor positions without coupling them to a transport or database. |
|
internal
|
|
|
strictjson
Package strictjson provides bounded JSON decoding with duplicate and unknown object member rejection.
|
Package strictjson provides bounded JSON decoding with duplicate and unknown object member rejection. |