persist

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: CC0-1.0 Imports: 14 Imported by: 0

README

persist

Go Reference

persist is a Go library for immutable, persistent JSON-like data structures. It provides compact values, scalar-keyed persistent maps, persistent vectors, and mixed map/vector path updates.

Use it when you want cheap snapshots, undo/redo state, copy-on-write configuration or policy trees, request-local document edits, or concurrent reads with independently derived versions.

Install

go get github.com/asciimoth/persist

Value Model

Value can hold:

  • nil
  • int64
  • float64
  • string
  • bool
  • Map
  • Vector

Key can hold only scalar key types: nil, int64, float64, string, and bool. Maps and vectors cannot be map keys.

Zero values are valid: Value{} is nil, Key{} is the nil key, Map{} is an empty map, and Vector{} is an empty vector.

Usage

package main

import (
	"fmt"

	"github.com/asciimoth/persist"
)

func main() {
	profile := persist.NewMap().
		Assoc(persist.KString("name"), persist.String("Ada")).
		Assoc(persist.KString("visits"), persist.Int(1))

	root := persist.MapValue(persist.NewMap().
		Assoc(persist.KString("profile"), persist.MapValue(profile)))

	next, err := persist.SetIn(
		root,
		persist.Int(2),
		persist.MapKey(persist.KString("profile")),
		persist.MapKey(persist.KString("visits")),
	)
	if err != nil {
		panic(err)
	}

	oldVisits, _ := persist.GetIn(root, persist.MapKey(persist.KString("profile")), persist.MapKey(persist.KString("visits")))
	newVisits, _ := persist.GetIn(next, persist.MapKey(persist.KString("profile")), persist.MapKey(persist.KString("visits")))
	oldN, _ := oldVisits.Int64()
	newN, _ := newVisits.Int64()

	fmt.Println(oldN, newN)
}

Every update returns a new root and leaves older roots unchanged. Unchanged subtrees are shared internally.

JSON

Use persist.FromJSON to decode ordinary JSON into a Value, and use Value.ToJSON, Map.ToJSON, or Vector.ToJSON to encode it again. Value, Map, and Vector also implement the standard encoding/json marshal and unmarshal interfaces.

root, err := persist.FromJSON([]byte(`{"items":[{"score":10}]}`))
if err != nil {
	panic(err)
}
data, err := root.ToJSON()

JSON objects decode as maps with string keys. Maps that use nil, integer, float, or bool keys are valid in memory but cannot be encoded as JSON objects. Integer-looking JSON numbers decode as Int when they fit in int64; decimal/exponent numbers decode as Float.

API Notes

  • Map.Assoc and Map.Dissoc return new maps.
  • Vector.Set, Vector.Append, and Vector.Pop return new vectors.
  • Map.All and Vector.All return standard Go iterators over immediate entries.
  • Map.Walk and Vector.Walk return standard Go iterators over nested map/vector trees with paths expressed as Step values.
  • GetIn and SetIn operate on nested map/vector paths.
  • FromJSON and ToJSON convert ordinary JSON documents to and from Value.
  • NewReadOnlyMapConfig, NewReadOnlyVectorConfig, and NewReadOnlyConfig expose read-only views over string-keyed map/vector trees that implement github.com/asciimoth/configer/configer.Config.
  • Value.Equal, Map.Equal, and Vector.Equal perform deep semantic equality checks.
  • Key.Equal, Step.Equal, and Kind.Equal compare keys, path steps, and kinds.
  • Value.Same is shallow: scalars compare by value, strings by content, and maps/vectors by identity. It is not deep equality.
  • Map iteration order is unspecified. Vector iteration is by ascending index.
  • Float map keys normalize -0 to +0 and all NaN payloads to a canonical NaN. Float leaf values preserve exact IEEE-754 bits.
  • String and KString retain the supplied string backing storage without copying. Use StringClone and KStringClone when long-lived values should avoid retaining a larger original buffer.

License

Files in this repository are distributed under the CC0 license.

CC0
To the extent possible under law, ASCIIMoth has waived all copyright and related or neighboring rights to persist.

Documentation

Overview

Package persist provides immutable, persistent values, maps, and vectors for JSON-like application data.

The public value domain is closed. A Value is nil, int64, float64, string, bool, Map, or Vector. A Key is the scalar subset accepted by Map: nil, int64, float64, string, or bool. The package does not accept arbitrary Go objects, reflection-based values, generic key types, map keys that are themselves collections, or mutable builders.

Build leaves with Nil, Int, Float, String, StringClone, and Bool. Build keys with KNil, KInt, KFloat, KString, KStringClone, and KBool. Wrap collections with MapValue and VectorValue when they need to be stored inside another Value.

Collection updates never mutate the receiver. Instead, methods such as Map.Assoc, Map.Dissoc, Vector.Set, Vector.Append, and Vector.Pop return a new collection root while unchanged internal nodes remain shared with older versions. This makes it cheap to keep snapshots, implement undo stacks, build copy-on-write configuration trees, or derive request-local changes from a shared baseline.

Zero values are usable:

  • Value{} is the nil value.
  • Key{} is the nil key.
  • Map{} is an empty map.
  • Vector{} is an empty vector.

Persistent maps and vectors are safe for concurrent reads. Multiple goroutines may also derive separate new versions from the same root at the same time because published nodes are never modified.

Use typed accessors such as Value.Int64, Value.Map, Key.StringValue, Step.Key, and Step.Index to inspect values and paths. Accessors return a boolean instead of panicking on a kind mismatch. Use GetIn and SetIn to read and replace nested data through mixed map/vector paths.

Use FromJSON, Value.ToJSON, Map.ToJSON, Vector.ToJSON, or the standard encoding/json package to convert ordinary JSON documents. JSON objects decode as maps with string keys. Maps with non-string keys cannot be encoded as JSON objects.

Use NewReadOnlyMapConfig, NewReadOnlyVectorConfig, and NewReadOnlyConfig for read-only views over string-keyed map/vector trees that implement github.com/asciimoth/configer/configer.Config.

Use Value.Equal, Map.Equal, and Vector.Equal for deep semantic equality. Use Key.Equal, Step.Equal, and Kind.Equal for public key, path, and kind comparisons. Map.All and Vector.All return standard Go iterators over immediate entries. Map.Walk and Vector.Walk return standard Go iterators over nested map/vector trees. Use Value.Same for cheap scalar equality and collection identity checks. Same is intentionally not deep equality for maps and vectors. Map iteration order is unspecified and may vary across map lineages; vector iteration order is ascending index order.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidPath = configer.ErrInvalidPath

ErrInvalidPath is returned when a path cannot be applied to the current tree.

View Source
var ErrNotFound = configer.ErrNotFound

ErrNotFound is returned when a path does not exist.

View Source
var ErrReadOnly = configer.ErrReadOnly

ErrReadOnly is returned by mutating operations on read-only configs.

Functions

This section is empty.

Types

type Key

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

Key has the same compact physical representation as Value, but public key constructors only admit scalar map-key types.

Invariants:

  • The zero Key is the nil key.
  • Valid keys are nil, int64, float64, string, and bool only.
  • Map and vector values cannot be represented as valid keys through the public API.

Usage:

  • Build keys with KNil, KInt, KFloat, KString, KStringClone, and KBool.
  • Use typed accessors when inspecting callback keys from Map.Range.

Security notes:

  • KString stores string bytes without copying and can extend backing string lifetimes. KStringClone makes an exact-size retained copy.
  • KFloat canonicalizes zeros and NaNs so untrusted NaN payloads cannot make entries unretrievable through non-reflexive key equality.

func KBool

func KBool(x bool) Key

KBool returns a bool map key.

func KFloat

func KFloat(x float64) Key

KFloat returns a float64 map key with total, reflexive key semantics.

Signed zero is normalized to positive zero, and all NaN payloads are normalized to one canonical quiet NaN. Int(1) and Float(1) remain distinct key kinds.

func KInt

func KInt(x int64) Key

KInt returns an int64 map key.

func KNil

func KNil() Key

KNil returns the nil map key. The zero Key is also nil.

func KString

func KString(x string) Key

KString returns a string map key without copying its bytes.

func KStringClone

func KStringClone(x string) Key

KStringClone returns a string map key after copying x into an exact-size backing allocation.

func (Key) BoolValue

func (k Key) BoolValue() (bool, bool)

BoolValue extracts a bool key.

func (Key) Equal

func (k Key) Equal(other Key) bool

Equal reports whether k and other are the same scalar map key.

func (Key) Float64

func (k Key) Float64() (float64, bool)

Float64 extracts a floating-point key.

func (Key) Int64

func (k Key) Int64() (int64, bool)

Int64 extracts an integer key.

func (Key) Kind

func (k Key) Kind() Kind

Kind reports the key's kind.

func (Key) StringValue

func (k Key) StringValue() (string, bool)

StringValue extracts a string key.

type Kind

type Kind uint8

Kind identifies the closed set of value and key variants supported by this package.

Invariants:

  • KindInvalid is never returned for values built through the public constructors.
  • Map and vector kinds are valid Value kinds but invalid Key kinds.

Usage:

  • Inspect a Value or Key with Kind before using typed accessors when the expected kind is not already known.

Security notes:

  • Kind is metadata only. Use FromJSON or json.Unmarshal to validate external JSON before inspecting decoded values.
const (
	// KindInvalid marks an impossible or internally corrupted representation.
	KindInvalid Kind = iota
	// KindNil is the nil leaf value and nil map key.
	KindNil
	// KindInt is an int64 leaf value or map key.
	KindInt
	// KindFloat is a float64 leaf value or map key.
	KindFloat
	// KindString is a string leaf value or map key.
	KindString
	// KindBool is a bool leaf value or map key.
	KindBool
	// KindMap is an immutable persistent map value.
	KindMap
	// KindVector is an immutable persistent vector value.
	KindVector
)

func (Kind) Equal

func (k Kind) Equal(other Kind) bool

Equal reports whether k and other identify the same kind.

func (Kind) String

func (k Kind) String() string

String returns a stable diagnostic name for k.

type Map

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

Map is an immutable persistent map from scalar Key to Value.

Invariants:

  • The zero Map is empty.
  • A non-empty small map stores alternating key/value slots in small and has root == nil.
  • A large map stores a CHAMP hash trie in root; trie nodes are never mutated after publication.
  • All derived versions share the same immutable hash state as their parent.

Usage:

  • Use NewMap for a fresh empty map, then Assoc and Dissoc to derive new versions.
  • Use Get for lookup, All or Range for unordered iteration, and Walk for recursive traversal.
  • Map is a small header; copying it is cheap and does not copy entries.

Security notes:

  • Maps use randomized maphash state per NewMap lineage to reduce predictable collision behavior. This is not a cryptographic hash or authorization boundary.
  • Published nodes are immutable, making concurrent reads and concurrent derivation from a shared root race-free.

func NewMap

func NewMap() Map

NewMap returns an empty map with a fresh randomized hash lineage.

The zero Map is also a valid empty map. NewMap is useful when independent map lineages should not share hash seed state.

func (Map) All added in v0.2.0

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

All returns an iterator over every key/value pair in m.

Iteration order is unspecified and may differ between map lineages and versions. The iterator stops early when yield returns false.

func (Map) Assoc

func (m Map) Assoc(k Key, v Value) Map

Assoc returns a map containing k -> v.

The receiver is never modified. If k already maps to a Same value, Assoc returns the original map header and preserves representation identity.

func (Map) Dissoc

func (m Map) Dissoc(k Key) Map

Dissoc returns a map without k.

The receiver is never modified. If k is absent, Dissoc returns the original map header.

func (Map) Equal

func (m Map) Equal(other Map) bool

Equal reports whether m and other contain the same keys mapped to Equal values.

func (Map) Get

func (m Map) Get(k Key) (Value, bool)

Get looks up k in m.

func (Map) Len

func (m Map) Len() int

Len returns the number of entries in m.

func (Map) MarshalJSON

func (m Map) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Map) Range

func (m Map) Range(fn func(Key, Value) bool)

Range calls fn for every entry until fn returns false.

Iteration order is unspecified and may differ between map lineages and versions. The callback must not rely on a stable order for serialization or signing.

func (Map) String added in v0.2.0

func (m Map) String() string

String returns m formatted as indented JSON.

Maps can be formatted only when every key is a string, because JSON object keys are strings. When m cannot be represented as JSON, String returns the serialization error text.

func (Map) ToJSON

func (m Map) ToJSON() ([]byte, error)

ToJSON encodes m as a JSON object.

Maps can be encoded only when every key is a string, because JSON object keys are strings. Non-finite Float values cannot be encoded as JSON numbers.

func (*Map) UnmarshalJSON

func (m *Map) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (Map) Walk added in v0.2.0

func (m Map) Walk() iter.Seq2[[]Step, Value]

Walk returns a depth-first preorder iterator over every value reachable from entries in m.

Paths are relative to m and do not include an empty path for m itself. Each yielded path is a fresh slice that may be retained. Map entry order is unspecified, and vector entries are walked in ascending index order. Collection values are yielded before their descendants.

type ReadOnlyConfig

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

ReadOnlyConfig is a Config wrapper around an immutable Value tree.

func NewReadOnlyConfig

func NewReadOnlyConfig(root Value) *ReadOnlyConfig

NewReadOnlyConfig returns a read-only Config rooted at root.

func NewReadOnlyMapConfig

func NewReadOnlyMapConfig(m Map) *ReadOnlyConfig

NewReadOnlyMapConfig returns a read-only Config rooted at m.

func NewReadOnlyVectorConfig

func NewReadOnlyVectorConfig(v Vector) *ReadOnlyConfig

NewReadOnlyVectorConfig returns a read-only Config rooted at v.

func (*ReadOnlyConfig) Delete

func (c *ReadOnlyConfig) Delete(path configer.Path) error

Delete returns ErrReadOnly.

func (*ReadOnlyConfig) Get

func (c *ReadOnlyConfig) Get(path configer.Path) (any, error)

Get returns a deep copy of the value at path.

func (*ReadOnlyConfig) IsReadOnly

func (c *ReadOnlyConfig) IsReadOnly() bool

IsReadOnly reports whether mutating operations are disabled.

func (*ReadOnlyConfig) Lock

func (c *ReadOnlyConfig) Lock()

Lock takes the write lock.

func (*ReadOnlyConfig) RLock

func (c *ReadOnlyConfig) RLock()

RLock takes the read lock.

func (*ReadOnlyConfig) RUnlock

func (c *ReadOnlyConfig) RUnlock()

RUnlock releases the read lock.

func (*ReadOnlyConfig) ReadOnly

func (c *ReadOnlyConfig) ReadOnly() configer.Config

ReadOnly returns a read-only view backed by the same lock.

func (*ReadOnlyConfig) Set

func (c *ReadOnlyConfig) Set(path configer.Path, value any) error

Set returns ErrReadOnly.

func (*ReadOnlyConfig) Snapshot

func (c *ReadOnlyConfig) Snapshot() any

Snapshot returns a deep copy of the view root.

func (*ReadOnlyConfig) Unlock

func (c *ReadOnlyConfig) Unlock()

Unlock releases the write lock.

func (*ReadOnlyConfig) View

func (c *ReadOnlyConfig) View(path configer.Path) configer.Config

View returns a new Config rooted at path and backed by the same lock.

type Step

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

Step is one segment in a mixed map/vector path.

Invariants:

  • Valid steps are created by MapKey or Index.
  • Map steps contain a scalar Key.
  • Index steps contain a vector index.

Usage:

  • Pass steps to GetIn and SetIn to traverse nested immutable values.
  • A final map key may be absent for SetIn, in which case it is associated. Intermediate map keys and all vector indices must already exist.

Security notes:

  • Step carries no external capabilities. Error messages from SetIn describe only the failing container kind or index.

func Index

func Index(i int) Step

Index returns a path step that selects i from a vector.

func MapKey

func MapKey(k Key) Step

MapKey returns a path step that selects k from a map.

func (Step) Equal

func (s Step) Equal(other Step) bool

Equal reports whether s and other select the same path segment.

func (Step) Index added in v0.2.0

func (s Step) Index() (int, bool)

Index returns the vector index selected by s.

The bool result is false when s is not an index step.

func (Step) Key added in v0.2.0

func (s Step) Key() (Key, bool)

Key returns the map key selected by s.

The bool result is false when s is not a map-key step.

type Value

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

Value is a compact immutable tagged value.

Invariants:

  • The zero Value is the nil value.
  • Values built by this package have one of the Kind constants and are never mutated after publication.
  • Map and vector values hold pointers to immutable collection headers.
  • String values hold a pointer to immutable Go string bytes and a byte length.

Usage:

  • Build values with Nil, Int, Float, String, StringClone, Bool, MapValue, and VectorValue.
  • Use typed accessors such as Int64 and Map to inspect a value.
  • Use Same for cheap scalar equality and collection identity checks. Do not treat Same as deep equality.

Security notes:

  • String stores the supplied string without copying. This can intentionally extend the lifetime of that string's backing bytes. Use StringClone when retaining a substring or sensitive input would be undesirable.
  • The representation uses unsafe internally, but it does not store persistent pointers in uintptr, does not expose mutable aliases, and does not modify string bytes.

func Bool

func Bool(x bool) Value

Bool returns a bool leaf value.

func Float

func Float(x float64) Value

Float returns a float64 leaf value.

Float values preserve their exact IEEE-754 bits. This means Value.Same distinguishes -0.0 from +0.0 and compares NaN values by stored bits. Float map keys deliberately use different, normalized semantics; see KFloat.

func FromJSON

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

FromJSON decodes a JSON document into a Value.

JSON objects become maps with string keys, JSON arrays become vectors, and JSON null becomes Nil. Integer-looking numbers become Int values when they fit in int64; numbers with decimal points or exponents become Float values.

func GetIn

func GetIn(root Value, path ...Step) (Value, bool)

GetIn follows a mixed map/vector path from root.

It returns false if any step cannot be applied or if an intermediate key or index is absent.

func Int

func Int(x int64) Value

Int returns an int64 leaf value.

func MapValue

func MapValue(m Map) Value

MapValue wraps an immutable Map as a Value.

Re-wrapping the same Map can produce a distinct collection identity for Value.Same. No map entries or nodes are copied.

func Nil

func Nil() Value

Nil returns the nil leaf value. The zero Value is also nil.

func SetIn

func SetIn(root Value, replacement Value, path ...Step) (Value, error)

SetIn returns a new nested root with path replaced by replacement.

If path is empty, SetIn returns replacement. The final map key may be absent; all intermediate containers must exist and vector indices must be valid. The original root is returned for semantic no-op updates.

Example
package main

import (
	"fmt"

	"github.com/asciimoth/persist"
)

func main() {
	item := persist.NewMap().
		Assoc(persist.KString("name"), persist.String("Ada")).
		Assoc(persist.KString("score"), persist.Int(10))
	items := persist.NewVector(persist.MapValue(item))
	doc := persist.MapValue(persist.NewMap().Assoc(persist.KString("items"), persist.VectorValue(items)))

	updated, err := persist.SetIn(
		doc,
		persist.Int(11),
		persist.MapKey(persist.KString("items")),
		persist.Index(0),
		persist.MapKey(persist.KString("score")),
	)
	if err != nil {
		panic(err)
	}

	oldScore, _ := persist.GetIn(doc, persist.MapKey(persist.KString("items")), persist.Index(0), persist.MapKey(persist.KString("score")))
	newScore, _ := persist.GetIn(updated, persist.MapKey(persist.KString("items")), persist.Index(0), persist.MapKey(persist.KString("score")))
	oldN, _ := oldScore.Int64()
	newN, _ := newScore.Int64()
	fmt.Println(oldN, newN)

}
Output:
10 11

func String

func String(x string) Value

String returns a string leaf value without copying its bytes.

The value is immutable because Go strings are immutable, but the backing bytes remain live for as long as the Value remains live. Use StringClone when the caller wants an exact-size retained copy.

func StringClone

func StringClone(x string) Value

StringClone returns a string leaf value after copying x into an exact-size backing allocation.

Use this when storing a long-lived value derived from a substring of a larger string, or when retaining the caller's original backing storage is not desirable. It does not erase or otherwise sanitize the original string.

func VectorValue

func VectorValue(v Vector) Value

VectorValue wraps an immutable Vector as a Value.

Re-wrapping the same Vector can produce a distinct collection identity for Value.Same. No vector elements or nodes are copied.

func (Value) BoolValue

func (v Value) BoolValue() (bool, bool)

BoolValue extracts a bool value.

func (Value) Equal

func (v Value) Equal(other Value) bool

Equal reports semantic equality with other.

Scalar values use the same equality semantics as Same. Map and vector values compare recursively by content instead of wrapper identity.

func (Value) Float64

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

Float64 extracts a floating-point value.

func (Value) GoString

func (v Value) GoString() string

GoString returns a constructor-like diagnostic representation.

func (Value) Int64

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

Int64 extracts an integer value.

func (Value) Kind

func (v Value) Kind() Kind

Kind reports the value's kind.

func (Value) Map

func (v Value) Map() (Map, bool)

Map extracts a map value. The returned Map remains immutable.

func (Value) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (Value) Same

func (v Value) Same(other Value) bool

Same reports scalar equality and collection identity equality.

Same intentionally does not perform deep collection equality. Maps and vectors compare Same only when their wrapper identity is the same. Semantic no-op updates preserve identity, but independently constructed equal collections need not compare Same.

func (Value) StringValue

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

StringValue extracts a string value.

func (Value) ToJSON

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

ToJSON encodes v as JSON.

Maps can be encoded only when every key is a string, because JSON object keys are strings. Non-finite Float values cannot be encoded as JSON numbers.

func (*Value) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler.

func (Value) Vector

func (v Value) Vector() (Vector, bool)

Vector extracts a vector value. The returned Vector remains immutable.

type Vector

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

Vector is an immutable persistent indexed sequence.

Invariants:

  • The zero Vector is empty.
  • tail contains the final 1..16 values when count > 0, or nil when empty.
  • root contains complete 16-value leaves before the tail.
  • shift is a multiple of vecBits and describes the root trie height.
  • Root, branch, and leaf nodes are never mutated after publication.

Usage:

  • Use NewVector or repeated Append to build a vector.
  • Use Get, Set, Append, Pop, Len, All, Range, and Walk for indexed sequence work.
  • Vector is optimized for append/pop at the end and random lookup/update. It intentionally does not provide insertion or removal in the middle.

Security notes:

  • The implementation stores branch and leaf pointers as unsafe.Pointer so a single pointer type can represent both node kinds. Pointers remain visible to the garbage collector and are never converted to uintptr.
  • Published nodes are immutable, so concurrent reads and concurrent derivation from a shared root are race-free.

func NewVector

func NewVector(values ...Value) Vector

NewVector returns a vector containing values in order.

func (Vector) All added in v0.2.0

func (v Vector) All() iter.Seq2[int, Value]

All returns an iterator over every index/value pair in v.

Iteration is in ascending index order. The iterator stops early when yield returns false.

func (Vector) Append

func (v Vector) Append(value Value) Vector

Append returns a vector with value added at the end.

The receiver is never modified.

func (Vector) Equal

func (v Vector) Equal(other Vector) bool

Equal reports whether v and other contain Equal values in the same order.

func (Vector) Get

func (v Vector) Get(i int) (Value, bool)

Get returns the value at index i.

func (Vector) Len

func (v Vector) Len() int

Len returns the number of values in v.

func (Vector) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (Vector) Pop

func (v Vector) Pop() (Vector, bool)

Pop returns a vector without its final value.

The receiver is never modified. The bool result is false when the vector is empty.

func (Vector) Range

func (v Vector) Range(fn func(int, Value) bool)

Range calls fn in ascending index order until fn returns false.

func (Vector) Set

func (v Vector) Set(i int, value Value) (Vector, bool)

Set returns a vector with index i replaced by value.

The receiver is never modified. The bool result is false for an invalid index. If the existing value is Same as value, Set returns the original vector header and true.

func (Vector) String added in v0.2.0

func (v Vector) String() string

String returns v formatted as indented JSON.

When v cannot be represented as JSON, String returns the serialization error text.

func (Vector) ToJSON

func (v Vector) ToJSON() ([]byte, error)

ToJSON encodes v as a JSON array.

Non-finite Float values cannot be encoded as JSON numbers.

func (*Vector) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler.

func (Vector) Walk added in v0.2.0

func (v Vector) Walk() iter.Seq2[[]Step, Value]

Walk returns a depth-first preorder iterator over every value reachable from entries in v.

Paths are relative to v and do not include an empty path for v itself. Each yielded path is a fresh slice that may be retained. Vector entries are walked in ascending index order, and nested map entry order is unspecified. Collection values are yielded before their descendants.

Jump to

Keyboard shortcuts

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