Skip to content
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.

Commit

Permalink
fix(number): fix printing float precision (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlittle authored Apr 18, 2019
1 parent be3f575 commit 5ac66b0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions fixtures/rawPlans/floatInput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
+ foo.bar
version: "1.10"
versionDiff: "{ \"number\": 1.10 }" => "{ \"number\": 2.0 }"
8 changes: 8 additions & 0 deletions fixtures/rawPlans/floatOutput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
+ foo.bar
version: "1.10"
versionDiff: {
- "number": 1.10
+ "number": 2.0
}


13 changes: 10 additions & 3 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ func processComplexAttributes(a *parser.Attribute, indentLength int) {

var old, new interface{}

json.Unmarshal(bBefore, &old) // nolint:gosec
json.Unmarshal(bAfter, &new) // nolint:gosec
beforeDecoder := json.NewDecoder(strings.NewReader(*a.Before))
beforeDecoder.UseNumber()
beforeDecoder.Decode(&old) // nolint:gosec

afterDecoder := json.NewDecoder(strings.NewReader(*a.After))
afterDecoder.UseNumber()
afterDecoder.Decode(&new) // nolint:gosec

oldPretty, _ := json.MarshalIndent(old, "", " ") // nolint:gosec
newPretty, _ := json.MarshalIndent(new, "", " ") // nolint:gosec
Expand Down Expand Up @@ -245,7 +250,9 @@ func formatValue(value string, indentLength int) string {
// Is JSON?
var j interface{}

json.Unmarshal([]byte(value), &j) // nolint:gosec
decoder := json.NewDecoder(strings.NewReader(value))
decoder.UseNumber()
decoder.Decode(&j) // nolint:gosec

// 4 (attribute padding) + 1 (key/value space separation) + 1 (opening quote for value ")
jsonIdentLegth := indentLength + 4 + 1 + 1
Expand Down
1 change: 1 addition & 0 deletions pkg/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestPrintPlan(t *testing.T) {
{"../../fixtures/rawPlans/base64Input.txt", "../../fixtures/rawPlans/base64Output.txt"},
{"../../fixtures/rawPlans/base64CreateInput.txt", "../../fixtures/rawPlans/base64CreateOutput.txt"},
{"../../fixtures/rawPlans/multilineAttributeInput.txt", "../../fixtures/rawPlans/multilineAttributeOutput.txt"},
{"../../fixtures/rawPlans/floatInput.txt", "../../fixtures/rawPlans/floatOutput.txt"},
}

for _, tc := range cases {
Expand Down

0 comments on commit 5ac66b0

Please sign in to comment.