Documentation
¶
Overview ¶
Package value defines the runtime Value type and its supporting domain-shaped types (Money, Duration, Range, time helpers) used throughout Vibescript. Hosts import this package directly when passing arguments, reading results, building globals, or implementing first-party capability interfaces.
Scope: this package intentionally houses both the runtime-value plumbing (Value, ValueKind, constructors, accessors, kind conversions) AND the domain-shaped scalar types (Money, Duration, Range, time helpers). They live together because each domain type is also a Value payload: NewMoney(m) wraps a Money, KindMoney tags it, and Value.Money() unwraps it. Splitting the domain scalars into a separate vibes/domain package would force value/ to import domain/ purely to define those payload kinds. The Value-payload coupling outweighs the organizational benefit of a standalone domain package, so the scalars stay here.
Index ¶
- Variables
- func NumericToSeconds(val Value) (int64, error)
- func ParseLocation(val Value) (*time.Location, error)
- func ParseLocationString(spec string) (*time.Location, error)
- func ParseTimeString(input, layout string, hasLayout bool, loc *time.Location) (time.Time, error)
- func TimeFromEpoch(val Value, loc *time.Location) (time.Time, error)
- func TimeFromParts(args []Value, defaultLoc *time.Location) (time.Time, error)
- func ValueToInt64(val Value) (int64, error)
- type BlockPayload
- type BuiltinPayload
- type ClassPayload
- type Duration
- type EnumPayload
- type EnumValuePayload
- type FunctionPayload
- type InstancePayload
- type Money
- type Range
- type SliceIdentity
- type Value
- func NewArray(a []Value) Value
- func NewBool(b bool) Value
- func NewDuration(d Duration) Value
- func NewFloat(f float64) Value
- func NewHash(h map[string]Value) Value
- func NewInt(i int64) Value
- func NewMoney(m Money) Value
- func NewNil() Value
- func NewObject(attrs map[string]Value) Value
- func NewRange(r Range) Value
- func NewString(s string) Value
- func NewSymbol(name string) Value
- func NewTime(t time.Time) Value
- func NewValue(kind ValueKind, data any) Value
- func (v Value) Array() []Value
- func (v Value) Block() BlockPayload
- func (v Value) Bool() bool
- func (v Value) Builtin() BuiltinPayload
- func (v Value) Class() ClassPayload
- func (v Value) Data() any
- func (v Value) Duration() Duration
- func (v Value) Enum() EnumPayload
- func (v Value) EnumValue() EnumValuePayload
- func (v Value) Equal(other Value) bool
- func (v Value) Float() float64
- func (v Value) Function() FunctionPayload
- func (v Value) Hash() map[string]Value
- func (v Value) Instance() InstancePayload
- func (v Value) Int() int64
- func (v Value) IsNil() bool
- func (v Value) Kind() ValueKind
- func (v Value) Money() Money
- func (v Value) Range() Range
- func (v Value) String() string
- func (v Value) Time() time.Time
- func (v Value) Truthy() bool
- type ValueKind
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultTimeParseLayouts = []string{ time.RFC3339Nano, time.RFC3339, time.RFC1123Z, time.RFC1123, "2006-01-02T15:04:05", "2006-01-02 15:04:05", "2006/01/02 15:04:05", "2006-01-02", "2006/01/02", "01/02/2006 15:04:05", "01/02/2006", }
DefaultTimeParseLayouts is the ordered list of layouts attempted by ParseTimeString when no explicit layout is supplied.
var RuntimeEqualer func(left, right Value) (bool, bool)
RuntimeEqualer is the hook used by Value.Equal to compare runtime-only kinds whose payload types live in the vibes package. The vibes package installs this hook during initialization. If unset, equality for those kinds falls back to pointer identity of the underlying payload.
var RuntimeStringer func(v Value) (string, bool)
RuntimeStringer is the hook used by Value.String to format runtime-only kinds (function, builtin, block, enum, enum value, class, instance) whose payload types live in the vibes package. The vibes package installs this hook during initialization. If unset, those kinds fall back to a generic rendering of the underlying payload.
Functions ¶
func NumericToSeconds ¶
NumericToSeconds converts an integer or floating-point Value to a count of whole seconds.
func ParseLocation ¶
ParseLocation parses a timezone specifier carried in a Value into a time.Location, returning (nil, nil) when val is nil.
func ParseLocationString ¶
ParseLocationString parses a timezone specifier string (named zone, fixed offset, or empty).
func ParseTimeString ¶
ParseTimeString parses a time string, optionally using a caller-supplied layout. When hasLayout is false the default layouts are tried in order.
func TimeFromEpoch ¶
TimeFromEpoch converts a numeric epoch value into a time.Time anchored to the supplied (or local) location.
func TimeFromParts ¶
TimeFromParts constructs a time.Time from year/month/day positional arguments, with optional hour/minute/second and timezone arguments.
func ValueToInt64 ¶
ValueToInt64 coerces an integer or floating-point Value to int64, returning an error for any other kind.
Types ¶
type BlockPayload ¶
type BlockPayload interface{ ValueBlockMarker() }
BlockPayload is the marker implemented by the runtime block type.
type BuiltinPayload ¶
type BuiltinPayload interface{ ValueBuiltinMarker() }
BuiltinPayload is the marker implemented by the runtime builtin type.
type ClassPayload ¶
type ClassPayload interface{ ValueClassMarker() }
ClassPayload is the marker implemented by the runtime class type so Value.Class can return a typed result without importing the runtime.
type Duration ¶
type Duration struct {
// contains filtered or unexported fields
}
Duration stores an integer number of seconds for now.
func DurationFromParts ¶
DurationFromParts assembles a Duration from week, day, hour, minute, and second components.
func DurationFromSeconds ¶
DurationFromSeconds builds a Duration from a whole-second count.
func ParseDurationString ¶
ParseDurationString parses a duration in Go's time.ParseDuration format or in ISO-8601 form.
func SecondsDuration ¶
SecondsDuration returns a Duration corresponding to the given integer value interpreted in the named time unit (seconds, minutes, hours, days, weeks, and their singular forms).
type EnumPayload ¶
type EnumPayload interface{ ValueEnumMarker() }
EnumPayload is the marker implemented by the runtime enum type.
type EnumValuePayload ¶
type EnumValuePayload interface{ ValueEnumValueMarker() }
EnumValuePayload is the marker implemented by the runtime enum-value type.
type FunctionPayload ¶
type FunctionPayload interface{ ValueFunctionMarker() }
FunctionPayload is the marker implemented by the runtime script-function type.
type InstancePayload ¶
type InstancePayload interface{ ValueInstanceMarker() }
InstancePayload is the marker implemented by the runtime instance type.
type Money ¶
type Money struct {
// contains filtered or unexported fields
}
Money represents an ISO-4217 currency amount stored as integer cents.
func NewMoneyFromCents ¶
NewMoneyFromCents constructs a Money from an integer cents value and a currency code.
func ParseMoneyLiteral ¶
ParseMoneyLiteral parses a textual money literal of the form "X.XX CUR".
func (Money) DivInt ¶
DivInt divides m by the given integer divisor, returning an error on division by zero.
type Range ¶
Range represents an integer range with inclusive start and end. It is a domain-shaped scalar that also serves as a Value payload (KindRange); it lives in the value package alongside Value itself because of that coupling. See doc.go for the rationale.
type SliceIdentity ¶
SliceIdentity captures the identity of a slice header so cycle detection in value graphs can recognize revisits.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is a tagged union holding any Vibescript runtime value.
func NewArray ¶
NewArray returns an array Value.
Example ¶
package main
import (
"fmt"
"github.com/mgomes/vibescript/vibes/value"
)
func main() {
v := value.NewArray([]value.Value{
value.NewInt(1),
value.NewInt(2),
value.NewInt(3),
})
fmt.Println(v.String())
}
Output: [1, 2, 3]
func NewDuration ¶
NewDuration returns a duration Value.
Example ¶
package main
import (
"fmt"
"github.com/mgomes/vibescript/vibes/value"
)
func main() {
v := value.NewDuration(value.DurationFromSeconds(90))
fmt.Println(v.String())
}
Output: 90s
func NewHash ¶
NewHash returns a hash (map) Value.
Example ¶
package main
import (
"fmt"
"github.com/mgomes/vibescript/vibes/value"
)
func main() {
v := value.NewHash(map[string]value.Value{
"name": value.NewString("acme"),
})
fmt.Println(v.String())
}
Output: {name: acme}
func NewInt ¶
NewInt returns an integer Value.
Example ¶
package main
import (
"fmt"
"github.com/mgomes/vibescript/vibes/value"
)
func main() {
v := value.NewInt(42)
fmt.Println(v.String())
}
Output: 42
func NewMoney ¶
NewMoney returns a money Value.
Example ¶
package main
import (
"fmt"
"github.com/mgomes/vibescript/vibes/value"
)
func main() {
amount, err := value.NewMoneyFromCents(1999, "USD")
if err != nil {
panic(err)
}
v := value.NewMoney(amount)
fmt.Println(v.String())
}
Output: 19.99 USD
func NewString ¶
NewString returns a string Value.
Example ¶
package main
import (
"fmt"
"github.com/mgomes/vibescript/vibes/value"
)
func main() {
v := value.NewString("hello")
fmt.Println(v.String())
}
Output: hello
func NewValue ¶
NewValue constructs a Value with the given kind and underlying data. It is intended for use by the vibes package when wrapping runtime payloads (blocks, classes, instances, enums, functions, builtins) whose types live outside this package.
func (Value) Block ¶
func (v Value) Block() BlockPayload
Block returns the underlying block payload of v, or nil if v is not a block. The concrete type is private to the runtime; callers operate through the BlockPayload marker.
func (Value) Builtin ¶
func (v Value) Builtin() BuiltinPayload
Builtin returns the underlying builtin payload of v, or nil if v is not a builtin. The concrete type is private to the runtime; callers operate through the BuiltinPayload marker.
func (Value) Class ¶
func (v Value) Class() ClassPayload
Class returns the underlying class payload of v, or nil if v is not a class. The concrete type is private to the runtime; callers operate through the ClassPayload marker.
func (Value) Data ¶
Data returns the underlying payload stored in v. Callers are expected to type-assert against the payload type associated with v.Kind().
func (Value) Duration ¶
Duration returns the duration content of v, or a zero Duration if v is not a duration.
func (Value) Enum ¶
func (v Value) Enum() EnumPayload
Enum returns the underlying enum definition payload of v, or nil if v is not an enum. The concrete type is private to the runtime; callers operate through the EnumPayload marker.
func (Value) EnumValue ¶
func (v Value) EnumValue() EnumValuePayload
EnumValue returns the underlying enum value payload of v, or nil if v is not an enum value. The concrete type is private to the runtime; callers operate through the EnumValuePayload marker.
func (Value) Equal ¶
Equal reports whether v and other hold the same kind and value.
Example ¶
ExampleValue_Equal contrasts equal and unequal Values across kinds.
package main
import (
"fmt"
"github.com/mgomes/vibescript/vibes/value"
)
func main() {
a := value.NewInt(1)
b := value.NewInt(1)
c := value.NewString("1")
fmt.Println(a.Equal(b))
fmt.Println(a.Equal(c))
}
Output: true false
func (Value) Function ¶
func (v Value) Function() FunctionPayload
Function returns the underlying script-function payload of v, or nil if v is not a function. The concrete type is private to the runtime; callers operate through the FunctionPayload marker.
func (Value) Instance ¶
func (v Value) Instance() InstancePayload
Instance returns the underlying instance payload of v, or nil if v is not an instance. The concrete type is private to the runtime; callers operate through the InstancePayload marker.
func (Value) String ¶
String returns the string representation of v.
Example ¶
package main
import (
"fmt"
"github.com/mgomes/vibescript/vibes/value"
)
func main() {
v := value.NewString("hello")
fmt.Println(v.String())
}
Output: hello