Skip to content

Commit

Permalink
Fix AST JSON to handle integer/float checks correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
goller committed Nov 7, 2017
1 parent a585c27 commit 599f07a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
12 changes: 10 additions & 2 deletions tick/ast/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ func (j JSONNode) Int64(field string) (int64, error) {

num, ok := n.(int64)
if !ok {
return 0, fmt.Errorf("field %s is not an integer value but is %T", field, n)
flt, ok := n.(float64)
if !ok {
return 0, fmt.Errorf("field %s is not an integer value but is %T", field, n)
}
num = int64(flt)
}
return num, nil
}
Expand All @@ -119,7 +123,11 @@ func (j JSONNode) Float64(field string) (float64, error) {

num, ok := n.(float64)
if !ok {
return 0, fmt.Errorf("field %s is not a floating point value but is %T", field, n)
integer, ok := n.(int64)
if !ok {
return 0, fmt.Errorf("field %s is not a floating point value but is %T", field, n)
}
num = float64(integer)
}
return num, nil
}
Expand Down
21 changes: 15 additions & 6 deletions tick/ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,25 @@ func (n *NumberNode) unmarshal(props JSONNode) error {
if n.IsInt, err = props.Bool("isint"); err != nil {
return err
}

if n.IsFloat, err = props.Bool("isfloat"); err != nil {
return err
if n.IsInt {
if n.Int64, err = props.Int64("int64"); err != nil {
return err
}
base, err := props.Int64("base")
if err != nil {
return err
}
if base == 0 {
return fmt.Errorf("integer base cannot be zero")
}
n.Base = int(base)
}

if n.Float64, err = props.Float64("float64"); err != nil {
if n.IsFloat, err = props.Bool("isfloat"); err != nil {
return err
}

if n.Int64, err = props.Int64("int64"); err != nil {
if n.IsFloat {
n.Float64, err = props.Float64("float64")
return err
}

Expand Down

0 comments on commit 599f07a

Please sign in to comment.