Skip to content

Commit

Permalink
Add useJSONNumber field to Plan (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Dec 7, 2023
1 parent ac10835 commit 970acde
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package tfjson

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand All @@ -29,6 +30,12 @@ const (

// 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.
Expand Down Expand Up @@ -85,6 +92,14 @@ type ResourceAttribute struct {
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 {
Expand Down Expand Up @@ -127,7 +142,11 @@ func (p *Plan) UnmarshalJSON(b []byte) error {
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
}
Expand Down

0 comments on commit 970acde

Please sign in to comment.