value

package
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: MIT Imports: 8 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

View Source
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.

View Source
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.

View Source
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

func NumericToSeconds(val Value) (int64, error)

NumericToSeconds converts an integer or floating-point Value to a count of whole seconds.

func ParseLocation

func ParseLocation(val Value) (*time.Location, error)

ParseLocation parses a timezone specifier carried in a Value into a time.Location, returning (nil, nil) when val is nil.

func ParseLocationString

func ParseLocationString(spec string) (*time.Location, error)

ParseLocationString parses a timezone specifier string (named zone, fixed offset, or empty).

func ParseTimeString

func ParseTimeString(input, layout string, hasLayout bool, loc *time.Location) (time.Time, error)

ParseTimeString parses a time string, optionally using a caller-supplied layout. When hasLayout is false the default layouts are tried in order.

func TimeFromEpoch

func TimeFromEpoch(val Value, loc *time.Location) (time.Time, error)

TimeFromEpoch converts a numeric epoch value into a time.Time anchored to the supplied (or local) location.

func TimeFromParts

func TimeFromParts(args []Value, defaultLoc *time.Location) (time.Time, error)

TimeFromParts constructs a time.Time from year/month/day positional arguments, with optional hour/minute/second and timezone arguments.

func ValueToInt64

func ValueToInt64(val Value) (int64, error)

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

func DurationFromParts(weeks, days, hours, minutes, seconds int64) Duration

DurationFromParts assembles a Duration from week, day, hour, minute, and second components.

func DurationFromSeconds

func DurationFromSeconds(seconds int64) Duration

DurationFromSeconds builds a Duration from a whole-second count.

func ParseDurationString

func ParseDurationString(input string) (Duration, error)

ParseDurationString parses a duration in Go's time.ParseDuration format or in ISO-8601 form.

func SecondsDuration

func SecondsDuration(value int64, unit string) Duration

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).

func (Duration) ISO8601

func (d Duration) ISO8601() string

ISO8601 returns the duration formatted as an ISO-8601 string.

func (Duration) Parts

func (d Duration) Parts() map[string]int64

Parts decomposes the duration into days, hours, minutes, and seconds.

func (Duration) Seconds

func (d Duration) Seconds() int64

Seconds returns the duration as a whole number of seconds.

func (Duration) String

func (d Duration) String() string

String returns the duration formatted as "<n>s".

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

func NewMoneyFromCents(cents int64, currency string) (Money, error)

NewMoneyFromCents constructs a Money from an integer cents value and a currency code.

func ParseMoneyLiteral

func ParseMoneyLiteral(input string) (Money, error)

ParseMoneyLiteral parses a textual money literal of the form "X.XX CUR".

func (Money) Add

func (m Money) Add(other Money) (Money, error)

Add returns the sum of m and other, or an error if their currencies differ or the result would overflow the int64 cents range.

func (Money) Cents

func (m Money) Cents() int64

Cents returns the amount in the smallest currency unit.

func (Money) Currency

func (m Money) Currency() string

Currency returns the ISO-4217 currency code.

func (Money) DivInt

func (m Money) DivInt(divisor int64) (Money, error)

DivInt divides m by the given integer divisor, returning an error on division by zero or on the one signed-division overflow case (MinInt64 / -1, whose true result is not representable in int64).

func (Money) MulInt

func (m Money) MulInt(factor int64) (Money, error)

MulInt multiplies m by the given integer factor, preserving the currency, or returns an error if the result would overflow the int64 cents range.

func (Money) String

func (m Money) String() string

String returns the amount formatted as "X.XX CUR".

func (Money) Sub

func (m Money) Sub(other Money) (Money, error)

Sub returns m minus other, or an error if their currencies differ or the result would overflow the int64 cents range.

type Range

type Range struct {
	Start int64
	End   int64
}

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

type SliceIdentity struct {
	Ptr uintptr
	Len int
	Cap int
}

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

func NewArray(a []Value) Value

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 NewBool

func NewBool(b bool) Value

NewBool returns a boolean Value.

func NewDuration

func NewDuration(d Duration) Value

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 NewFloat

func NewFloat(f float64) Value

NewFloat returns a floating-point Value.

func NewHash

func NewHash(h map[string]Value) Value

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

func NewInt(i int64) Value

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

func NewMoney(m Money) Value

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 NewNil

func NewNil() Value

NewNil returns a nil Value.

func NewObject

func NewObject(attrs map[string]Value) Value

NewObject returns an object Value with the given attributes.

func NewRange

func NewRange(r Range) Value

NewRange returns a range Value.

func NewString

func NewString(s string) Value

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 NewSymbol

func NewSymbol(name string) Value

NewSymbol returns a symbol Value.

func NewTime

func NewTime(t time.Time) Value

NewTime returns a time Value.

func NewValue

func NewValue(kind ValueKind, data any) Value

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) Array

func (v Value) Array() []Value

Array returns the array content of v, or nil if v is not an array.

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) Bool

func (v Value) Bool() bool

Bool returns the boolean content of v, or false if v is not a bool.

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

func (v Value) Data() any

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

func (v Value) Duration() 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

func (v Value) Equal(other Value) bool

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) Float

func (v Value) Float() float64

Float returns the float content of v, coercing from int if needed.

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) Hash

func (v Value) Hash() map[string]Value

Hash returns the hash content of v, or nil if v is not a hash or object.

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) Int

func (v Value) Int() int64

Int returns the integer content of v, coercing from float if needed.

func (Value) IsNil

func (v Value) IsNil() bool

IsNil reports whether v is a nil value.

func (Value) Kind

func (v Value) Kind() ValueKind

Kind returns the ValueKind of v.

func (Value) Money

func (v Value) Money() Money

Money returns the money content of v, or a zero Money if v is not money.

func (Value) Range

func (v Value) Range() Range

Range returns the range content of v, or a zero Range if v is not a range.

func (Value) String

func (v Value) String() 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

func (Value) Time

func (v Value) Time() time.Time

Time returns the time content of v, or a zero time if v is not a time.

func (Value) Truthy

func (v Value) Truthy() bool

Truthy reports whether v is considered true in a boolean context.

type ValueKind

type ValueKind int

ValueKind identifies the type of a runtime Value.

const (
	// KindNil is the nil value kind.
	KindNil ValueKind = iota
	KindBool
	KindInt
	KindFloat
	KindString
	KindArray
	KindHash
	KindFunction
	KindBuiltin
	KindMoney
	KindDuration
	KindTime
	KindSymbol
	KindObject
	KindRange
	KindBlock
	KindEnum
	KindEnumValue
	KindClass
	KindInstance
)

func (ValueKind) String

func (k ValueKind) String() string

String returns the human-readable name of the ValueKind.

Jump to

Keyboard shortcuts

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