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 ¶
- func Decode(v Value, dst any) error
- func Marshal(v Value) ([]byte, error)
- type Kind
- type Value
- func Encode(x any) (Value, error)
- func MakeArray(dims []int, cells ...Value) Value
- func MakeBool(b bool) Value
- func MakeBytes(b []byte) Value
- func MakeFloat(f float64) Value
- func MakeInt(n int64) Value
- func MakeKeyword(name string) Value
- func MakeList(vs ...Value) Value
- func MakeMap(kv ...Value) Value
- func MakeString(s string) Value
- func MakeSymbol(name string) Value
- func MakeTagged(typ string, data Value) Value
- func MakeVector(vs ...Value) Value
- func StrMap(kv map[string]Value) Value
- func Unmarshal(data []byte) (Value, error)
- func (v Value) Bool() (bool, bool)
- func (v Value) Bytes() ([]byte, bool)
- func (v Value) Dims() []int
- func (v Value) Float() (float64, bool)
- func (v Value) Get(key any) (Value, bool)
- func (v Value) Index(i int) Value
- func (v Value) Int() (int64, bool)
- func (v Value) IsNil() bool
- func (v Value) Items() []Value
- func (v Value) Keys() []Value
- func (v Value) Kind() Kind
- func (v Value) Len() int
- func (v *Value) Set(key, val Value)
- func (v Value) Str() (string, bool)
- func (v Value) String() string
- func (v Value) Tag() (string, bool)
- func (v Value) TagData() Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
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.
Types ¶
type Kind ¶
type Kind uint8
Kind is the ELPS type of a Value.
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 ¶
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 MakeKeyword ¶
MakeKeyword returns the keyword :name. A leading colon is optional.
func MakeMap ¶
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 MakeSymbol ¶
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 ¶
MakeTagged returns a tagged value of type typ.
func Unmarshal ¶
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) Get ¶
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) Items ¶
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) Set ¶
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 ¶
Str returns the text of a string, a symbol or a keyword (without its colon), and whether v is one of them.