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

node

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Annotation

type Annotation struct {
	Key   AnnotationKey
	Value any
}

Annotation is a single piece of derived data attached to a node by a lens.

type AnnotationKey

type AnnotationKey struct {
	Lens string // e.g. "stats", "merkle", "decode"
	Name string // e.g. "mean", "hash", "type"
}

AnnotationKey namespaces annotations to prevent collisions between lenses.

type Kind

type Kind uint8

Kind represents the JSON value type of a node.

const (
	KindNull Kind = iota
	KindBool
	KindNumber
	KindString
	KindArray
	KindObject
)

func (Kind) String

func (k Kind) String() string

type Node

type Node struct {
	Kind  Kind
	Key   string // object key if this is an object value; "" otherwise
	Index int    // array index if this is an array element; -1 otherwise
	Raw   string // raw JSON token for leaf nodes (e.g. `"hello"`, `42`, `true`, `null`)

	Children []*Node
	Parent   *Node
	// contains filtered or unexported fields
}

Node is the core data structure. Parse once, annotate many times. All lenses read from the same tree and write their own annotation keys.

func (*Node) Annotate

func (n *Node) Annotate(a Annotation)

Annotate stamps an annotation onto this node. Safe for concurrent use.

func (*Node) CountNodes

func (n *Node) CountNodes() int

CountNodes returns the total number of nodes in this subtree (including self).

func (*Node) GetAnnotation

func (n *Node) GetAnnotation(lens, name string) (any, bool)

GetAnnotation returns the value for a specific lens+name key.

func (*Node) GetAnnotations

func (n *Node) GetAnnotations(lens string) []Annotation

GetAnnotations returns all annotations for a given lens.

func (*Node) MarshalJSON

func (n *Node) MarshalJSON() ([]byte, error)

MarshalJSON produces compact JSON for this node, preserving key order.

func (*Node) Path

func (n *Node) Path() string

Path returns the dot-notation path from root to this node (e.g. ".users[0].name").

func (*Node) ToAny

func (n *Node) ToAny() any

ToAny converts the node back to a Go any value suitable for json.Marshal.

func (*Node) Walk

func (n *Node) Walk(fn func(*Node) bool)

Walk visits every node depth-first pre-order (parent before children). Return false from fn to skip the subtree rooted at that node.

func (*Node) WalkPost

func (n *Node) WalkPost(fn func(*Node))

WalkPost visits every node depth-first post-order (children before parent). Required by merkle hashing, where children must be hashed before their parent.

Jump to

Keyboard shortcuts

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