Documentation
¶
Overview ¶
Package dom provides live structural operations over go-yaml v4 representation nodes.
The package works directly with yaml.Node values from go.yaml.in/yaml/v4. Merge and Update mutate live node graphs in place. FindNodes and FindNode return live interior pointers. Clone returns detached deep copies, and Equal compares node content while ignoring presentation details such as comments and style.
Merge mutates the destination:
var base, overlay yaml.Node
_ = yaml.Unmarshal([]byte("a: 1\n"), &base)
_ = yaml.Unmarshal([]byte("b: 2\n"), &overlay)
err := dom.Merge(&base, &overlay)
Clone first for non-destructive merge:
merged := dom.Clone(&base) err := dom.Merge(merged, &overlay)
FindNodes and Update compose for in-place edits:
nodes := dom.FindNodes(doc, func(n *yaml.Node) bool {
return n.Kind == yaml.ScalarNode && n.Value == "old"
})
err := dom.Update(nodes, func(n *yaml.Node) error {
n.Value = "new"
return nil
})
Use github.com/yaml/go-yaml-yq when you want yq's expression language. The two packages compose only through *yaml.Node; neither imports the other.
Index ¶
- func Clone(node *yaml.Node) *yaml.Node
- func Equal(a, b *yaml.Node) bool
- func FindNode(root *yaml.Node, pred func(*yaml.Node) bool) (*yaml.Node, error)
- func FindNodes(root *yaml.Node, pred func(*yaml.Node) bool) []*yaml.Node
- func Merge(dst, src *yaml.Node, opts ...MergeOption) error
- func Update(nodes []*yaml.Node, fn func(*yaml.Node) error) error
- type KeyFilter
- type MergeOption
- func WithAppendSequences() MergeOption
- func WithClobberTags(enable ...bool) MergeOption
- func WithKeyFilter(f KeyFilter) MergeOption
- func WithNullMerge(s NullStrategy) MergeOption
- func WithOnlyExistingKeys() MergeOption
- func WithOnlyNewKeys() MergeOption
- func WithSequenceMerge(s SequenceStrategy) MergeOption
- func WithSharedSource(enable ...bool) MergeOption
- type NullStrategy
- type SequenceStrategy
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clone ¶
Clone returns an independent deep copy. Aliases in the clone point at cloned anchor nodes, not the original graph.
Example ¶
var base, overlay yaml.Node
_ = yaml.Unmarshal([]byte("a: 1\n"), &base)
_ = yaml.Unmarshal([]byte("b: 2\n"), &overlay)
merged := dom.Clone(&base)
_ = dom.Merge(merged, &overlay)
fmt.Println(dom.Equal(&base, merged))
Output: false
func Equal ¶
Equal reports deep content equality. It compares kind, tag, value, children, and alias targets, while ignoring style, comments, and source positions.
func FindNodes ¶
FindNodes walks root in pre-order and returns live pointers to nodes for which pred returns true. Document nodes are traversed but not passed to pred. Alias targets are not followed.
func Merge ¶
func Merge(dst, src *yaml.Node, opts ...MergeOption) error
Merge deep-merges src into dst in place. dst is mutated; src is not unless WithSharedSource is set, in which case src subtrees may be grafted directly.
Example ¶
var base, overlay yaml.Node
_ = yaml.Unmarshal([]byte("items: [a]\nmeta:\n env: dev\n"), &base)
_ = yaml.Unmarshal([]byte("items: [b]\nmeta:\n version: v1\n"), &overlay)
_ = dom.Merge(&base, &overlay, dom.WithAppendSequences())
out, _ := yaml.Marshal(&base)
fmt.Print(string(out))
Output: items: [a, b] meta: env: dev version: v1
Types ¶
type MergeOption ¶
type MergeOption func(*mergeConfig)
func WithAppendSequences ¶
func WithAppendSequences() MergeOption
func WithClobberTags ¶
func WithClobberTags(enable ...bool) MergeOption
func WithKeyFilter ¶
func WithKeyFilter(f KeyFilter) MergeOption
func WithNullMerge ¶
func WithNullMerge(s NullStrategy) MergeOption
func WithOnlyExistingKeys ¶
func WithOnlyExistingKeys() MergeOption
func WithOnlyNewKeys ¶
func WithOnlyNewKeys() MergeOption
func WithSequenceMerge ¶
func WithSequenceMerge(s SequenceStrategy) MergeOption
func WithSharedSource ¶
func WithSharedSource(enable ...bool) MergeOption
type NullStrategy ¶
type NullStrategy int
const ( NullOverwrite NullStrategy = iota NullIgnore NullDelete )
type SequenceStrategy ¶
type SequenceStrategy int
const ( SequenceReplace SequenceStrategy = iota SequenceAppend SequenceByIndex )
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
append-sequences
command
|
|
|
basic-merge
command
|
|
|
clone-merge
command
|
|
|
find-update
command
|