-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from jszroberto/master
Little refactor + Bug fixes
- Loading branch information
Showing
14 changed files
with
703 additions
and
491 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
goml-linux-amd64 | ||
goml-darwin-amd64 | ||
vendor/* | ||
goml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
all: | ||
go build -o goml cmd/goml/main.go | ||
test: | ||
go test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package goml | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/smallfish/simpleyaml" | ||
) | ||
|
||
func DeleteInFile(file string, path string) error { | ||
yaml, err := ReadYamlFromFile(file) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = Delete(yaml, path) | ||
if err != nil { | ||
return errors.New("Couldn't delete property for path: " + path) | ||
} | ||
|
||
if yaml.IsMap() { | ||
keys, err := yaml.GetMapKeys() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(keys) == 0 { | ||
err = os.Remove(path) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return WriteYaml(yaml, file) | ||
} | ||
|
||
func Delete(yml *simpleyaml.Yaml, path string) error { | ||
propsArr := strings.Split(path, ".") | ||
propName := propsArr[len(propsArr)-1] | ||
props := propsArr[:len(propsArr)-1] | ||
newPath := strings.Join(props, ".") | ||
|
||
if index, err := strconv.Atoi(propName); err == nil { | ||
tmp, props := get(yml, newPath) | ||
prop, err := tmp.Array() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
prop = append(prop[:index], prop[index+1:]...) | ||
|
||
updateYaml(yml, props, prop) | ||
return nil | ||
} | ||
|
||
if strings.Contains(propName, ":") { | ||
tmp, props := get(yml, newPath) | ||
prop, err := tmp.Array() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
index, err := returnIndexForProp(propName, prop) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
prop = append(prop[:index], prop[index+1:]...) | ||
updateYaml(yml, props, prop) | ||
return nil | ||
} | ||
|
||
var res map[interface{}]interface{} | ||
if len(propsArr) == 1 { | ||
tmp, err := yml.Map() | ||
if err != nil { | ||
return err | ||
} | ||
res = tmp | ||
} else { | ||
prop, _ := get(yml, newPath) | ||
tmp, err := prop.Map() | ||
if err != nil { | ||
return err | ||
} | ||
res = tmp | ||
} | ||
_, ok := res[propName] | ||
if ok { | ||
delete(res, propName) | ||
} else { | ||
return errors.New("property not found") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package goml_test | ||
|
||
import ( | ||
. "github.com/JulzDiverse/goml" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"github.com/smallfish/simpleyaml" | ||
) | ||
|
||
var _ = Describe("Delete", func() { | ||
var yml *simpleyaml.Yaml | ||
var err error | ||
|
||
BeforeEach(func() { | ||
yaml := `map: | ||
name: foo | ||
array: | ||
- bar | ||
- var | ||
- zar | ||
mapArray: | ||
- foo: bar | ||
zoo: lion | ||
arr: | ||
- one | ||
- two | ||
- three | ||
- foo: var | ||
boo: laa` | ||
|
||
yml, err = simpleyaml.NewYaml([]byte(yaml)) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
|
||
It("should delete a value from a map", func() { | ||
err = Delete(yml, "map.name") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err = Get(yml, "map.name") | ||
Expect(err).To(MatchError("property not found")) | ||
}) | ||
|
||
It("should delete a value from an array ", func() { | ||
err = Delete(yml, "array.0") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err := Get(yml, "array.:bar") | ||
Expect(err).To(MatchError("property not found")) | ||
|
||
err = Delete(yml, "array.:zar") | ||
Expect(err).NotTo(HaveOccurred()) | ||
_, err = Get(yml, "array.:zar") | ||
Expect(err).To(MatchError("property not found")) | ||
}) | ||
|
||
It("should delete a value from an map inside an array ", func() { | ||
err = Delete(yml, "mapArray.foo:bar.zoo") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err := Get(yml, "mapArray.foo:bar.zoo") | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
|
||
It("should delete a value from an array inside a map which in turn is inside an array ", func() { | ||
err = Delete(yml, "mapArray.foo:bar.arr.0") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err := Get(yml, "mapArray.foo:bar.arr.:one") | ||
Expect(err).To(MatchError("property not found")) | ||
|
||
err = Delete(yml, "mapArray.foo:bar.arr.:two") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err = Get(yml, "mapArray.foo:bar.arr.:two") | ||
Expect(err).To(MatchError("property not found")) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package goml | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/smallfish/simpleyaml" | ||
) | ||
|
||
func GetFromFile(file string, path string) (interface{}, error) { | ||
yaml, err := ReadYamlFromFile(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return Get(yaml, path) | ||
} | ||
|
||
func GetFromFileAsSimpleYaml(file string, path string) (*simpleyaml.Yaml, error) { | ||
yaml, err := ReadYamlFromFile(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return GetAsSimpleYaml(yaml, path) | ||
} | ||
|
||
func Get(yml *simpleyaml.Yaml, path string) (interface{}, error) { | ||
val, ok := get(yml, path) | ||
if ok == nil { | ||
return nil, errors.New("property not found") | ||
} | ||
|
||
result, err := ExtractType(val) | ||
return result, err | ||
} | ||
|
||
func GetAsSimpleYaml(yml *simpleyaml.Yaml, path string) (*simpleyaml.Yaml, error) { | ||
val, ok := get(yml, path) | ||
if ok == nil { | ||
return nil, errors.New("property not found") | ||
} | ||
|
||
return val, nil | ||
} | ||
|
||
func get(yml *simpleyaml.Yaml, path string) (*simpleyaml.Yaml, []string) { | ||
solvedPath := []string{} | ||
|
||
props := strings.Split(path, ".") | ||
for _, p := range props { | ||
if index, err := strconv.Atoi(p); err == nil { | ||
yml = yml.GetIndex(index) | ||
solvedPath = append(solvedPath, strconv.Itoa(index)) | ||
continue | ||
} | ||
|
||
if strings.Contains(p, ":") { | ||
if prop, err := yml.Array(); err == nil { | ||
index, err := returnIndexForProp(p, prop) | ||
if err != nil { | ||
return yml, nil | ||
} | ||
|
||
yml = yml.GetIndex(index) | ||
solvedPath = append(solvedPath, strconv.Itoa(index)) | ||
continue | ||
} | ||
} | ||
solvedPath = append(solvedPath, p) | ||
yml = yml.Get(p) | ||
} | ||
return yml, solvedPath | ||
} |
Oops, something went wrong.