Skip to content

Commit 3d2ca20

Browse files
committed
DOM: Add methods to convert DOM constructs to vanilla Go types.
- Container => asMap() returns map[string]interface{} - List => AsSlice() returns []interface{} - Leaf => Value() returns interface{} Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
1 parent 79d1ebb commit 3d2ca20

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

dom/container.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ func (c *containerImpl) Flatten() map[string]Leaf {
7373
return ret
7474
}
7575

76+
func (c *containerImpl) AsMap() map[string]interface{} {
77+
return encodeContainerFn(c)
78+
}
79+
7680
func (c *containerImpl) Equals(node Node) bool {
7781
if node == nil || !node.IsContainer() {
7882
return false

dom/container_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ func TestLookup(t *testing.T) {
120120
assert.Equal(t, "leaf1", doc.Lookup("level1.level2a.level3a").(Leaf).Value())
121121
}
122122

123+
func TestContainerAsMap(t *testing.T) {
124+
data, err := os.ReadFile("../testdata/doc1.yaml")
125+
assert.Nil(t, err)
126+
doc, err := b.FromReader(bytes.NewReader(data), DefaultYamlDecoder)
127+
assert.Nil(t, err)
128+
fm := doc.AsMap()
129+
assert.Equal(t, 1, len(fm))
130+
assert.NotNil(t, fm["level1"])
131+
}
132+
123133
func TestFlatten(t *testing.T) {
124134
data, err := os.ReadFile("../testdata/doc1.yaml")
125135
assert.Nil(t, err)

dom/list.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func (l *listImpl) Items() []Node {
3333
return c
3434
}
3535

36+
func (l *listImpl) AsSlice() []interface{} {
37+
return encoderListFn(l)
38+
}
39+
3640
func (l *listImpl) Size() int {
3741
return len(l.items)
3842
}

dom/list_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,12 @@ func TestListClone(t *testing.T) {
9898
assert.Equal(t, l2.Items()[0], l.Items()[0])
9999
assert.Equal(t, l2.Items()[1], l.Items()[1])
100100
}
101+
102+
func TestListAsSlice(t *testing.T) {
103+
l := ListNode(LeafNode(1), LeafNode(2), LeafNode(3))
104+
l2 := l.AsSlice()
105+
assert.Equal(t, 3, len(l2))
106+
assert.Equal(t, 1, l2[0])
107+
assert.Equal(t, 2, l2[1])
108+
assert.Equal(t, 3, l2[2])
109+
}

dom/types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ type List interface {
119119
Size() int
120120
// Items returns copy of slice of all nodes in this list
121121
Items() []Node
122+
123+
// AsSlice converts recursively content of this List into []interface{}.
124+
// Result consists from Go vanilla constructs only.
125+
AsSlice() []interface{}
122126
}
123127

124128
// NodeList is sequence of zero or more Nodes
@@ -136,6 +140,10 @@ type Container interface {
136140
Lookup(path string) Node
137141
// Flatten flattens this Container into list of leaves
138142
Flatten() map[string]Leaf
143+
144+
// AsMap converts recursively content of this container into map[string]interface{}
145+
// Result consists from Go vanilla constructs only and thus could be directly used in Go templates.
146+
AsMap() map[string]interface{}
139147
// Search finds all paths where Node's value is equal to given value according to provided SearchValueFunc.
140148
// If no match is found, nil is returned.
141149
Search(fn SearchValueFunc) []string

0 commit comments

Comments
 (0)