output

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: Apache-2.0 Imports: 24 Imported by: 1

README

Output encoding

Output contracts are compiled at bootstrap and published with the registered component. A declared FormatSelector() input chooses the request source: header/Accept negotiates media types, or a query source such as query/_format accepts format names. Without a declaration the legacy _format query source remains available. An absent value uses the route marshaller, then the component format (JSON by default). A handler-provided response retains control of its status, headers, and body. Execution and encoding failures use JSON errors.

Supported formats are json, csv, xml, tabular, and xls/xlsx. XLS responses contain an XLSX workbook. Native SQLX, Structology, XMLify, and XLSy encoders retain typed values; tabular JSON preserves the surrounding output envelope and transforms its data slot. Unsupported Accept values return 406; invalid query format names return 400. Formats are checked before route execution, so a JSON-only custom output cannot be used to reveal internal fields through a different encoder after a mutation.

CSV and tabular row authority includes a direct view declared with cardinality one, or an explicitly named struct/pointer data slot (DataField or an output/view / output/body parameter). A direct singleton's child collections remain relations within that row. Invocation field selection retains this row authority. Singleton and array carriers become typed slices only for these two codecs; JSON, XML, and XLS retain the structural output shape.

A nil singleton represents zero rows: CSV emits its field header and tabular emits [], inside the existing envelope when one is present. A nil envelope retains the existing empty CSV / null tabular behavior. CSV wire contracts remain textual text/csv downloads; tabular still requires an explicit wire schema projection for documentation.

Go component settings use the shared settings tags, for example:

format:"csv" output:"{\"exclude\":[\"Rows.Secret\"],\"omitEmpty\":true,\"title\":\"Records\"}"

The same policy can be authored in DQL:

#setting($_ = $format('csv'))
#setting($_ = $output_exclude('Rows.Secret'))
#setting($_ = $output_omit_empty(true))
#setting($_ = $output_title('Records'))

Exclusions resolve to canonical Go-field paths at registration. Relative paths first resolve against the main data rows. Unambiguous JSON aliases are accepted at every nesting level; unknown or ambiguous paths fail registration. Export formats receive typed copies with the excluded fields removed, leaving the application's output unchanged. omitEmpty applies to JSON output, including a tabular envelope. File formats use the title as an attachment filename.

caseFormat and dateFormat configure JSON and tabular JSON. Other formats retain their native field-format tags. jsonMarshal resolves an authored Go type through bootstrap's type authority. It must implement Marshal(any) ([]byte, error); as in original Datly, a custom JSON marshaller owns the complete JSON representation instead of the default formatting policy.

CSV and XML codec metadata is reused safely by a registered plan. Each XLSX workbook keeps its own mutable native encoder session. Field/type discovery and serialization-shape construction use viant/x/shape.

Documentation

Overview

Package output compiles component output encoding independently of transport and delegates wire formats to their native codec owners.

Index

Constants

This section is empty.

Variables

View Source
var ErrSchemaUnavailable = errors.New("output JSON schema unavailable")

ErrSchemaUnavailable means the encoder cannot be described by a static schema. Consumers may omit optional discovery metadata, but must not substitute Go names.

Functions

func ContentType

func ContentType(format string) (string, error)

Types

type CompileInput

type CompileInput struct {
	Component *spec.Component
	Type      reflect.Type
	DataField string
}

type Compiler

type Compiler struct {
	Lookup func(string) (reflect.Type, error)
}

func (Compiler) Compile

func (c Compiler) Compile(input CompileInput) (*Plan, error)

type JSONWireType added in v1.1.0

type JSONWireType interface {
	JSONWireType() reflect.Type
}

JSONWireType is an optional declaration for custom JSON marshalers whose successful wire value has a stable, separately typed shape. OpenAPI uses the declared type instead of treating the marshaler as arbitrary JSON.

type Marshaller

type Marshaller interface{ Marshal(any) ([]byte, error) }

type Plan

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

Plan contains registered output authority, never invocation input or rows.

func (*Plan) DefaultFormat

func (p *Plan) DefaultFormat() string

func (*Plan) Encode

func (p *Plan) Encode(ctx context.Context, format string, value any) (Result, error)

func (*Plan) FormatSelector added in v1.1.0

func (p *Plan) FormatSelector() (spec.BindSource, bool)

FormatSelector returns the single declared HTTP source for request-selected output formats. Without one, the legacy _format query source remains active.

func (*Plan) JSONOnly added in v1.1.0

func (p *Plan) JSONOnly() bool

func (*Plan) JSONSchema added in v1.1.0

func (p *Plan) JSONSchema() (map[string]any, error)

JSONSchema describes JSON output using the compiled presentation settings. Unlike Wire, this discovery path permits dynamic leaves (including Status.Error). Field naming is kept here, beside the encoder, rather than in MCP input binding.

func (*Plan) TransportReady

func (p *Plan) TransportReady() bool

TransportReady reports the declared SDK response contract, independently of any invocation result. Documentation may supply static HTTP response schemas.

func (*Plan) Type

func (p *Plan) Type() reflect.Type

func (*Plan) Wire

func (p *Plan) Wire(format string) (WireContract, error)

Wire derives representability from the actual compiled encoding plan. Source settings may be changed or absent after compilation and are not authority. Transformed JSON uses the native serializer plan. XML/tabular and opaque encoders still require an explicit wire projection.

type Result

type Result struct {
	Data               []byte
	ContentType        string
	ContentDisposition string
	Filename           string
}

type WireContract

type WireContract struct {
	JSON               *jsonmarshal.WireShape
	ContentDisposition string
	Type               reflect.Type
	ContentType        string
	Binary             bool
}

WireContract describes a statically representable encoded result. Type is the wire value type, which may differ from the component's structural output type. Binary marks an opaque byte stream rather than a textual string.

Jump to

Keyboard shortcuts

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