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.
Related Packages ¶
- 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
- Variables
- func CheckTag(tag string) error
- func Compare(a, b *Node) int
- func HeadTag(tag string) (string, string)
- func TagArgs(tag string) (string, []string, string)
- func TagCompose(tag string, args []string, oTag string) string
- func TagGet(tag, what string) (string, []string)
- func TagHas(tag, what string) bool
- func TagRemove(tag, what string) string
- func ToMap(node *Node) map[string]*Node
- func Truth(node *Node) bool
- type KeyVal
- type Node
- func Comment(n *Node, c string) *Node
- func FromBool(v bool) *Node
- func FromFloat(f float64) *Node
- func FromInt(v int64) *Node
- func FromIntKeysMap(yMap map[uint32]*Node) *Node
- func FromIntKeysMapAt(res *Node, yMap map[uint32]*Node) *Node
- func FromKeyVals(kvs []KeyVal) *Node
- func FromKeyValsAt(res *Node, kvs []KeyVal) *Node
- func FromMap(yMap map[string]*Node) *Node
- func FromSlice(ySlice []*Node) *Node
- func FromString(v string) *Node
- func FromStringAt(p *Node, v string) *Node
- func Get(y *Node, field string) *Node
- func Null() *Node
- func (y *Node) Clone() *Node
- func (y *Node) CloneTo(dst *Node) *Node
- func (y *Node) DeepEqual(other *Node) bool
- func (node *Node) FromTonyIR(o *Node) error
- func (node *Node) GetKPath(kp string) (*Node, error)
- func (y *Node) GetPath(yPath string) (*Node, error)
- func (n *Node) Hash() uint64
- func (node *Node) KPath() string
- func (node *Node) ListKPath(dst []*Node, kp string) ([]*Node, error)
- func (y *Node) ListPath(dst []*Node, yPath string) ([]*Node, error)
- func (y *Node) MarshalJSON() ([]byte, error)
- func (y *Node) NonCommentParent() *Node
- func (y *Node) Path() string
- func (y *Node) RemoveComments()
- func (y *Node) Root() *Node
- func (y *Node) ToIntKeysMap() (map[uint32]*Node, error)
- func (node *Node) ToTonyIR() (*Node, error)
- func (y *Node) UnmarshalJSON(d []byte) error
- func (y *Node) Visit(f func(y *Node, isPost bool) (bool, error)) error
- func (y *Node) WithTag(tag string) *Node
- type Path
- type Type
Constants ¶
const ( IntKeysTag = "!sparsearray" IntKeysFmt = "%d" MergeKey = "<<" )
Variables ¶
var ( ErrParse = errors.New("parse error") ErrBadFormat = format.ErrBadFormat )
Functions ¶
Types ¶
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 FromIntKeysMap ¶
func FromKeyVals ¶
func FromKeyValsAt ¶
func FromString ¶
func FromStringAt ¶
func (*Node) DeepEqual ¶ added in v0.0.9
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) GetKPath ¶ added in v0.0.10
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) Hash ¶ added in v0.0.7
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
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
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) MarshalJSON ¶
func (*Node) NonCommentParent ¶
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)