File tree Expand file tree Collapse file tree 5 files changed +35
-0
lines changed Expand file tree Collapse file tree 5 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -73,6 +73,10 @@ func (c *containerImpl) Flatten() map[string]Leaf {
73
73
return ret
74
74
}
75
75
76
+ func (c * containerImpl ) AsMap () map [string ]interface {} {
77
+ return encodeContainerFn (c )
78
+ }
79
+
76
80
func (c * containerImpl ) Equals (node Node ) bool {
77
81
if node == nil || ! node .IsContainer () {
78
82
return false
Original file line number Diff line number Diff line change @@ -120,6 +120,16 @@ func TestLookup(t *testing.T) {
120
120
assert .Equal (t , "leaf1" , doc .Lookup ("level1.level2a.level3a" ).(Leaf ).Value ())
121
121
}
122
122
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
+
123
133
func TestFlatten (t * testing.T ) {
124
134
data , err := os .ReadFile ("../testdata/doc1.yaml" )
125
135
assert .Nil (t , err )
Original file line number Diff line number Diff line change @@ -33,6 +33,10 @@ func (l *listImpl) Items() []Node {
33
33
return c
34
34
}
35
35
36
+ func (l * listImpl ) AsSlice () []interface {} {
37
+ return encoderListFn (l )
38
+ }
39
+
36
40
func (l * listImpl ) Size () int {
37
41
return len (l .items )
38
42
}
Original file line number Diff line number Diff line change @@ -98,3 +98,12 @@ func TestListClone(t *testing.T) {
98
98
assert .Equal (t , l2 .Items ()[0 ], l .Items ()[0 ])
99
99
assert .Equal (t , l2 .Items ()[1 ], l .Items ()[1 ])
100
100
}
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
+ }
Original file line number Diff line number Diff line change @@ -119,6 +119,10 @@ type List interface {
119
119
Size () int
120
120
// Items returns copy of slice of all nodes in this list
121
121
Items () []Node
122
+
123
+ // AsSlice converts recursively content of this List into []interface{}.
124
+ // Result consists from Go vanilla constructs only.
125
+ AsSlice () []interface {}
122
126
}
123
127
124
128
// NodeList is sequence of zero or more Nodes
@@ -136,6 +140,10 @@ type Container interface {
136
140
Lookup (path string ) Node
137
141
// Flatten flattens this Container into list of leaves
138
142
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 {}
139
147
// Search finds all paths where Node's value is equal to given value according to provided SearchValueFunc.
140
148
// If no match is found, nil is returned.
141
149
Search (fn SearchValueFunc ) []string
You can’t perform that action at this time.
0 commit comments