Documentation
¶
Overview ¶
Package drytypes is a pure-Go (CGO-free) MRI-faithful reimplementation of the Ruby dry-types gem: a composable type system with coercion, constraints, and combinators. Every type is a Type — a value you build from a constructor (Strict, Coercible, Params, JSON, Nominal, …) and refine with combinator methods (Optional, Default, Constrained, Enum, Or, Constructor, …). Applying a type to an input coerces and validates it, returning the coerced value or an error whose message is byte-identical to the dry-types gem's.
Ruby value model ¶
Ruby values are represented by the same small, fixed set of Go types the go-ruby-* ecosystem uses, so a host (go-embedded-ruby / rbgo) maps its object graph to and from this package with no glue:
Ruby Go ---- -- nil nil true / false bool Integer int64, *big.Int (int/int32 accepted on input) Float float64 String string Symbol Symbol Array []any Hash *Map (ordered), map[string]any / map[Symbol]any (input) Date Date Time / DateTime Time
A host callable (a default block or a constructor function) is a Go closure of type [func(any) any] or Callable.
Index ¶
- Variables
- func ArrayOf(elem Type) *baseType
- func Valid(t Type, input any) bool
- type Callable
- type CoercionError
- type Constraint
- type ConstraintError
- type Date
- type HashSchema
- func (s *HashSchema) AsType() Type
- func (b HashSchema) Call(input any) (any, error)
- func (b HashSchema) Constrained(cs ...Constraint) Type
- func (b HashSchema) Constructor(fn Callable) Type
- func (b HashSchema) Default(val any) Type
- func (b HashSchema) DefaultFn(fn func() any) Type
- func (b HashSchema) Enum(values ...any) Type
- func (b HashSchema) GetMeta() map[string]any
- func (b HashSchema) Meta(m map[string]any) Type
- func (b HashSchema) Optional() Type
- func (b HashSchema) Or(other Type) Type
- func (s *HashSchema) Strict() *HashSchema
- type Map
- type MissingKeyError
- type Pair
- type Result
- type SchemaError
- type SchemaKey
- type Symbol
- type Time
- type Type
- func CoercibleFloat() Type
- func CoercibleInteger() Type
- func CoercibleString() Type
- func CoercibleSymbol() Type
- func JSONDate() Type
- func JSONDateTime() Type
- func JSONNil() Type
- func JSONSymbol() Type
- func JSONTime() Type
- func NominalArray() Type
- func NominalBool() Type
- func NominalFloat() Type
- func NominalHash() Type
- func NominalInteger() Type
- func NominalString() Type
- func NominalSymbol() Type
- func ParamsBool() Type
- func ParamsDate() Type
- func ParamsDateTime() Type
- func ParamsFloat() Type
- func ParamsInteger() Type
- func ParamsNil() Type
- func ParamsSymbol() Type
- func ParamsTime() Type
- func StrictArray() Type
- func StrictBool() Type
- func StrictDate() Type
- func StrictDateTime() Type
- func StrictFloat() Type
- func StrictHash() Type
- func StrictInteger() Type
- func StrictNil() Type
- func StrictString() Type
- func StrictSymbol() Type
- func StrictTime() Type
- type UnknownKeysError
Constants ¶
This section is empty.
Variables ¶
var Undefined = undefined{}
Undefined is dry-types' sentinel for "no value given" — it triggers a Type.Default's fallback. Pass it as the input to Call to request the default. (In the gem this is Dry::Types::Undefined.)
Functions ¶
Types ¶
type CoercionError ¶
type CoercionError struct{ Message string }
CoercionError is raised when a coercion fails (e.g. Integer("abc")). Its message is byte-identical to Dry::Types::CoercionError#message.
func (*CoercionError) Error ¶
func (e *CoercionError) Error() string
type Constraint ¶
Constraint is one dry-logic predicate applied by [baseType.Constrained]. The map key is the predicate name (gt, gteq, lt, lteq, format, size, min_size, max_size, included_in, filled, …) and the value is its argument.
type ConstraintError ¶
type ConstraintError struct {
Message string
// Input is the offending value.
Input any
// Rule is the failed predicate rendered as dry-logic renders it
// (e.g. `gt?(18, 10) failed`).
Rule string
}
ConstraintError is raised when a value violates a constraint (including the implicit type? constraint of a strict type). Its message matches Dry::Types::ConstraintError#message: `<input> violates constraints (<rule>)`.
func (*ConstraintError) Error ¶
func (e *ConstraintError) Error() string
type Date ¶
Date is a Ruby Date (a calendar day with no time-of-day). It is the coercion target of the *::Date types. Stored as year/month/day plus the parsed underlying time for formatting.
type HashSchema ¶
type HashSchema struct {
// contains filtered or unexported fields
}
HashSchema is a struct-hash type (dry-types' `Hash.schema({...})`). It embeds [*baseType], so it *is* a Type: it composes with every combinator and with ArrayOf. Build one with NewSchema and refine with HashSchema.Strict.
func NewSchema ¶
func NewSchema(keys ...SchemaKey) *HashSchema
NewSchema builds a Hash.schema type from its members.
func (*HashSchema) AsType ¶
func (s *HashSchema) AsType() Type
AsType exposes the schema as a plain Type (identity — a *HashSchema already is a Type; kept for readability at call sites).
func (HashSchema) Constrained ¶
func (b HashSchema) Constrained(cs ...Constraint) Type
Constrained returns a type that applies each predicate after the base coercion/validation (dry-types' `.constrained(...)`). Predicates run in the order given; the first failure reports the gem's `<pred>?(<arg>, <val>) failed` (or the arity-1 `<pred>?(<val>) failed`) constraint message.
func (HashSchema) Constructor ¶
Constructor returns a type that first runs fn over the input, then applies the base type to fn's result (dry-types' `.constructor(fn)`).
func (HashSchema) Default ¶
Default returns a type that substitutes val when the input is Undefined (dry-types' `.default(val)`). The substituted value is returned as-is (the gem does not re-run coercion on a static default).
func (HashSchema) DefaultFn ¶
DefaultFn returns a type whose default is produced by calling fn (dry-types' `.default { ... }` block form).
func (HashSchema) Enum ¶
Enum returns a type that additionally requires the (coerced) value to be one of values (dry-types' `.enum(...)`), reporting the `included_in?` constraint on a miss.
func (HashSchema) Meta ¶
Meta returns a copy of the type carrying the merged metadata (dry-types' `.meta(...)`).
func (HashSchema) Optional ¶
func (b HashSchema) Optional() Type
Optional returns a type that also accepts nil (dry-types' `.optional`, i.e. `Nil | self`). A nil input yields nil; any other input goes through the base.
func (HashSchema) Or ¶
Or returns the sum type `self | other` (dry-types' `A | B`): it tries the base first, then other; on total failure it reports other's error (matching the gem, whose sum surfaces the right-hand branch's message).
func (*HashSchema) Strict ¶
func (s *HashSchema) Strict() *HashSchema
Strict returns a copy of the schema that rejects unexpected keys (dry-types' `.strict`), raising *UnknownKeysError.
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is an insertion-ordered Ruby Hash. Schema coercion yields a *Map so key order round-trips.
type MissingKeyError ¶
type MissingKeyError struct{ Message string }
MissingKeyError is raised by a strict schema when a required key is absent: `:key is missing in Hash input`.
func (*MissingKeyError) Error ¶
func (e *MissingKeyError) Error() string
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result is the outcome of Try: a success carrying the coerced value or a failure carrying the error (mirrors Dry::Types::Result::Success/Failure).
func Try ¶
Try applies t and returns a Result (dry-types' `type.try`): Success carries the coerced value, Failure carries the error.
type SchemaError ¶
type SchemaError struct{ Message string }
SchemaError is raised when a schema member fails coercion/validation: `<value> (<Class>) has invalid type for :key <inner rule>`.
func (*SchemaError) Error ¶
func (e *SchemaError) Error() string
type SchemaKey ¶
SchemaKey is one member of a HashSchema: a key, its type, and whether the key is optional (dry-types' trailing `?` on the key name).
type Time ¶
Time is a Ruby Time / DateTime. It wraps a Go time.Time; the *::DateTime types coerce into the same shape (dry-types coerces both via Date._parse-style parsing and yields a Time-like object the host distinguishes by its own class mapping).
type Type ¶
type Type interface {
// Call coerces and validates input, returning the coerced value or an error.
Call(input any) (any, error)
// Optional returns a type that also accepts nil (dry-types' `.optional`).
Optional() Type
// Default substitutes val when the input is [Undefined] (`.default(val)`).
Default(val any) Type
// DefaultFn substitutes fn() when the input is [Undefined] (`.default { }`).
DefaultFn(fn func() any) Type
// Constructor pre-processes input through fn, then applies the base type.
Constructor(fn Callable) Type
// Meta returns a copy carrying the merged metadata (`.meta(...)`).
Meta(m map[string]any) Type
// GetMeta returns the attached metadata.
GetMeta() map[string]any
// Or returns the sum type `self | other` (`A | B`).
Or(other Type) Type
// Enum requires the coerced value to be one of values (`.enum(...)`).
Enum(values ...any) Type
// Constrained applies dry-logic predicates after coercion (`.constrained`).
Constrained(cs ...Constraint) Type
// contains filtered or unexported methods
}
Type is a composable dry-types type: it coerces-and-validates an input, and carries the combinator methods so types chain fluently (e.g. `StrictInteger().Constrained(...).Optional()`).
Call applies the type: it returns the coerced value on success or a *CoercionError / *ConstraintError / schema error on failure — the same value and message the dry-types gem's `type[input]` produces.
The interface is closed to this package (every Type is a *baseType) via the unexported node method, which keeps combinator composition total.
func CoercibleFloat ¶
func CoercibleFloat() Type
CoercibleFloat is Types::Coercible::Float (Kernel#Float).
func CoercibleInteger ¶
func CoercibleInteger() Type
CoercibleInteger is Types::Coercible::Integer (Kernel#Integer).
func CoercibleString ¶
func CoercibleString() Type
CoercibleString is Types::Coercible::String (#to_s).
func CoercibleSymbol ¶
func CoercibleSymbol() Type
CoercibleSymbol is Types::Coercible::Symbol (#to_sym).
func JSONNil ¶
func JSONNil() Type
JSONNil is Types::JSON::Nil (passes nil through, else type-checks nil).
func NominalInteger ¶
func NominalInteger() Type
NominalInteger is Types::Nominal::Integer (no constraint).
func NominalString ¶
func NominalString() Type
NominalString is Types::Nominal::String (no constraint).
func NominalSymbol ¶
func NominalSymbol() Type
NominalSymbol is Types::Nominal::Symbol (no constraint).
type UnknownKeysError ¶
type UnknownKeysError struct{ Message string }
UnknownKeysError is raised by a strict schema on unexpected keys: `unexpected keys [:a, :b] in Hash input`.
func (*UnknownKeysError) Error ¶
func (e *UnknownKeysError) Error() string
