Skip to content

Commit

Permalink
error on property not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Skupnjak committed Apr 4, 2017
1 parent f68ed1e commit a857417
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
42 changes: 29 additions & 13 deletions goml/goml.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
package goml

import (
"errors"
"fmt"
"io/ioutil"
"strconv"
"strings"

"gopkg.in/yaml.v2"

"github.com/smallfish/simpleyaml"
"gopkg.in/yaml.v2"
)

func Get(yml *simpleyaml.Yaml, path string) (*simpleyaml.Yaml, error) {
val, _ := get(yml, path)
// res := extractType(val)
return val, nil
}

func ExtractType(value *simpleyaml.Yaml) interface{} {
func ExtractType(value *simpleyaml.Yaml) (interface{}, error) {
if v, err := value.String(); err == nil {
return v
return v, nil
}
if v, err := value.Bool(); err == nil {
return strconv.FormatBool(v)
return strconv.FormatBool(v), nil
}
if v, err := value.Int(); err == nil {
return strconv.Itoa(v)
return strconv.Itoa(v), nil
}
if v, err := value.Array(); err == nil {
strSl := []string{}
Expand All @@ -34,12 +33,12 @@ func ExtractType(value *simpleyaml.Yaml) interface{} {
strSl = append(strSl, tmp)
}
str := strings.Join(strSl, ",")
return str
return str, nil
}
if v, err := value.Map(); err == nil {
return v
return v, nil
}
return nil
return nil, errors.New("property not found")
}

func extractArrayType(value interface{}) string {
Expand All @@ -57,9 +56,10 @@ func extractArrayType(value interface{}) string {
}

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

if index, err := strconv.Atoi(propName); err == nil {
Expand Down Expand Up @@ -100,6 +100,14 @@ func Set(yml *simpleyaml.Yaml, path string, val interface{}) error {
return nil
}

fmt.Println(len(propsArr), props)
if len(propsArr) == 1 {
prop, _ := yml.Map()
fmt.Println("prop", prop[path])
prop[path] = val

}

tmp, _ := get(yml, newPath)
prop, err := tmp.Map()
if err != nil {
Expand Down Expand Up @@ -184,6 +192,14 @@ func ReadYamlFromFile(filename string) (*simpleyaml.Yaml, error) {
if err != nil {
return nil, err
}

//val := yaml.MapSlice{}
//err = yaml.Unmarshal([]byte(file), &val)
//if err != nil {
//return nil, errors.New("unmarshal []byte to yaml failed: " + err.Error())
//}
//fmt.Printf("--- m:\n%v\n\n", val)

yml, _ := simpleyaml.NewYaml(file)
return yml, nil
}
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func getParam(c *cli.Context) {
exitWithError(errors.New("Couldn't find property"))
}

res := goml.ExtractType(rawValue)
res, err := goml.ExtractType(rawValue)
exitWithError(err)

// fmt.Printf("%s", rawValue)
fmt.Println(res)
Expand Down

0 comments on commit a857417

Please sign in to comment.