63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
// Package document defines ConfigKit's parser-independent configuration tree.
|
|
package document
|
|
|
|
// Kind identifies a configuration node category.
|
|
type Kind uint8
|
|
|
|
const (
|
|
Mapping Kind = iota + 1
|
|
Sequence
|
|
String
|
|
Boolean
|
|
Number
|
|
Null
|
|
)
|
|
|
|
// Location identifies a source coordinate. Root depth and unavailable coordinates
|
|
// are represented independently by callers; line and column are one-based when set.
|
|
type Location struct {
|
|
SourceID string
|
|
Line int
|
|
Column int
|
|
}
|
|
|
|
// Entry is an ordered mapping entry with separate key and value locations.
|
|
type Entry struct {
|
|
Key string
|
|
KeyLocation Location
|
|
ValueLocation Location
|
|
Value *Node
|
|
}
|
|
|
|
// Node is ConfigKit's owned configuration representation. Number Text retains its
|
|
// exact validated decimal spelling; Boolean Text is "true" or "false".
|
|
type Node struct {
|
|
Kind Kind
|
|
Location Location
|
|
Text string
|
|
Mapping []Entry
|
|
Sequence []*Node
|
|
}
|
|
|
|
// Clone returns a detached copy of a node tree.
|
|
func Clone(node *Node) *Node {
|
|
if node == nil {
|
|
return nil
|
|
}
|
|
cloned := *node
|
|
if node.Mapping != nil {
|
|
cloned.Mapping = make([]Entry, len(node.Mapping))
|
|
for i, entry := range node.Mapping {
|
|
cloned.Mapping[i] = entry
|
|
cloned.Mapping[i].Value = Clone(entry.Value)
|
|
}
|
|
}
|
|
if node.Sequence != nil {
|
|
cloned.Sequence = make([]*Node, len(node.Sequence))
|
|
for i, child := range node.Sequence {
|
|
cloned.Sequence[i] = Clone(child)
|
|
}
|
|
}
|
|
return &cloned
|
|
}
|