Help us shape the future of Go! Take the 2026 Go Developer Survey

lval

package
v1.95.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package lval holds ELPS values in Go without the interpreter, and reads and writes them as elps typed JSON (json:dump-bytes :typed true, libjson.DumpTyped). A Value holds one value: an int, a float, a string, a symbol, a keyword, a boolean, bytes, a list, a vector, a sorted map, a tagged value or an array.

It is for Go code that exchanges values with an ELPS program but cannot link package lisp, such as a WebAssembly guest. Typed JSON keeps every type: 1 and 1.0 stay apart, and so do a string, a symbol and a keyword of one spelling. Marshal writes the bytes libjson.DumpTyped writes for the same value, and Unmarshal reads what libjson.LoadTyped reads.

Decode and Encode convert between a Value and ordinary Go values. A struct maps to a sorted map by the rules of package elpstag, which lisp.Decode and lisp.Encode follow too:

var req struct {
	Item     string `elps:"item"`
	Quantity int    `elps:"quantity"`
}
if err := lval.Decode(v, &req); err != nil { ... }

The package imports neither package lisp nor encoding/json.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(v Value, dst any) error

Decode stores v in the Go value dst points to:

  • a Value takes v as it is;
  • an int or uint type takes an int (or a whole float), a float type any number, a string a string, symbol or keyword, a bool a boolean or nil (false), a []byte bytes;
  • a slice or array takes a list or vector, nil as an empty slice;
  • a map takes a sorted map, and a struct a sorted map whose keys name its fields by the rules of package elpstag, which lisp.Decode follows too: the field's elps tag (`elps:"name"`), or its name, in any case, as a string, symbol or keyword key; when several keys match a field, the one spelled exactly as its name wins;
  • an any takes a natural Go value: int64, float64, string, bool, []byte, []any, map[string]any, or a Value for anything else.

These are lisp.DecodeInto's rules, and a map of keys that convert to one Go key (such as "a" and :a for a map[string]T) fails as it does there.

func Marshal

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

Marshal writes v as elps typed JSON: the bytes libjson.DumpTyped writes for the same value, map members in typed JSON's order.

Types

type Kind

type Kind uint8

Kind is the ELPS type of a Value.

const (
	Nil Kind = iota // the empty list, ()
	Int
	Float
	String
	Symbol
	Keyword
	Bool
	Bytes
	List
	Vector
	Map
	Tagged
	Array
)

The kinds of Value.

func (Kind) String

func (k Kind) String() string

type Value

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

Value is one ELPS value. The zero Value is nil, the empty list.

func Encode

func Encode(x any) (Value, error)

Encode returns the Value of a Go value: the reverse of Decode. A struct becomes a sorted map with string keys (its fields' elps tags or names; a tag with ",omitempty" leaves out a zero field), a Go map a sorted map, a slice a vector, a nil pointer, slice or map nil.

func MakeArray

func MakeArray(dims []int, cells ...Value) Value

MakeArray returns an array of dims with cells in row-major order.

func MakeBool

func MakeBool(b bool) Value

MakeBool returns the symbol true or false.

func MakeBytes

func MakeBytes(b []byte) Value

MakeBytes returns bytes.

func MakeFloat

func MakeFloat(f float64) Value

MakeFloat returns a float.

func MakeInt

func MakeInt(n int64) Value

MakeInt returns an int.

func MakeKeyword

func MakeKeyword(name string) Value

MakeKeyword returns the keyword :name. A leading colon is optional.

func MakeList

func MakeList(vs ...Value) Value

MakeList returns a list of vs; with none it returns nil.

func MakeMap

func MakeMap(kv ...Value) Value

MakeMap returns a sorted map of alternating keys and values. A key must be a string, a symbol, a keyword, an int or a boolean; a later value of the same key replaces an earlier one.

func MakeString

func MakeString(s string) Value

MakeString returns a string.

func MakeSymbol

func MakeSymbol(name string) Value

MakeSymbol returns the symbol name. As in ELPS, "true" and "false" are booleans, and a name that starts with a colon is a keyword.

func MakeTagged

func MakeTagged(typ string, data Value) Value

MakeTagged returns a tagged value of type typ.

func MakeVector

func MakeVector(vs ...Value) Value

MakeVector returns a vector of vs.

func StrMap

func StrMap(kv map[string]Value) Value

StrMap returns a sorted map with string keys.

func Unmarshal

func Unmarshal(data []byte) (Value, error)

Unmarshal reads typed JSON into a Value. It accepts every input libjson.LoadTyped accepts, and reads it as the same value. It does not check that the input is canonical, so it also accepts some inputs LoadTyped refuses, such as a number with a leading zero.

func (Value) Bool

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

Bool returns v's boolean, and whether v is true or false.

func (Value) Bytes

func (v Value) Bytes() ([]byte, bool)

Bytes returns v's bytes, and whether v is bytes.

func (Value) Dims

func (v Value) Dims() []int

Dims returns an array's dimensions.

func (Value) Float

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

Float returns v as a float (an int converted), and whether it is a number.

func (Value) Get

func (v Value) Get(key any) (Value, bool)

Get returns the value of key in a map, and whether it is there. A Go string key finds a string key; use a Value to find a symbol or keyword.

func (Value) Index

func (v Value) Index(i int) Value

Index returns element i of a list, vector or array.

func (Value) Int

func (v Value) Int() (int64, bool)

Int returns v's int, and whether v is an int.

func (Value) IsNil

func (v Value) IsNil() bool

IsNil reports whether v is the empty list.

func (Value) Items

func (v Value) Items() []Value

Items returns the elements of a list, a vector or an array (row-major), or a map's values in key order. It returns nil for anything else.

func (Value) Keys

func (v Value) Keys() []Value

Keys returns a map's keys, in order.

func (Value) Kind

func (v Value) Kind() Kind

Kind returns v's ELPS type.

func (Value) Len

func (v Value) Len() int

Len is the number of elements of a list, vector, array or map.

func (*Value) Set

func (v *Value) Set(key, val Value)

Set sets key to val in the map v, which must be a map (the zero Value becomes one). As in an ELPS map, a string and a symbol of one spelling are one key: setting one replaces the other.

func (Value) Str

func (v Value) Str() (string, bool)

Str returns the text of a string, a symbol or a keyword (without its colon), and whether v is one of them.

func (Value) String

func (v Value) String() string

String renders v in ELPS syntax, for messages and logs.

func (Value) Tag

func (v Value) Tag() (string, bool)

Tag returns a tagged value's type name, and whether v is tagged.

func (Value) TagData

func (v Value) TagData() Value

TagData returns a tagged value's data (nil for anything else).

Jump to

Keyboard shortcuts

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