value

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 2 Imported by: 0

README

Documentation

Overview

Package value is a dynamic, JSON-compatible value model: a value space with typed, constant-error accessors and the coercion rules a small expression language needs. A decoded JSON document is already a Value, so the package interoperates directly with encoding/json while supplying the typed accessors and arithmetic that raw any values lack.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsBool

func AsBool(v Value) (bool, error)

AsBool returns v as a bool, or ErrNotBool.

func AsFloat

func AsFloat(v Value) (float64, error)

AsFloat returns v as a float64, or ErrNotNumber.

func AsInt

func AsInt(v Value) (int64, error)

AsInt returns v as an int64 (truncating a float), or ErrNotNumber.

func AsObject

func AsObject(v Value) (map[string]Value, error)

AsObject returns v as an object, or ErrNotObject.

func AsString

func AsString(v Value) (string, error)

AsString returns v as a string, or ErrNotString.

func Compare

func Compare(a, b Value) (int, error)

Compare orders a and b, returning -1, 0, or 1. Numbers compare across int/float; strings compare lexically; any other pairing is ErrIncomparable.

func Equal

func Equal(a, b Value) bool

Equal reports value equality, treating int and float as comparable numbers.

func Truthy

func Truthy(v Value) bool

Truthy reports truthiness: nil and false are falsey; everything else (including zero and the empty string) is truthy.

Types

type Error

type Error string

Error is the sentinel error type for the value package. Every error the package emits is a constant of this type, matched with errors.Is.

const (
	// ErrNotObject is returned when a value is required to be an object.
	ErrNotObject Error = "value: not an object"
	// ErrNotList is returned when a value is required to be a list.
	ErrNotList Error = "value: not a list"
	// ErrNotString is returned when a value is required to be a string.
	ErrNotString Error = "value: not a string"
	// ErrNotNumber is returned when a value is required to be numeric.
	ErrNotNumber Error = "value: not a number"
	// ErrNotBool is returned when a value is required to be a bool.
	ErrNotBool Error = "value: not a bool"
	// ErrIncomparable is returned when two values cannot be ordered.
	ErrIncomparable Error = "value: incomparable types"
)

func (Error) Error

func (e Error) Error() string

Error renders the sentinel message.

type Kind

type Kind int

Kind names the dynamic type of a Value.

const (
	// KindNull is the kind of nil.
	KindNull Kind = iota
	// KindBool is the kind of a bool.
	KindBool
	// KindInt is the kind of an int64.
	KindInt
	// KindFloat is the kind of a float64.
	KindFloat
	// KindString is the kind of a string.
	KindString
	// KindList is the kind of a []Value.
	KindList
	// KindObject is the kind of a map[string]Value.
	KindObject
)

func KindOf

func KindOf(v Value) Kind

KindOf reports the Kind of v. An unrecognized concrete type reports KindNull.

type Value

type Value = any

Value is the dynamic value: nil | bool | int64 | float64 | string | []Value | map[string]Value. The alias keeps encoding/json interop direct (a decoded JSON document is already a Value); the accessors below supply the typed, constant-error discipline. Numbers decoded from JSON are float64; numeric literals may be int64. The accessors and arithmetic treat both uniformly.

func Add

func Add(a, b Value) (Value, error)

Add adds two numbers, or concatenates when either operand is a string. Any other pairing is ErrNotNumber.

func AsList

func AsList(v Value) ([]Value, error)

AsList returns v as a list, or ErrNotList.

Jump to

Keyboard shortcuts

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