Skip to content

Commit

Permalink
Make ((calc)) output int64s if possible. Fixes #286
Browse files Browse the repository at this point in the history
  • Loading branch information
geofffranks committed Apr 8, 2019
1 parent 9c2210a commit 195c525
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions assets/calc/large-ints.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
int: (( calc "90 * 86400" ))
float: (( calc "(90 * 86400) + 0.1" ))
9 changes: 9 additions & 0 deletions ci/release_notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Bug Fixes

- Fixed `((calc))` to output integer values for its output, when
the result is a large integer. Previously, it was treated as a float,
and converted to scientific notation by the YAML marshaler. Fixes #286.

# Acknowledgements

Thanks @jhunt for finding and reporting the issue!
12 changes: 12 additions & 0 deletions cmd/spruce/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,18 @@ z:
`)
So(stdout, ShouldEqual, "")
})

Convey("Calc returns int64s if possible", func() {
os.Args = []string{"spruce", "merge", "--prune", "meta", "../../assets/calc/large-ints.yml"}
stdout = ""
stderr = ""
main()
So(stderr, ShouldEqual, "")
So(stdout, ShouldEqual, `float: 7.776e+06
int: 7776000
`)
})
})

Convey("YAML output is ordered the same way each time (#184)", func() {
Expand Down
7 changes: 7 additions & 0 deletions op_calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ func (CalcOperator) Run(ev *Evaluator, args []*Expr) (*Response, error) {
return nil, evaluateError
}

if resultFloat, ok := result.(float64); ok {
resultInt := int64(resultFloat)
if float64(resultInt) == resultFloat {
result = resultInt
}
}

DEBUG(" evaluated result: %v", result)
return &Response{
Type: Replace,
Expand Down

0 comments on commit 195c525

Please sign in to comment.