Skip to content

Commit

Permalink
Merge pull request #1 from jszroberto/master
Browse files Browse the repository at this point in the history
Add support for float keys
  • Loading branch information
herrjulz authored Jul 19, 2017
2 parents 2dbb4d4 + a71bf28 commit e1ea553
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions goml/goml.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func ExtractType(value *simpleyaml.Yaml) (interface{}, error) {
if v, err := value.Int(); err == nil {
return strconv.Itoa(v), nil
}
if v, err := value.Float(); err == nil {
return fmt.Sprint(v), nil
}
if v, err := value.Array(); err == nil {
strSl := []string{}
for _, val := range v {
Expand All @@ -65,6 +68,8 @@ func extractArrayType(value interface{}) string {
return strconv.FormatBool(value.(bool))
case int:
return strconv.Itoa(value.(int))
case float64:
return fmt.Sprint(value.(float64))
}
return ""
}
Expand All @@ -78,7 +83,7 @@ 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("peroperty not found")
return errors.New("property not found")
}

prop, err := tmp.Array()
Expand Down Expand Up @@ -228,6 +233,10 @@ func SetValueForType(yaml *simpleyaml.Yaml, path string, value *simpleyaml.Yaml)
err := Set(yaml, path, v)
return err
}
if v, err := value.Float(); err == nil {
err := Set(yaml, path, v)
return err
}
if v, err := value.Array(); err == nil {
err := Set(yaml, path, v)
return err
Expand All @@ -250,9 +259,7 @@ func WriteYaml(yml *simpleyaml.Yaml, file string) error {
return err
}

ioutil.WriteFile(file, gomlSave, 0644)

return nil
return ioutil.WriteFile(file, gomlSave, 0644)
}

func ReadYaml(yaml []byte) (*simpleyaml.Yaml, error) {
Expand All @@ -269,15 +276,7 @@ func ReadYamlFromFile(filename string) (*simpleyaml.Yaml, error) {
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
return simpleyaml.NewYaml(file)
}

func get(yml *simpleyaml.Yaml, path string) (*simpleyaml.Yaml, []string) {
Expand Down

0 comments on commit e1ea553

Please sign in to comment.