ir

package
v0.0.20 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package ir provides the intermediate representation for Tony format documents.

Tony documents are represented as trees of ir.Node values. Nodes can be: atomic (null, bool, number, string), composite (object, array), or metadata (comments, tags).

Creating Nodes

obj := ir.FromMap(map[string]*ir.Node{
    "name": ir.FromString("alice"),
    "age":  ir.FromInt(30),
})
arr := ir.FromSlice([]*ir.Node{ir.FromInt(1), ir.FromInt(2)})

Navigation

child, err := node.GetKPath("users[0].name")
path := node.KPath() // "users[0].name"

Node Types

The Type field indicates node type: NullType, BoolType, NumberType, StringType, ArrayType, ObjectType, CommentType.

Objects

For ObjectType, Fields[i] is the key for Values[i]. Field keys are either:

  • String nodes (normal object keys)
  • Int nodes fitting uint32 (sparse array keys)
  • Null nodes (merge keys, may repeat)

Objects have all int keys or all non-int keys (no mixing).

Numbers

Number values use Int64 (64-bit signed), Float64 (64-bit IEEE), or Number (string fallback).

Comments

CommentType nodes represent head comments (Values[0] is the commented node) or line comments (in the Comment field of another node).

Thread Safety

Nodes are not thread-safe. Synchronize access or clone for concurrent use.

  • github.com/signadot/tony-format/go-tony/parse - Parse text to IR
  • github.com/signadot/tony-format/go-tony/encode - Encode IR to text
  • github.com/signadot/tony-format/go-tony/schema - Schema validation

Package ir contains the Tony format implementation.

Index

Constants

View Source
const (
	IntKeysTag = "!sparsearray"
	IntKeysFmt = "%d"
	MergeKey   = "<<"
)

Variables

View Source
var (
	ErrParse     = errors.New("parse error")
	ErrBadFormat = format.ErrBadFormat
)

Functions

func CheckTag

func CheckTag(tag string) error

func Compare added in v0.0.7

func Compare(a, b *Node) int

Compare returns an integer comparing two nodes. The result will be 0 if a==b, -1 if a < b, and +1 if a > b.

func HeadTag

func HeadTag(tag string) (string, string)

func TagArgs

func TagArgs(tag string) (string, []string, string)

func TagCompose

func TagCompose(tag string, args []string, oTag string) string

func TagGet

func TagGet(tag, what string) (string, []string)

func TagHas

func TagHas(tag, what string) bool

TagHas: what should be ! prefixed

func TagRemove

func TagRemove(tag, what string) string

func ToMap

func ToMap(node *Node) map[string]*Node

func Truth

func Truth(node *Node) bool

Types

type KeyVal

type KeyVal struct {
	Key *Node
	Val *Node
}

type Node

type Node struct {
	Type        Type
	Parent      *Node
	ParentIndex int
	ParentField string
	Fields      []*Node
	Values      []*Node

	Tag     string
	Lines   []string
	Comment *Node

	String  string
	Bool    bool
	Number  string
	Float64 *float64
	Int64   *int64
}

func Comment added in v0.0.7

func Comment(n *Node, c string) *Node

func FromBool

func FromBool(v bool) *Node

func FromFloat

func FromFloat(f float64) *Node

func FromInt

func FromInt(v int64) *Node

func FromIntKeysMap

func FromIntKeysMap(yMap map[uint32]*Node) *Node

func FromIntKeysMapAt

func FromIntKeysMapAt(res *Node, yMap map[uint32]*Node) *Node

func FromKeyVals

func FromKeyVals(kvs []KeyVal) *Node

func FromKeyValsAt

func FromKeyValsAt(res *Node, kvs []KeyVal) *Node

func FromMap

func FromMap(yMap map[string]*Node) *Node

func FromSlice

func FromSlice(ySlice []*Node) *Node

func FromString

func FromString(v string) *Node

func FromStringAt

func FromStringAt(p *Node, v string) *Node

func Get

func Get(y *Node, field string) *Node

func Null

func Null() *Node

func (*Node) Clone

func (y *Node) Clone() *Node

func (*Node) CloneTo

func (y *Node) CloneTo(dst *Node) *Node

func (*Node) DeepEqual added in v0.0.9

func (y *Node) DeepEqual(other *Node) bool

DeepEqual reports whether two nodes are deeply equal. It compares all data fields recursively, but does not compare Parent, ParentIndex, or ParentField as these are structural metadata.

func (*Node) FromTonyIR added in v0.0.6

func (node *Node) FromTonyIR(o *Node) error

func (*Node) GetKPath added in v0.0.10

func (node *Node) GetKPath(kp string) (*Node, error)

GetKPath navigates an ir.Node tree using a kinded path. Similar to GetPath() but uses kinded path syntax.

Example:

rootNode.GetKPath("a.b.c") navigates to rootNode.Values["a"].Values["b"].Values["c"]

Returns an error if the path doesn't exist or is invalid.

func (*Node) GetPath

func (y *Node) GetPath(yPath string) (*Node, error)

func (*Node) Hash added in v0.0.7

func (n *Node) Hash() uint64

Hash returns a 64-bit hash of the node. Hash includes comments It panics if n is nil.

func (*Node) KPath added in v0.0.10

func (node *Node) KPath() string

KPath returns the kinded path string representation of this node's position in the tree. Similar to Path() but returns kinded path syntax (e.g., "a.b[0]" instead of "$.a.b[0]").

Examples:

  • Root node → ""
  • Object field "a" → "a"
  • Array element at index 0 → "[0]"
  • Nested object "a.b" → "a.b"
  • Mixed "a[0].b" → "a[0].b"

func (*Node) ListKPath added in v0.0.10

func (node *Node) ListKPath(dst []*Node, kp string) ([]*Node, error)

ListKPath traverses an ir.Node tree and collects all nodes matching a kinded path. Similar to ListPath() but uses kinded path syntax.

Returns a slice of matching nodes.

func (*Node) ListPath

func (y *Node) ListPath(dst []*Node, yPath string) ([]*Node, error)

func (*Node) MarshalJSON

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

func (*Node) NonCommentParent

func (y *Node) NonCommentParent() *Node

func (*Node) Path

func (y *Node) Path() string

func (*Node) RemoveComments added in v0.0.7

func (y *Node) RemoveComments()

RemoveComments removes all comment nodes from the tree recursively. It removes: - Comment nodes (Type == CommentType) - Comment fields from all nodes (sets Comment to nil)

func (*Node) Root

func (y *Node) Root() *Node

func (*Node) ToIntKeysMap

func (y *Node) ToIntKeysMap() (map[uint32]*Node, error)

func (*Node) ToTonyIR added in v0.0.6

func (node *Node) ToTonyIR() (*Node, error)

func (*Node) UnmarshalJSON

func (y *Node) UnmarshalJSON(d []byte) error

func (*Node) Visit

func (y *Node) Visit(f func(y *Node, isPost bool) (bool, error)) error

func (*Node) WithTag

func (y *Node) WithTag(tag string) *Node

type Path

type Path struct {
	IndexAll bool
	Index    *int
	Field    *string
	Subtree  bool
	Next     *Path
}

func ParsePath

func ParsePath(p string) (*Path, error)

func (*Path) String

func (p *Path) String() string

type Type

type Type int
const (
	NullType Type = iota
	NumberType
	StringType
	BoolType
	ObjectType
	ArrayType
	CommentType
)

func Types

func Types() []Type

func (Type) IsLeaf

func (t Type) IsLeaf() bool

func (Type) MarshalText

func (t Type) MarshalText() ([]byte, error)

func (Type) String

func (t Type) String() string

func (*Type) UnmarshalText

func (t *Type) UnmarshalText(d []byte) error

Directories

Path Synopsis
Package kpath provides kinded path parsing and navigation.
Package kpath provides kinded path parsing and navigation.

Jump to

Keyboard shortcuts

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