Skip to content

Commit 3b1b5a3

Browse files
smallfishsmallfish
authored andcommitted
upgrade with go mod
1 parent a320310 commit 3b1b5a3

6 files changed

Lines changed: 143 additions & 145 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/smallfish/simpleyaml
2+
3+
go 1.16
4+
5+
require (
6+
github.com/stretchr/testify v1.7.0 // indirect
7+
gopkg.in/yaml.v2 v2.4.0
8+
)

go.sum

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
7+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
11+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
12+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
13+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

helper/util/get_paths.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
package util
22

33
import (
4+
"bytes"
45
"errors"
6+
"strconv"
7+
58
"github.com/smallfish/simpleyaml"
6-
"strconv"
7-
"bytes"
89
)
910

10-
var ArrayOfPaths = make([]string, 0)
11+
var (
12+
ArrayOfPaths = make([]string, 0)
13+
)
1114

1215
func GetAllExistingPaths(y *simpleyaml.Yaml, PathSlice []string) ([]string, error) {
1316
if y.IsMap() {
1417
keys, err := y.GetMapKeys()
1518
if err != nil {
1619
return nil, errors.New("Retrieving map keys failed")
1720
}
21+
1822
for k, _ := range keys {
1923
if k != 0 {
2024
PathSlice = PathSlice[:len(PathSlice)-1]
2125
}
26+
2227
PathSlice = append(PathSlice, keys[k])
2328
GetAllExistingPaths(y.Get(keys[k]), PathSlice)
2429
}
@@ -27,36 +32,41 @@ func GetAllExistingPaths(y *simpleyaml.Yaml, PathSlice []string) ([]string, erro
2732
if err != nil {
2833
return nil, errors.New("Retrieving array failed")
2934
}
35+
3036
for k, _ := range arr {
3137
if k != 0 {
3238
PathSlice = PathSlice[:len(PathSlice)-1]
3339
}
40+
3441
PathSlice = append(PathSlice, strconv.Itoa(k))
3542
GetAllExistingPaths(y.GetIndex(k), PathSlice)
3643
}
3744
} else {
3845
var buffer bytes.Buffer
3946
for k, _ := range PathSlice {
40-
if k == len(PathSlice)-1 {
41-
buffer.WriteString(PathSlice[k])
42-
}else{
43-
buffer.WriteString(PathSlice[k]+"/")
44-
}
47+
if k == len(PathSlice)-1 {
48+
buffer.WriteString(PathSlice[k])
49+
} else {
50+
buffer.WriteString(PathSlice[k] + "/")
51+
}
4552
}
53+
4654
ArrayOfPaths = append(ArrayOfPaths, buffer.String())
4755
}
56+
4857
return ArrayOfPaths, nil
4958
}
5059

5160
// GetAllPaths retrieves all possible paths in the YAML file
5261
//
5362
// Example:
5463
// util.GetAllPaths(*Yaml)
55-
func GetAllPaths(y *simpleyaml.Yaml) ([]string, error) {
64+
func GetAllPaths(y *simpleyaml.Yaml) ([]string, error) {
5665
InitialPath := make([]string, 0)
57-
AllPaths, err := GetAllExistingPaths(y, InitialPath)
66+
AllPaths, err := GetAllExistingPaths(y, InitialPath)
5867
if err != nil {
5968
return nil, errors.New("Retrieving paths failed")
6069
}
70+
6171
return AllPaths, nil
6272
}

simpleyaml.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// a Go package to interact with arbitrary YAML.
1+
// Package simpleyaml: a Go package to interact with arbitrary YAML.
22
//
33
// Example:
44
// var data = []byte(`
@@ -27,10 +27,12 @@
2727
// // y.Get("bb").Get("cc").Get("dd").Array()
2828
// // y.Get("bb").Get("cc").Get("dd").GetIndex(1).Int()
2929
// // y.GetPath("bb", "cc", "ee").String()
30+
3031
package simpleyaml
3132

3233
import (
3334
"errors"
35+
3436
"gopkg.in/yaml.v2"
3537
)
3638

@@ -41,19 +43,16 @@ type Yaml struct {
4143
// NewYaml returns a pointer to a new `Yaml` object after unmarshaling `body` bytes
4244
func NewYaml(body []byte) (*Yaml, error) {
4345
var val interface{}
44-
err := yaml.Unmarshal(body, &val)
45-
if err != nil {
46+
if err := yaml.Unmarshal(body, &val); err != nil {
4647
return nil, errors.New("unmarshal []byte to yaml failed: " + err.Error())
4748
}
49+
4850
return &Yaml{val}, nil
4951
}
5052

51-
// Check if the given branch was found
53+
// IsFound Check if the given branch was found
5254
func (y *Yaml) IsFound() bool {
53-
if y.data == nil {
54-
return false
55-
}
56-
return true
55+
return y.data != nil
5756
}
5857

5958
// Get returns a pointer to a new `Yaml` object for `key` in its `map` representation
@@ -67,6 +66,7 @@ func (y *Yaml) Get(key interface{}) *Yaml {
6766
return &Yaml{val}
6867
}
6968
}
69+
7070
return &Yaml{nil}
7171
}
7272

@@ -79,6 +79,7 @@ func (y *Yaml) GetPath(branch ...interface{}) *Yaml {
7979
for _, p := range branch {
8080
yin = yin.Get(p)
8181
}
82+
8283
return yin
8384
}
8485

@@ -87,21 +88,22 @@ func (y *Yaml) Array() ([]interface{}, error) {
8788
if a, ok := (y.data).([]interface{}); ok {
8889
return a, nil
8990
}
91+
9092
return nil, errors.New("type assertion to []interface{} failed")
9193
}
9294

9395
func (y *Yaml) IsArray() bool {
9496
_, err := y.Array()
95-
9697
return err == nil
9798
}
9899

99-
// return the size of array
100+
// GetArraySize return the size of array
100101
func (y *Yaml) GetArraySize() (int, error) {
101102
a, err := y.Array()
102103
if err != nil {
103104
return 0, err
104105
}
106+
105107
return len(a), nil
106108
}
107109

@@ -117,6 +119,7 @@ func (y *Yaml) GetIndex(index int) *Yaml {
117119
return &Yaml{a[index]}
118120
}
119121
}
122+
120123
return &Yaml{nil}
121124
}
122125

@@ -125,6 +128,7 @@ func (y *Yaml) Int() (int, error) {
125128
if v, ok := (y.data).(int); ok {
126129
return v, nil
127130
}
131+
128132
return 0, errors.New("type assertion to int failed")
129133
}
130134

@@ -133,6 +137,7 @@ func (y *Yaml) Bool() (bool, error) {
133137
if v, ok := (y.data).(bool); ok {
134138
return v, nil
135139
}
140+
136141
return false, errors.New("type assertion to bool failed")
137142
}
138143

@@ -141,13 +146,15 @@ func (y *Yaml) String() (string, error) {
141146
if v, ok := (y.data).(string); ok {
142147
return v, nil
143148
}
149+
144150
return "", errors.New("type assertion to string failed")
145151
}
146152

147153
func (y *Yaml) Float() (float64, error) {
148154
if v, ok := (y.data).(float64); ok {
149155
return v, nil
150156
}
157+
151158
return 0, errors.New("type assertion to float64 failed")
152159
}
153160

@@ -156,27 +163,30 @@ func (y *Yaml) Map() (map[interface{}]interface{}, error) {
156163
if m, ok := (y.data).(map[interface{}]interface{}); ok {
157164
return m, nil
158165
}
166+
159167
return nil, errors.New("type assertion to map[interface]interface{} failed")
160168
}
161169

162-
// Check if it is a map
170+
// IsMap Check if it is a map
163171
func (y *Yaml) IsMap() bool {
164172
_, err := y.Map()
165173
return err == nil
166174
}
167175

168-
// Get all the keys of the map
176+
// GetMapKeys Get all the keys of the map
169177
func (y *Yaml) GetMapKeys() ([]string, error) {
170178
m, err := y.Map()
171179

172180
if err != nil {
173181
return nil, err
174182
}
183+
175184
keys := make([]string, 0)
176185
for k, _ := range m {
177186
if s, ok := k.(string); ok {
178187
keys = append(keys, s)
179188
}
180189
}
190+
181191
return keys, nil
182192
}

0 commit comments

Comments
 (0)