Documentation
¶
Overview ¶
Package value distinguishes absent fields, explicit nulls, and present values. Optional[Nullable[T]] represents all three states without losing T's type.
Index ¶
- Constants
- func CheckJSONKeys[K comparable](ctx context.Context, keys []string, limits JSONKeyLimits) error
- func EncodeJSON[T any](ctx context.Context, input T, limits JSONEncodingLimits) ([]byte, error)
- func IsNullableType[T any]() bool
- func IsOptionalType[T any]() bool
- func SupportsJSONKeys[K comparable]() bool
- type JSON
- type JSONEncodingLimits
- type JSONKeyLimits
- type List
- type Nullable
- type Optional
Constants ¶
Variables ¶
This section is empty.
Functions ¶
func CheckJSONKeys ¶
func CheckJSONKeys[K comparable](ctx context.Context, keys []string, limits JSONKeyLimits) error
CheckJSONKeys validates object names before decoding their containing value. It preserves K, rejects noncanonical aliases and duplicate decoded identities, and shares native codec rules with typed storage snapshots. Callers keep the names unchanged until return. Codecs must be deterministic, bounded and must not retain borrowed input. Cancellation waits for active codec work to finish.
func EncodeJSON ¶
EncodeJSON encodes an owned, bounded JSON document with ordinary Go field/tag semantics. It preserves transport string NULs and exact json.Number values. Optional/Nullable values share the same input inspection as typed snapshots. It returns no bytes on failure. It does not infer a DTO schema; contract.JSON adds the generated schema checks before a transport can publish these bytes. Panic and Goexit in codecs become internal faults. Cancellation never abandons a codec that is still running or retaining resources.
func IsNullableType ¶
IsNullableType reports whether T carries Foundry's nullable-value contract. It inspects type capability without invoking methods or reading a value.
func IsOptionalType ¶
IsOptionalType reports whether T carries Foundry's optional-value contract. It checks type capability without invoking methods or reading a value.
func SupportsJSONKeys ¶
func SupportsJSONKeys[K comparable]() bool
SupportsJSONKeys checks whether K supports the framework's native map codecs. It invokes no value methods. A comparable type alone does not imply JSON object-key support; Go's JSON/text method rules still apply.
Types ¶
type JSON ¶
type JSON[T any] struct { // contains filtered or unexported fields }
JSON stores an immutable, typed JSON snapshot. Decode returns fresh values, including fresh maps/slices. Canonical equality matches JSONB value equality; object key order and numeric spelling are not retained. The zero value is invalid, not JSON null or SQL NULL. Use JSON[Nullable[T]] for JSON null and Nullable[JSON[T]] for a nullable SQL column.
func NewJSON ¶
NewJSON snapshots a concrete Go value through its JSON representation. Custom JSON/text methods own their validation and must be deterministic and safe for concurrent use, preserve inputs and return owned decoded values. Construction retains only canonical text, never the input's maps or slices.
func ParseJSON ¶
ParseJSON validates JSON syntax, resource bounds and its concrete Go shape. Unknown/duplicate keys, missing required struct fields, invalid Unicode and nulls in non-nullable fields fail. Optional, omitempty and omitzero fields may be absent. Custom unmarshalers own the schema inside their representation.
func (JSON[T]) IsJSONNull ¶
IsJSONNull reports whether the stored JSON text is the null literal.
func (JSON[T]) MarshalJSON ¶
MarshalJSON returns the stored JSON text as bytes, propagating Text errors.
func (*JSON[T]) UnmarshalJSON ¶
UnmarshalJSON parses a bounded JSON document, replacing the receiver only on success.
type JSONEncodingLimits ¶
JSONEncodingLimits bounds an encoded document and the native input inspection. Depth and Nodes describe JSON, with the root at depth zero. Steps separately bounds native values, indirections and declared fields visited before encoding. Bytes also bounds inspected native string/byte content, before codec execution. All limits except Depth must be positive. Depth may not exceed JSONMaxDepth. Custom codecs own their internal work and allocation; their emitted document still passes the output bounds. Callers must keep input unchanged until return.
func (JSONEncodingLimits) Validate ¶
func (l JSONEncodingLimits) Validate() error
Validate checks positive encoding budgets and the supported depth range.
type JSONKeyLimits ¶
type JSONKeyLimits struct{ Bytes, Keys int }
JSONKeyLimits bounds names supplied for one JSON object. Bytes counts decoded name bytes; Keys bounds entries. Both limits are positive. Formatting probes additionally allow only the bounded escaping overhead of a canonical name.
func (JSONKeyLimits) Validate ¶
func (l JSONKeyLimits) Validate() error
Validate checks that byte and key budgets are positive.
type List ¶
type List[T any] []T
List is a slice whose JSON form is never null: a nil or empty List encodes as [], and decoding null is rejected. Generated DTO schemas, OpenAPI and TypeScript describe it as a non-nullable array, so clients iterate it without null checks. It is an ordinary slice otherwise (len, range, append, index). Use a plain slice when null is a meaningful value.
func (List[T]) MarshalJSON ¶
MarshalJSON encodes a nil List as an empty array.
func (List[T]) MarshalJSONTo ¶
MarshalJSONTo is the encoding/json/v2 form used by Foundry's encoder. It keeps the caller's options, such as rejecting invalid UTF-8, for elements.
func (*List[T]) UnmarshalJSON ¶
UnmarshalJSON rejects null and decodes elements with exact numbers.
type Nullable ¶
type Nullable[T any] struct { // contains filtered or unexported fields }
Nullable represents database/transport null or a present T. It does not represent omission. The zero value is null; Of includes T's zero value.
func (Nullable[T]) MarshalJSON ¶
MarshalJSON encodes null or the present value as JSON.
func (*Nullable[T]) UnmarshalJSON ¶
UnmarshalJSON decodes null or a present JSON value, replacing the receiver only on success.
type Optional ¶
type Optional[T any] struct { // contains filtered or unexported fields }
Optional represents an omitted field or a present T, including T's zero value. Its zero value is omitted. Use json:",omitzero" on enclosing DTO fields; an omitted Optional cannot serialize as a standalone JSON value.
func (Optional[T]) MarshalJSON ¶
MarshalJSON encodes a present value; an omitted value returns a missing fault and requires omitzero at its containing field.
func (*Optional[T]) UnmarshalJSON ¶
UnmarshalJSON decodes a present value, rejecting null unless T supports null or is a JSON document. The receiver changes only on success.