Skip to content

Commit

Permalink
add function for in memory manipulations
Browse files Browse the repository at this point in the history
  • Loading branch information
GorillaJulz committed Nov 14, 2017
1 parent aa50903 commit 06c5242
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ func DeleteInFile(file string, path string) error {
return WriteYaml(yaml, file)
}

func DeleteInMemory(file []byte, path string) ([]byte, error) {
yaml := simpleyaml.NewYaml(file)

err = Delete(yaml, path)
if err != nil {
return nil, errors.New("Couldn't delete property for path: " + path)
}

yamlMap, err := yaml.Map()
if err != nil {
return nil, err
}

yamlBytes, err := yaml.Marshal(yamlMap)
if err != nil {
return nil, err
}

return yamlBytes, nil
}

func Delete(yml *simpleyaml.Yaml, path string) error {
propsArr := strings.Split(path, ".")
propName := propsArr[len(propsArr)-1]
Expand Down
24 changes: 24 additions & 0 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ func SetInFile(file string, path string, val interface{}) error {
return WriteYaml(yaml, file)
}

func SetInMemory(file []byte, path string, val interface{}) ([]byte, error) {
yaml, err := ReadYamlFromFile(file)
if err != nil {
return nil, err
}

err = Set(yaml, path, val)
if err != nil {
return nil, err
}

yamlMap, err := yaml.Map()
if err != nil {
return nil, err
}

yamlBytes, err := yaml.Marshal(yamlMap)
if err != nil {
return nil, err
}

return yamlBytes, nil
}

func Set(yml *simpleyaml.Yaml, path string, val interface{}) error {
propsArr := strings.Split(path, ".")
propName := propsArr[len(propsArr)-1]
Expand Down

0 comments on commit 06c5242

Please sign in to comment.