shape

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

repository/shape

repository/shape provides a dynamic, in-memory pipeline for building Datly runtime artifacts from either:

  • Go structs (scan -> plan -> load)
  • DQL (compile -> load)

without generating YAML route/resource files.

Packages

  • shape/scan: discovers view/state tags from struct fields (Embedder-aware).
  • shape/plan: normalizes scan output into a deterministic shape plan.
  • shape/load: materializes view.Resource, view.View, and a runtime-neutral component artifact.
  • shape/compile: compiles DQL into a shape plan for dynamic loading.

Facade API

Use shape.Engine or package helpers:

  • shape.LoadViews(ctx, src, opts...)
  • shape.LoadComponent(ctx, src, opts...)
  • shape.LoadDQLViews(ctx, dql, opts...)
  • shape.LoadDQLComponent(ctx, dql, opts...)

Minimal Struct Flow

engine := shape.New(
    shape.WithScanner(scan.New()),
    shape.WithPlanner(plan.New()),
    shape.WithLoader(load.New()),
    shape.WithName("/v1/api/report"),
)

views, err := engine.LoadViews(ctx, &MyOutput{})

Minimal DQL Flow

engine := shape.New(
    shape.WithCompiler(compile.New()),
    shape.WithLoader(load.New()),
    shape.WithName("/v1/api/report"),
)

component, err := engine.LoadDQLComponent(ctx, "SELECT id FROM ORDERS t")

DQL Directives

shape recognizes three directive forms in DQL:

  • #set(...): contract declarations (legacy-compatible).
  • #define(...): contract declarations (alias of #set(...) for clearer intent).
  • #settings(...) / #setting(...): runtime/settings directives.

Runtime/settings directives currently support:

  • #settings($_ = $package('module/path'))
  • #settings($_ = $import('alias', 'github.com/acme/pkg'))
  • #settings($_ = $meta('docs/path.md'))
  • #settings($_ = $cache(true, '5m'))
  • #settings($_ = $mcp('tool.name', 'description', 'docs/mcp/tool.md'))
  • #settings($_ = $connector('analytics')) (default connector for views that do not already declare one)

Column Discovery Policy

Shape compile now exposes column discovery policy for DQL->IR:

  • auto (default): require discovery for SELECT * and for views without concrete declared shape.
  • on: always mark query views for discovery.
  • off: disable discovery; compile fails when discovery is required.

Use shape.WithColumnDiscoveryModeDefault(...) on engine defaults or shape.WithColumnDiscoveryMode(...) as compile option.

Repository Integration

repository/components.go can optionally merge views generated by the shape pipeline during init.

Enable via:

repository.WithShapePipeline(true)

Default is disabled to preserve existing behavior.

Documentation

Overview

Package shape provides building blocks for dynamic repository loading from struct and DQL sources without requiring persisted YAML artifacts.

Index

Constants

View Source
const (
	CompileMixedModeExecWins     CompileMixedMode = "exec_wins"
	CompileMixedModeReadWins     CompileMixedMode = "read_wins"
	CompileMixedModeErrorOnMixed CompileMixedMode = "error_on_mixed"

	CompileUnknownNonReadWarn  CompileUnknownNonReadMode = "warn"
	CompileUnknownNonReadError CompileUnknownNonReadMode = "error"

	CompileProfileCompat CompileProfile = "compat"
	CompileProfileStrict CompileProfile = "strict"

	CompileColumnDiscoveryAuto CompileColumnDiscoveryMode = "auto"
	CompileColumnDiscoveryOn   CompileColumnDiscoveryMode = "on"
	CompileColumnDiscoveryOff  CompileColumnDiscoveryMode = "off"
)

Variables

View Source
var (
	ErrNilSource             = errors.New("shape: source was nil")
	ErrNilDQL                = errors.New("shape: dql was empty")
	ErrScannerNotConfigured  = errors.New("shape: scanner was not configured")
	ErrPlannerNotConfigured  = errors.New("shape: planner was not configured")
	ErrLoaderNotConfigured   = errors.New("shape: loader was not configured")
	ErrCompilerNotConfigured = errors.New("shape: compiler was not configured")
)

Functions

This section is empty.

Types

type CompileColumnDiscoveryMode

type CompileColumnDiscoveryMode string

type CompileMixedMode

type CompileMixedMode string

type CompileOption

type CompileOption func(*CompileOptions)

func WithColumnDiscoveryMode

func WithColumnDiscoveryMode(mode CompileColumnDiscoveryMode) CompileOption

func WithCompileProfile

func WithCompileProfile(profile CompileProfile) CompileOption

func WithCompileStrict

func WithCompileStrict(strict bool) CompileOption

func WithDQLPathMarker

func WithDQLPathMarker(marker string) CompileOption

WithDQLPathMarker overrides the path marker used to locate platform root from source path. Default is "/dql/".

func WithInferTypeContextDefaults

func WithInferTypeContextDefaults(enabled bool) CompileOption

WithInferTypeContextDefaults enables/disables source-path based type context defaults.

func WithMixedMode

func WithMixedMode(mode CompileMixedMode) CompileOption

func WithRoutesRelativePath

func WithRoutesRelativePath(path string) CompileOption

WithRoutesRelativePath overrides routes path relative to detected platform root. Default is "repo/dev/Datly/routes".

func WithTypeContextPackageDefaults

func WithTypeContextPackageDefaults(dir, name, path string) CompileOption

WithTypeContextPackageDefaults sets package dir/name/path in one call.

func WithTypeContextPackageDir

func WithTypeContextPackageDir(dir string) CompileOption

WithTypeContextPackageDir sets default type-context package directory (for xgen parity).

func WithTypeContextPackageName

func WithTypeContextPackageName(name string) CompileOption

WithTypeContextPackageName sets default type-context package name (for xgen parity).

func WithTypeContextPackagePath

func WithTypeContextPackagePath(path string) CompileOption

WithTypeContextPackagePath sets default type-context package import path (for xgen parity).

func WithUnknownNonReadMode

func WithUnknownNonReadMode(mode CompileUnknownNonReadMode) CompileOption

type CompileOptions

type CompileOptions struct {
	Strict              bool
	Profile             CompileProfile
	MixedMode           CompileMixedMode
	UnknownNonReadMode  CompileUnknownNonReadMode
	ColumnDiscoveryMode CompileColumnDiscoveryMode
	DQLPathMarker       string
	RoutesRelativePath  string
	TypePackageDir      string
	TypePackageName     string
	TypePackagePath     string
	InferTypeContext    *bool
}

type CompileProfile

type CompileProfile string

type CompileUnknownNonReadMode

type CompileUnknownNonReadMode string

type ComponentArtifact

type ComponentArtifact struct {
	Resource  *view.Resource
	Component any
}

ComponentArtifact is the runtime component payload produced by Loader. Component stays untyped in the skeleton to avoid coupling shape package to repository internals before the implementation phase.

func LoadComponent

func LoadComponent(ctx context.Context, src any, opts ...Option) (*ComponentArtifact, error)

LoadComponent is a package-level helper for struct source component loading.

func LoadDQLComponent

func LoadDQLComponent(ctx context.Context, dql string, opts ...Option) (*ComponentArtifact, error)

LoadDQLComponent is a package-level helper for DQL source component loading.

type DQLCompiler

type DQLCompiler interface {
	Compile(ctx context.Context, source *Source, opts ...CompileOption) (*PlanResult, error)
}

DQLCompiler compiles DQL source directly into a shape plan.

type Engine

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

Engine is a thin facade over scan -> plan -> load pipeline.

func New

func New(opts ...Option) *Engine

New creates an Engine facade.

func (*Engine) LoadComponent

func (e *Engine) LoadComponent(ctx context.Context, src any) (*ComponentArtifact, error)

LoadComponent executes scan -> plan -> load for struct source.

func (*Engine) LoadDQLComponent

func (e *Engine) LoadDQLComponent(ctx context.Context, dql string) (*ComponentArtifact, error)

LoadDQLComponent executes compile -> load for DQL source.

func (*Engine) LoadDQLViews

func (e *Engine) LoadDQLViews(ctx context.Context, dql string) (*ViewArtifacts, error)

LoadDQLViews executes compile -> load for DQL source.

func (*Engine) LoadViews

func (e *Engine) LoadViews(ctx context.Context, src any) (*ViewArtifacts, error)

LoadViews executes scan -> plan -> load for struct source.

type LoadOption

type LoadOption func(*LoadOptions)

type LoadOptions

type LoadOptions struct{}

type Loader

type Loader interface {
	LoadViews(ctx context.Context, plan *PlanResult, opts ...LoadOption) (*ViewArtifacts, error)
	LoadComponent(ctx context.Context, plan *PlanResult, opts ...LoadOption) (*ComponentArtifact, error)
}

Loader materializes runtime artifacts from normalized plan.

type Mode

type Mode string

Mode controls which execution flow is expected from the shape pipeline.

const (
	ModeUnspecified Mode = ""
	ModeStruct      Mode = "struct"
	ModeDQL         Mode = "dql"
)

type Option

type Option func(*Options)

Option mutates Options.

func WithColumnDiscoveryModeDefault

func WithColumnDiscoveryModeDefault(mode CompileColumnDiscoveryMode) Option

WithColumnDiscoveryModeDefault sets default column discovery policy used by Engine DQL compile path.

func WithCompileProfileDefault

func WithCompileProfileDefault(profile CompileProfile) Option

WithCompileProfileDefault sets default compiler profile used by Engine DQL compile path.

func WithCompiler

func WithCompiler(compiler DQLCompiler) Option

func WithLegacyTranslatorDefaults

func WithLegacyTranslatorDefaults() Option

WithLegacyTranslatorDefaults configures Engine compile defaults to legacy-compatible behavior.

func WithLoader

func WithLoader(loader Loader) Option

func WithMixedModeDefault

func WithMixedModeDefault(mode CompileMixedMode) Option

WithMixedModeDefault sets default compiler mixed read/exec mode used by Engine DQL compile path.

func WithMode

func WithMode(mode Mode) Option

func WithName

func WithName(name string) Option

func WithPlanner

func WithPlanner(planner Planner) Option

func WithRuntime

func WithRuntime(runtime RuntimeRegistrar) Option

func WithScanner

func WithScanner(scanner Scanner) Option

func WithStrict

func WithStrict(strict bool) Option

func WithUnknownNonReadModeDefault

func WithUnknownNonReadModeDefault(mode CompileUnknownNonReadMode) Option

WithUnknownNonReadModeDefault sets default unknown non-read mode used by Engine DQL compile path.

type Options

type Options struct {
	Mode                Mode
	Strict              bool
	Name                string
	Scanner             Scanner
	Planner             Planner
	Loader              Loader
	Compiler            DQLCompiler
	Runtime             RuntimeRegistrar
	CompileProfile      CompileProfile
	CompileMixedMode    CompileMixedMode
	UnknownNonReadMode  CompileUnknownNonReadMode
	ColumnDiscoveryMode CompileColumnDiscoveryMode
}

Options stores shape facade dependencies and behavior flags.

func NewOptions

func NewOptions(opts ...Option) *Options

NewOptions builds Options from varargs.

type PlanOption

type PlanOption func(*PlanOptions)

type PlanOptions

type PlanOptions struct{}

type PlanResult

type PlanResult struct {
	Source *Source
	Plan   any
}

PlanResult is the output produced by Planner.

type Planner

type Planner interface {
	Plan(ctx context.Context, scan *ScanResult, opts ...PlanOption) (*PlanResult, error)
}

Planner normalizes discovered descriptors into execution plan.

type RuntimeRegistrar

type RuntimeRegistrar interface {
	RegisterViews(ctx context.Context, artifacts *ViewArtifacts) error
	RegisterComponent(ctx context.Context, artifacts *ComponentArtifact) error
}

RuntimeRegistrar optionally registers loaded artifacts in runtime services.

type ScanOption

type ScanOption func(*ScanOptions)

type ScanOptions

type ScanOptions struct{}

type ScanResult

type ScanResult struct {
	Source      *Source
	Descriptors any
}

ScanResult is the output produced by Scanner.

type Scanner

type Scanner interface {
	Scan(ctx context.Context, source *Source, opts ...ScanOption) (*ScanResult, error)
}

Scanner discovers shape descriptors from Source.

type Source

type Source struct {
	Name         string
	Path         string
	Connector    string
	Struct       any
	Type         reflect.Type
	TypeName     string
	TypeRegistry *x.Registry
	DQL          string
}

Source represents the caller-provided shape source.

func (*Source) EnsureTypeRegistry

func (s *Source) EnsureTypeRegistry() *x.Registry

EnsureTypeRegistry returns source registry ensuring root type is registered when available.

func (*Source) ResolveRootType

func (s *Source) ResolveRootType() (reflect.Type, error)

ResolveRootType resolves source root type from explicit Type, Struct, or viant/x registry.

type ViewArtifacts

type ViewArtifacts struct {
	Resource *view.Resource
	Views    view.Views
}

ViewArtifacts is the runtime view payload produced by Loader.

func LoadDQLViews

func LoadDQLViews(ctx context.Context, dql string, opts ...Option) (*ViewArtifacts, error)

LoadDQLViews is a package-level helper for DQL source view loading.

func LoadViews

func LoadViews(ctx context.Context, src any, opts ...Option) (*ViewArtifacts, error)

LoadViews is a package-level helper for struct source view loading.

Directories

Path Synopsis
Package compile provides DQL-to-shape compilation.
Package compile provides DQL-to-shape compilation.
dml
dql
Package load defines materialization responsibilities for runtime artifacts.
Package load defines materialization responsibilities for runtime artifacts.
Package plan defines normalization and shape-planning responsibilities.
Package plan defines normalization and shape-planning responsibilities.
Package scan defines scanning responsibilities for struct/DQL inputs.
Package scan defines scanning responsibilities for struct/DQL inputs.

Jump to

Keyboard shortcuts

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