value

package
v0.0.0-...-66342f7 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidKind          = errors.New("invalid kind")
	ErrAttributeNotFound    = errors.New("attribute not found")
	ErrArrayIndexOutOfRange = errors.New("array index out of range")
	ErrInvalidKey           = errors.New("invalid key")
	ErrDivisionByZero       = errors.New("division by zero")
)

Functions

This section is empty.

Types

type Function

type Function = func(...Value) (Value, error)

type Indexer

type Indexer interface {
	Get(Value) (Value, error)
	Values() iter.Seq[Value]
	Len() int

	Index(int) (Value, error)
	All() iter.Seq2[int, Value]
	WithMarks(marks uint16) Indexer
}

Indexer is an interface used to represent array types.

type Kind

type Kind uint8
const (
	NullKind Kind = iota
	BoolKind
	NumberKind
	StringKind
	ArrayKind
	ObjectKind
	FuncKind
)

func (Kind) String

func (k Kind) String() string

type List

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

List implements Indexer and is the default for the array type.

func (*List) All

func (l *List) All() iter.Seq2[int, Value]

All returns the array's (idx, Value) pairs.

func (*List) Get

func (l *List) Get(key Value) (Value, error)

Get returns the array's value at the specified index (Value).

func (*List) Index

func (l *List) Index(i int) (Value, error)

Index returns the array's value at the specified index (int).

func (*List) Len

func (l *List) Len() int

Len returns the array's length.

func (*List) Values

func (l *List) Values() iter.Seq[Value]

Values returns the array's values.

func (*List) WithMarks

func (l *List) WithMarks(marks uint16) Indexer

WithMarks returns a new Indexer with the additional marks added.

type Map

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

Map implements Mapper and is the default for the object type.

func (*Map) All

func (m *Map) All() iter.Seq2[Value, Value]

All returns the object's key/value pairs.

func (*Map) Attr

func (m *Map) Attr(key string) (Value, error)

Attr returns an object value by key (string).

func (*Map) Get

func (m *Map) Get(key Value) (Value, error)

Get returns an object value by key (Value).

func (*Map) Keys

func (m *Map) Keys() iter.Seq[Value]

Keys returns the object's keys.

func (*Map) Len

func (m *Map) Len() int

Len returns the object's length.

func (*Map) Values

func (m *Map) Values() iter.Seq[Value]

Values returns the object's values.

func (*Map) WithMarks

func (m *Map) WithMarks(marks uint16) Mapper

WithMarks returns a new Mapper with the additional marks added.

type Mapper

type Mapper interface {
	Get(Value) (Value, error)
	Values() iter.Seq[Value]
	Len() int

	Attr(string) (Value, error)
	All() iter.Seq2[Value, Value]
	Keys() iter.Seq[Value]
	WithMarks(marks uint16) Mapper
}

Mapper is an interface used to represent objects types.

type Value

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

Value represents a value of the following types: string, number, bool, object, array, null, func

func Array

func Array(v Indexer) Value

Array returns a new array value.

func Bool

func Bool(v bool) Value

Bool returns a new bool value.

func Func

func Func(fn func(...Value) (Value, error)) Value

Func returns a new func value.

func MustMap

func MustMap(kvs ...Value) Value

MustMap creates a new ordered map by passing (key, value, key, value)

This function panics if there's an odd number of elements passed, or if a key value is not a String.

func NewList

func NewList(v ...Value) Value

NewList returns a new array value backed by List.

func NewMap

func NewMap(kvs ...Value) (Value, error)

NewMap creates a new ordered map by passing (key, value, key, value)

func Null

func Null() Value

Null returns null value.

func Number

func Number[T ~int | ~int64 | ~uint64 | ~float64](v T) Value

Number returns a new number value.

func Object

func Object(v Mapper) Value

Object returns a new object value.

func ParseNumber

func ParseNumber(v string) (Value, error)

ParseNumber parses a string number and returns a Value with of kind Number.

func ParseString

func ParseString(v string) (Value, error)

ParseString parses a literal string. It's similar to strconv.Unquote.

func String

func String(v string) Value

String returns a new string value.

func (Value) Add

func (v Value) Add(x Value) (Value, error)

Add adds x and returns a new value with the result.

func (Value) Attr

func (v Value) Attr(name string) (Value, error)

Attr returns an attributes value by name.

func (Value) Bool

func (v Value) Bool() (bool, error)

Bool returns the value as a boolean

func (Value) Call

func (v Value) Call(args ...Value) (Value, error)

Call calls the function value with the specified arguments.

func (Value) Divide

func (v Value) Divide(x Value) (Value, error)

Divide divides by x and returns a new value with the result.

func (Value) Equal

func (v Value) Equal(other Value) Value

Equal returns true if the values are identical.

func (Value) Float

func (v Value) Float() (float64, error)

Float returns a number as a float64

func (Value) Get

func (v Value) Get(key Value) (Value, error)

Get returns either an attribute or index value.

The key can therefore be a number or string.

func (Value) GreaterThan

func (v Value) GreaterThan(other Value) Value

GreaterThan returns true if v is greater than other.

func (Value) GreaterThanEqual

func (v Value) GreaterThanEqual(other Value) Value

GreaterThanEqual returns true if v is greater than or equal to other.

func (Value) HasMarks

func (v Value) HasMarks(marks uint16) bool

HasMark returns whether the valve has the specified mark.

func (Value) Index

func (v Value) Index(i int) (Value, error)

Index returns an array's value by index.

func (Value) Indexer

func (v Value) Indexer() (Indexer, error)

Indexer returns an indexer if the value is an array.

func (Value) Int64

func (v Value) Int64() (int64, error)

Int64 returns a number as an int64

func (Value) Kind

func (v Value) Kind() Kind

Kind returns the value's Kind

func (Value) Len

func (v Value) Len() (int, error)

Len returns the length of strings, arrays and objects.

func (Value) LessThan

func (v Value) LessThan(other Value) Value

LessThan returns true if v is less than other.

func (Value) LessThanEqual

func (v Value) LessThanEqual(other Value) Value

LessThanEqual returns true if v is less than or equal to other.

func (Value) Mapper

func (v Value) Mapper() (Mapper, error)

Mapper returns a mapper if the value is an object.

func (Value) Marks

func (v Value) Marks(recursive bool) uint16

Marks returns the values marks.

func (Value) MarshalJSON

func (v Value) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Value) Modulo

func (v Value) Modulo(x Value) (Value, error)

Modulo calculates v % x and returns a new value with the result.

func (Value) Multiply

func (v Value) Multiply(x Value) (Value, error)

Multiply multiplies by x and returns a new value with the result.

func (Value) Negate

func (v Value) Negate() (Value, error)

Negate negates the value.

For boolean values: !value For number values: -value

func (Value) Not

func (v Value) Not() Value

Not returns true if the value is falsy.

func (Value) String

func (v Value) String() string

String returns a string representation of the value.

For values that are of kind String, this returns the actual string value.

Object and Array kinds are serialized to JSON.

func (Value) Subtract

func (v Value) Subtract(x Value) (Value, error)

Subtract subtracts x and returns a new value with the result.

func (Value) Truthy

func (v Value) Truthy() bool

Truthy returns if the value is "truthy" depending on the type:

bool: true if true string: true if length > 0 number: true if value > 0 object: true if length > 0 array: true of length > 0 null: false

func (*Value) UnmarshalJSON

func (v *Value) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (Value) WithMarks

func (v Value) WithMarks(marks uint16) Value

WithMarks returns a copy of the value with the specified marks.

Marks are not considered part of the value for equality/comparison.

Jump to

Keyboard shortcuts

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