Skip to content

Commit 8012471

Browse files
committed
generate util package, modify test file package name, add new methods to util directory file
1 parent 5a956e1 commit 8012471

3 files changed

Lines changed: 80 additions & 70 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.go

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@ package simpleyaml
3131

3232
import (
3333
"errors"
34-
"strconv"
3534
"gopkg.in/yaml.v2"
36-
"bytes"
3735
)
3836

3937
type Yaml struct {
4038
data interface{}
4139
}
42-
var array_of_paths = make([]string, 0)
40+
4341
// NewYaml returns a pointer to a new `Yaml` object after unmarshaling `body` bytes
4442
func NewYaml(body []byte) (*Yaml, error) {
4543
var val interface{}
@@ -182,55 +180,3 @@ func (y *Yaml) GetMapKeys() ([]string, error) {
182180
}
183181
return keys, nil
184182
}
185-
186-
// GetAllPaths retrieves all possible paths in the YAML file
187-
//
188-
// Example:
189-
// y.GetAllPaths(yaml_file, array_of_strings)
190-
func GetAllExistingPaths(y *Yaml, path_slice []string) []string {
191-
if y.IsMap() {
192-
keys, err := y.GetMapKeys()
193-
if err != nil {
194-
panic(err)
195-
}
196-
for k, _ := range keys {
197-
if k != 0 {
198-
path_slice = path_slice[:len(path_slice)-1]
199-
}
200-
path_slice = append(path_slice, keys[k])
201-
GetAllExistingPaths(y.Get(keys[k]), path_slice)
202-
}
203-
} else if y.IsArray() {
204-
arr, err := y.Array()
205-
if err != nil {
206-
panic(err)
207-
}
208-
for k, _ := range arr {
209-
if k != 0 {
210-
path_slice = path_slice[:len(path_slice)-1]
211-
}
212-
path_slice = append(path_slice, strconv.Itoa(k))
213-
GetAllExistingPaths(y.GetIndex(k), path_slice)
214-
}
215-
} else {
216-
var buffer bytes.Buffer
217-
for k, _ := range path_slice {
218-
if k == len(path_slice)-1 {
219-
buffer.WriteString(path_slice[k])
220-
}else{
221-
buffer.WriteString(path_slice[k]+".")
222-
}
223-
}
224-
array_of_paths = append(array_of_paths, buffer.String())
225-
}
226-
return array_of_paths
227-
}
228-
229-
// Helper function to invoke GetAllExistingPaths
230-
func (y *Yaml) GetAllPaths() []string {
231-
yin := y
232-
all_paths := make([]string, 0)
233-
initial_path := make([]string, 0)
234-
all_paths = GetAllExistingPaths(yin, initial_path)
235-
return all_paths
236-
}

simpleyaml_test.go

Lines changed: 17 additions & 15 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
}
@@ -142,14 +144,14 @@ func TestGetPath(t *testing.T) {
142144
}
143145

144146
func TestGetAllPaths(t *testing.T) {
145-
y, err := NewYaml(data)
147+
y, err := simpleyaml.NewYaml(data)
146148
if err != nil {
147149
t.Fatal("init yaml failed")
148150
}
149151

150-
v := y.GetAllPaths()
152+
v, err := util.GetAllPaths(y)
151153
if err != nil {
152-
t.Fatal("get yaml failed")
154+
t.Fatal("Getting all paths failed")
153155
}
154156

155157
t.Log(v)
@@ -159,7 +161,7 @@ func TestGetAllPaths(t *testing.T) {
159161
}
160162

161163
func TestArray(t *testing.T) {
162-
y, err := NewYaml(data)
164+
y, err := simpleyaml.NewYaml(data)
163165
if err != nil {
164166
t.Fatal("init yaml failed")
165167
}
@@ -174,7 +176,7 @@ func TestArray(t *testing.T) {
174176
}
175177

176178
func TestMap(t *testing.T) {
177-
y, err := NewYaml(data)
179+
y, err := simpleyaml.NewYaml(data)
178180
if err != nil {
179181
t.Fatal("init yaml failed")
180182
}
@@ -192,7 +194,7 @@ func TestMap(t *testing.T) {
192194
}
193195

194196
func TestIsFound(t *testing.T) {
195-
y, err := NewYaml(data)
197+
y, err := simpleyaml.NewYaml(data)
196198
if err != nil {
197199
t.Fatal("init yaml failed")
198200
}

0 commit comments

Comments
 (0)