podman
1package unstable2
3import (4"fmt"5"unsafe"6
7"github.com/pelletier/go-toml/v2/internal/danger"8)
9
10// Iterator over a sequence of nodes.
11//
12// Starts uninitialized, you need to call Next() first.
13//
14// For example:
15//
16// it := n.Children()
17// for it.Next() {
18// n := it.Node()
19// // do something with n
20// }
21type Iterator struct {22started bool23node *Node24}
25
26// Next moves the iterator forward and returns true if points to a
27// node, false otherwise.
28func (c *Iterator) Next() bool {29if !c.started {30c.started = true31} else if c.node.Valid() {32c.node = c.node.Next()33}34return c.node.Valid()35}
36
37// IsLast returns true if the current node of the iterator is the last
38// one. Subsequent calls to Next() will return false.
39func (c *Iterator) IsLast() bool {40return c.node.next == 041}
42
43// Node returns a pointer to the node pointed at by the iterator.
44func (c *Iterator) Node() *Node {45return c.node46}
47
48// Node in a TOML expression AST.
49//
50// Depending on Kind, its sequence of children should be interpreted
51// differently.
52//
53// - Array have one child per element in the array.
54// - InlineTable have one child per key-value in the table (each of kind
55// InlineTable).
56// - KeyValue have at least two children. The first one is the value. The rest
57// make a potentially dotted key.
58// - Table and ArrayTable's children represent a dotted key (same as
59// KeyValue, but without the first node being the value).
60//
61// When relevant, Raw describes the range of bytes this node is referring to in
62// the input document. Use Parser.Raw() to retrieve the actual bytes.
63type Node struct {64Kind Kind
65Raw Range // Raw bytes from the input.66Data []byte // Node value (either allocated or referencing the input).67
68// References to other nodes, as offsets in the backing array69// from this node. References can go backward, so those can be70// negative.71next int // 0 if last element72child int // 0 if no child73}
74
75// Range of bytes in the document.
76type Range struct {77Offset uint3278Length uint3279}
80
81// Next returns a pointer to the next node, or nil if there is no next node.
82func (n *Node) Next() *Node {83if n.next == 0 {84return nil85}86ptr := unsafe.Pointer(n)87size := unsafe.Sizeof(Node{})88return (*Node)(danger.Stride(ptr, size, n.next))89}
90
91// Child returns a pointer to the first child node of this node. Other children
92// can be accessed calling Next on the first child. Returns an nil if this Node
93// has no child.
94func (n *Node) Child() *Node {95if n.child == 0 {96return nil97}98ptr := unsafe.Pointer(n)99size := unsafe.Sizeof(Node{})100return (*Node)(danger.Stride(ptr, size, n.child))101}
102
103// Valid returns true if the node's kind is set (not to Invalid).
104func (n *Node) Valid() bool {105return n != nil106}
107
108// Key returns the children nodes making the Key on a supported node. Panics
109// otherwise. They are guaranteed to be all be of the Kind Key. A simple key
110// would return just one element.
111func (n *Node) Key() Iterator {112switch n.Kind {113case KeyValue:114value := n.Child()115if !value.Valid() {116panic(fmt.Errorf("KeyValue should have at least two children"))117}118return Iterator{node: value.Next()}119case Table, ArrayTable:120return Iterator{node: n.Child()}121default:122panic(fmt.Errorf("Key() is not supported on a %s", n.Kind))123}124}
125
126// Value returns a pointer to the value node of a KeyValue.
127// Guaranteed to be non-nil. Panics if not called on a KeyValue node,
128// or if the Children are malformed.
129func (n *Node) Value() *Node {130return n.Child()131}
132
133// Children returns an iterator over a node's children.
134func (n *Node) Children() Iterator {135return Iterator{node: n.Child()}136}
137