Skip to content

Commit

Permalink
add ability to create the specified path it does not exist
Browse files Browse the repository at this point in the history
This commits enables goml to create YAML paths if they are not exist. Moreover
it is now able to set properties in empty YAML files.
  • Loading branch information
herrjulz committed Apr 25, 2018
1 parent 0cea11a commit 3619242
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 11 deletions.
4 changes: 2 additions & 2 deletions delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ mapArray:
Expect(err).NotTo(HaveOccurred())
})

It("should also delete", func() {
yam, _ := DeleteInMemory([]byte(yaml), "array.:lar")
PIt("should also delete", func() {
DeleteInMemory([]byte(yaml), "array.:lar")
//Expect(err).ToNot(HaveOccurred())
Expect(1).To(Equal(2))
})
Expand Down
5 changes: 5 additions & 0 deletions get20.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package goml

func Get(file []byte, path string) (string, error) {

}
50 changes: 41 additions & 9 deletions set.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package goml

import (
"errors"
"io/ioutil"
"strconv"
"strings"
Expand All @@ -11,6 +10,11 @@ import (
"github.com/smallfish/simpleyaml"
)

const (
ADDMAP = "GOML_ADD_MAP"
ADDARRAY = "GOML_ADD_ARRAY"
)

func SetKeyInFile(file string, path string, key string) error {
yaml, err := ReadYamlFromFile(file)
if err != nil {
Expand Down Expand Up @@ -48,6 +52,10 @@ func SetInFile(file string, path string, val interface{}) error {
}

func SetInMemory(file []byte, path string, val interface{}) ([]byte, error) {
if len(file) == 0 {
file = append(file, []byte(`{}`)...)
}

yml, err := simpleyaml.NewYaml(file)
if err != nil {
return nil, err
Expand Down Expand Up @@ -80,15 +88,18 @@ func Set(yml *simpleyaml.Yaml, path string, val interface{}) error {
if index, err := strconv.Atoi(propName); err == nil {
tmp, props := get(yml, newPath)
if props == nil {
return errors.New("property not found")
if err = Set(yml, newPath, ADDARRAY); err != nil {
return err //errors.New("SORRY CANNOT DO THAT. property not found")
}
tmp, props = get(yml, newPath)
}

prop, err := tmp.Array()
if err != nil {
return err
}

prop[index] = convertValueType(val)
prop[index] = convertValueType(setValueOrAddChild(val))

updateYaml(yml, props, prop)
return nil
Expand All @@ -100,6 +111,7 @@ func Set(yml *simpleyaml.Yaml, path string, val interface{}) error {
if err != nil {
return err
}

resolved := convertValueType(val)
prop = append(prop, resolved)
updateYaml(yml, props, prop)
Expand All @@ -110,36 +122,56 @@ func Set(yml *simpleyaml.Yaml, path string, val interface{}) error {
tmp, props := get(yml, newPath)
prop, err := tmp.Array()
if err != nil {
return err
if err = Set(yml, newPath, ADDARRAY); err != nil {
return err //errors.New("SORRY CANNOT DO THAT. property not found")
}
tmp, props = get(yml, newPath)
prop, err = tmp.Array()
}

index, err := returnIndexForProp(propName, prop)
if err != nil {
return err
index = createArrayEntry(propName, &prop)
} else {
prop[index] = convertValueType(setValueOrAddChild(val))
}

prop[index] = convertValueType(val)
updateYaml(yml, props, prop)
return nil
}

if len(propsArr) == 1 {
prop, _ := yml.Map()
prop[path] = convertValueType(val)
prop[path] = convertValueType(setValueOrAddChild(val))
return nil
}

tmp, _ := get(yml, newPath)
prop, err := tmp.Map()
if err != nil {
return err
if err = Set(yml, newPath, ADDMAP); err != nil {
return err
}
tmp, _ = get(yml, newPath)
prop, err = tmp.Map()
}

prop[propName] = convertValueType(val)
prop[propName] = convertValueType(setValueOrAddChild(val))

return nil
}

func setValueOrAddChild(val interface{}) interface{} {
switch val {
case ADDMAP:
m := make(map[interface{}]interface{})
return m
case ADDARRAY:
return []interface{}{}
}
return val
}

func SetValueForType(yaml *simpleyaml.Yaml, path string, value *simpleyaml.Yaml) error {
if v, err := value.String(); err == nil {
err := Set(yaml, path, v)
Expand Down
14 changes: 14 additions & 0 deletions set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@ mapArray:
err = Set(yml, "mapArray.foo:wolverine.arr.0", "new")
Expect(Get(yml, "mapArray.foo:wolverine.arr.0")).To(Equal("new"))
})

Context("If a path does not exist", func() {
It("should create the path", func() {
err = Set(yml, "map.awesome", "bam")
Expect(err).ToNot(HaveOccurred())

Expect(Get(yml, "map.awesome")).To(Equal("bam"))

err = Set(yml, "mapArray.luffy:gomugomuno.beat", "katakuri")
Expect(err).ToNot(HaveOccurred())

Expect(Get(yml, "mapArray.luffy:gomugomuno.beat")).To(Equal("katakuri"))
})
})
})
15 changes: 15 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func ReadYamlFromFile(filename string) (*simpleyaml.Yaml, error) {
return nil, err
}

if len(file) == 0 {
file = append(file, []byte(`{}`)...)
}

return simpleyaml.NewYaml(file)
}

Expand All @@ -124,3 +128,14 @@ func returnIndexForProp(propName string, array []interface{}) (int, error) {

return 0, errors.New("property not found")
}

func createArrayEntry(propName string, array *[]interface{}) int {
keyVal := strings.Split(propName, ":")
key, val := keyVal[0], keyVal[1]
fmt.Println("KEY:", key, "VAL", val)
m := make(map[interface{}]interface{})
m[key] = val
*array = append(*array, m)

return len(*array) - 1
}

0 comments on commit 3619242

Please sign in to comment.