Add useJSONNumber field to Plan (#111)
diff --git a/plan.go b/plan.go
index b6583c0..04a71ca 100644
--- a/plan.go
+++ b/plan.go
@@ -4,6 +4,7 @@
package tfjson
import (
+ "bytes"
"encoding/json"
"errors"
"fmt"
@@ -29,6 +30,12 @@
// Plan represents the entire contents of an output Terraform plan.
type Plan struct {
+ // useJSONNumber opts into the behavior of calling
+ // json.Decoder.UseNumber prior to decoding the plan, which turns
+ // numbers into json.Numbers instead of float64s. Set it using
+ // Plan.UseJSONNumber.
+ useJSONNumber bool
+
// The version of the plan format. This should always match the
// PlanFormatVersion constant in this package, or else an unmarshal
// will be unstable.
@@ -85,6 +92,14 @@
Attribute []json.RawMessage `json:"attribute"`
}
+// UseJSONNumber controls whether the Plan will be decoded using the
+// json.Number behavior or the float64 behavior. When b is true, the Plan will
+// represent numbers in PlanOutputs as json.Numbers. When b is false, the
+// Plan will represent numbers in PlanOutputs as float64s.
+func (p *Plan) UseJSONNumber(b bool) {
+ p.useJSONNumber = b
+}
+
// Validate checks to ensure that the plan is present, and the
// version matches the version supported by this library.
func (p *Plan) Validate() error {
@@ -127,7 +142,11 @@
type rawPlan Plan
var plan rawPlan
- err := json.Unmarshal(b, &plan)
+ dec := json.NewDecoder(bytes.NewReader(b))
+ if p.useJSONNumber {
+ dec.UseNumber()
+ }
+ err := dec.Decode(&plan)
if err != nil {
return err
}