Skip to content

Commit a320310

Browse files
authored
Merge pull request #8 from qu1queee/getallpaths
adding recursive method to retrieve all paths from a yaml file
2 parents 116d8e5 + 8012471 commit a320310

2 files changed

Lines changed: 93 additions & 12 deletions

File tree

helper/util/get_paths.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package util
2+
3+
import (
4+
"errors"
5+
"github.com/smallfish/simpleyaml"
6+
"strconv"
7+
"bytes"
8+
)
9+
10+
var ArrayOfPaths = make([]string, 0)
11+
12+
func GetAllExistingPaths(y *simpleyaml.Yaml, PathSlice []string) ([]string, error) {
13+
if y.IsMap() {
14+
keys, err := y.GetMapKeys()
15+
if err != nil {
16+
return nil, errors.New("Retrieving map keys failed")
17+
}
18+
for k, _ := range keys {
19+
if k != 0 {
20+
PathSlice = PathSlice[:len(PathSlice)-1]
21+
}
22+
PathSlice = append(PathSlice, keys[k])
23+
GetAllExistingPaths(y.Get(keys[k]), PathSlice)
24+
}
25+
} else if y.IsArray() {
26+
arr, err := y.Array()
27+
if err != nil {
28+
return nil, errors.New("Retrieving array failed")
29+
}
30+
for k, _ := range arr {
31+
if k != 0 {
32+
PathSlice = PathSlice[:len(PathSlice)-1]
33+
}
34+
PathSlice = append(PathSlice, strconv.Itoa(k))
35+
GetAllExistingPaths(y.GetIndex(k), PathSlice)
36+
}
37+
} else {
38+
var buffer bytes.Buffer
39+
for k, _ := range PathSlice {
40+
if k == len(PathSlice)-1 {
41+
buffer.WriteString(PathSlice[k])
42+
}else{
43+
buffer.WriteString(PathSlice[k]+"/")
44+
}
45+
}
46+
ArrayOfPaths = append(ArrayOfPaths, buffer.String())
47+
}
48+
return ArrayOfPaths, nil
49+
}
50+
51+
// GetAllPaths retrieves all possible paths in the YAML file
52+
//
53+
// Example:
54+
// util.GetAllPaths(*Yaml)
55+
func GetAllPaths(y *simpleyaml.Yaml) ([]string, error) {
56+
InitialPath := make([]string, 0)
57+
AllPaths, err := GetAllExistingPaths(y, InitialPath)
58+
if err != nil {
59+
return nil, errors.New("Retrieving paths failed")
60+
}
61+
return AllPaths, nil
62+
}

simpleyaml_test.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
package simpleyaml
1+
package simpleyaml_test
22

33
import (
44
"testing"
5+
"github.com/smallfish/simpleyaml"
6+
"github.com/smallfish/simpleyaml/helper/util"
57
)
68

79
var data = []byte(`
@@ -23,7 +25,7 @@ bb:
2325
`)
2426

2527
func TestBool(t *testing.T) {
26-
y, err := NewYaml(data)
28+
y, err := simpleyaml.NewYaml(data)
2729
if err != nil {
2830
t.Fatal("init yaml failed")
2931
}
@@ -38,7 +40,7 @@ func TestBool(t *testing.T) {
3840
}
3941

4042
func TestString(t *testing.T) {
41-
y, err := NewYaml(data)
43+
y, err := simpleyaml.NewYaml(data)
4244
if err != nil {
4345
t.Fatal("init yaml failed")
4446
}
@@ -53,7 +55,7 @@ func TestString(t *testing.T) {
5355
}
5456

5557
func TestStringFromIntKey(t *testing.T) {
56-
y, err := NewYaml(data)
58+
y, err := simpleyaml.NewYaml(data)
5759
if err != nil {
5860
t.Fatal("init yaml failed")
5961
}
@@ -69,7 +71,7 @@ func TestStringFromIntKey(t *testing.T) {
6971
}
7072

7173
func TestFloat(t *testing.T) {
72-
y, err := NewYaml(data)
74+
y, err := simpleyaml.NewYaml(data)
7375
if err != nil {
7476
t.Fatal("init yaml failed")
7577
}
@@ -85,7 +87,7 @@ func TestFloat(t *testing.T) {
8587
}
8688

8789
func TestInt(t *testing.T) {
88-
y, err := NewYaml(data)
90+
y, err := simpleyaml.NewYaml(data)
8991
if err != nil {
9092
t.Fatal("init yaml failed")
9193
}
@@ -100,7 +102,7 @@ func TestInt(t *testing.T) {
100102
}
101103

102104
func TestGetIndex(t *testing.T) {
103-
y, err := NewYaml(data)
105+
y, err := simpleyaml.NewYaml(data)
104106
if err != nil {
105107
t.Fatal("init yaml failed")
106108
}
@@ -112,7 +114,7 @@ func TestGetIndex(t *testing.T) {
112114
}
113115

114116
func TestString2(t *testing.T) {
115-
y, err := NewYaml(data)
117+
y, err := simpleyaml.NewYaml(data)
116118
if err != nil {
117119
t.Fatal("init yaml failed")
118120
}
@@ -127,7 +129,7 @@ func TestString2(t *testing.T) {
127129
}
128130

129131
func TestGetPath(t *testing.T) {
130-
y, err := NewYaml(data)
132+
y, err := simpleyaml.NewYaml(data)
131133
if err != nil {
132134
t.Fatal("init yaml failed")
133135
}
@@ -141,8 +143,25 @@ func TestGetPath(t *testing.T) {
141143
}
142144
}
143145

146+
func TestGetAllPaths(t *testing.T) {
147+
y, err := simpleyaml.NewYaml(data)
148+
if err != nil {
149+
t.Fatal("init yaml failed")
150+
}
151+
152+
v, err := util.GetAllPaths(y)
153+
if err != nil {
154+
t.Fatal("Getting all paths failed")
155+
}
156+
157+
t.Log(v)
158+
if len(v) != 10 {
159+
t.Fatal("Number of paths do not match number or real paths.")
160+
}
161+
}
162+
144163
func TestArray(t *testing.T) {
145-
y, err := NewYaml(data)
164+
y, err := simpleyaml.NewYaml(data)
146165
if err != nil {
147166
t.Fatal("init yaml failed")
148167
}
@@ -157,7 +176,7 @@ func TestArray(t *testing.T) {
157176
}
158177

159178
func TestMap(t *testing.T) {
160-
y, err := NewYaml(data)
179+
y, err := simpleyaml.NewYaml(data)
161180
if err != nil {
162181
t.Fatal("init yaml failed")
163182
}
@@ -175,7 +194,7 @@ func TestMap(t *testing.T) {
175194
}
176195

177196
func TestIsFound(t *testing.T) {
178-
y, err := NewYaml(data)
197+
y, err := simpleyaml.NewYaml(data)
179198
if err != nil {
180199
t.Fatal("init yaml failed")
181200
}

0 commit comments

Comments
 (0)