-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow ignoring undefined attributes during unmarshalling (#213)
* Adding alternative implementation of Unmarshal which allows supplying of opts which can be used to modify the unmarshalling behaviour, such as ignoring undefined attributes (#212) * Upper case JSONOpts field (#212) * Code review changes (#212) * Apply suggestions from code review Co-authored-by: Brian Flad <bflad417@gmail.com> Co-authored-by: Brian Flad <bflad417@gmail.com>
- Loading branch information
1 parent
a325d5b
commit 71dfab6
Showing
7 changed files
with
318 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
```release-note:enhancement | ||
tfprotov5: Added `RawState` type `UnmarshalWithOpts` method to facilitate configurable behaviour during unmarshalling | ||
``` | ||
|
||
```release-note:enhancement | ||
tfprotov6: Added `RawState` type `UnmarshalWithOpts` method to facilitate configurable behaviour during unmarshalling | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package tfprotov5_test | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"github.com/hashicorp/terraform-plugin-go/tfprotov5" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
func TestRawStateUnmarshalWithOpts(t *testing.T) { | ||
t.Parallel() | ||
type testCase struct { | ||
rawState tfprotov5.RawState | ||
value tftypes.Value | ||
typ tftypes.Type | ||
opts tfprotov5.UnmarshalOpts | ||
} | ||
tests := map[string]testCase{ | ||
"object-of-bool-number": { | ||
rawState: tfprotov5.RawState{ | ||
JSON: []byte(`{"bool":true,"number":0}`), | ||
}, | ||
value: tftypes.NewValue(tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"bool": tftypes.Bool, | ||
"number": tftypes.Number, | ||
}, | ||
}, map[string]tftypes.Value{ | ||
"bool": tftypes.NewValue(tftypes.Bool, true), | ||
"number": tftypes.NewValue(tftypes.Number, big.NewFloat(0)), | ||
}), | ||
typ: tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"bool": tftypes.Bool, | ||
"number": tftypes.Number, | ||
}, | ||
}, | ||
}, | ||
"object-with-missing-attribute": { | ||
rawState: tfprotov5.RawState{ | ||
JSON: []byte(`{"bool":true,"number":0,"unknown":"whatever"}`), | ||
}, | ||
value: tftypes.NewValue(tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"bool": tftypes.Bool, | ||
"number": tftypes.Number, | ||
}, | ||
}, map[string]tftypes.Value{ | ||
"bool": tftypes.NewValue(tftypes.Bool, true), | ||
"number": tftypes.NewValue(tftypes.Number, big.NewFloat(0)), | ||
}), | ||
typ: tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"bool": tftypes.Bool, | ||
"number": tftypes.Number, | ||
}, | ||
}, | ||
opts: tfprotov5.UnmarshalOpts{ | ||
ValueFromJSONOpts: tftypes.ValueFromJSONOpts{ | ||
IgnoreUndefinedAttributes: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
val, err := test.rawState.UnmarshalWithOpts(test.typ, test.opts) | ||
if err != nil { | ||
t.Fatalf("unexpected error unmarshaling: %s", err) | ||
} | ||
|
||
if diff := cmp.Diff(test.value, val); diff != "" { | ||
t.Errorf("Unexpected results (-wanted +got): %s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package tfprotov6_test | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
func TestRawStateUnmarshalWithOpts(t *testing.T) { | ||
t.Parallel() | ||
type testCase struct { | ||
rawState tfprotov6.RawState | ||
value tftypes.Value | ||
typ tftypes.Type | ||
opts tfprotov6.UnmarshalOpts | ||
} | ||
tests := map[string]testCase{ | ||
"object-of-bool-number": { | ||
rawState: tfprotov6.RawState{ | ||
JSON: []byte(`{"bool":true,"number":0}`), | ||
}, | ||
value: tftypes.NewValue(tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"bool": tftypes.Bool, | ||
"number": tftypes.Number, | ||
}, | ||
}, map[string]tftypes.Value{ | ||
"bool": tftypes.NewValue(tftypes.Bool, true), | ||
"number": tftypes.NewValue(tftypes.Number, big.NewFloat(0)), | ||
}), | ||
typ: tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"bool": tftypes.Bool, | ||
"number": tftypes.Number, | ||
}, | ||
}, | ||
}, | ||
"object-with-missing-attribute": { | ||
rawState: tfprotov6.RawState{ | ||
JSON: []byte(`{"bool":true,"number":0,"unknown":"whatever"}`), | ||
}, | ||
value: tftypes.NewValue(tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"bool": tftypes.Bool, | ||
"number": tftypes.Number, | ||
}, | ||
}, map[string]tftypes.Value{ | ||
"bool": tftypes.NewValue(tftypes.Bool, true), | ||
"number": tftypes.NewValue(tftypes.Number, big.NewFloat(0)), | ||
}), | ||
typ: tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"bool": tftypes.Bool, | ||
"number": tftypes.Number, | ||
}, | ||
}, | ||
opts: tfprotov6.UnmarshalOpts{ | ||
ValueFromJSONOpts: tftypes.ValueFromJSONOpts{ | ||
IgnoreUndefinedAttributes: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
val, err := test.rawState.UnmarshalWithOpts(test.typ, test.opts) | ||
if err != nil { | ||
t.Fatalf("unexpected error unmarshaling: %s", err) | ||
} | ||
|
||
if diff := cmp.Diff(test.value, val); diff != "" { | ||
t.Errorf("Unexpected results (-wanted +got): %s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.