Skip to content

Commit

Permalink
Merge pull request #223 from orangedeng/dyx-float
Browse files Browse the repository at this point in the history
Add float support for Rancher API
  • Loading branch information
ibuildthecloud authored Nov 19, 2018
2 parents 17430d7 + 1883e14 commit 5deb22f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions parse/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ func ConvertSimple(fieldType string, value interface{}, op Operation) (interface
return convert.ToString(value), nil
case "int":
return convert.ToNumber(value)
case "float":
return convert.ToFloat(value)
case "password":
return convert.ToString(value), nil
case "string":
Expand Down
24 changes: 24 additions & 0 deletions types/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,30 @@ func ToNumber(value interface{}) (int64, error) {
return strconv.ParseInt(ToString(value), 10, 64)
}

func ToFloat(value interface{}) (float64, error) {
value = Singular(value)

f64, ok := value.(float64)
if ok {
return f64, nil
}

f32, ok := value.(float32)
if ok {
return float64(f32), nil
}

if n, ok := value.(json.Number); ok {
i, err := n.Int64()
if err == nil {
return float64(i), nil
}
f, err := n.Float64()
return float64(f), err
}
return strconv.ParseFloat(ToString(value), 64)
}

func Capitalize(s string) string {
if len(s) <= 1 {
return strings.ToUpper(s)
Expand Down

0 comments on commit 5deb22f

Please sign in to comment.