-
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.
tfprotov5+tfprotov6: Add DynamicValue type IsNull method
Reference: #305
- Loading branch information
Showing
6 changed files
with
310 additions
and
0 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,6 @@ | ||
kind: ENHANCEMENTS | ||
body: 'tfprotov5: Added `DynamicValue` type `IsNull` method, which enables checking | ||
if the value is null without type information and fully decoding underlying data' | ||
time: 2023-06-27T12:58:06.917152-04:00 | ||
custom: | ||
Issue: "305" |
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,6 @@ | ||
kind: ENHANCEMENTS | ||
body: 'tfprotov6: Added `DynamicValue` type `IsNull` method, which enables checking | ||
if the value is null without type information and fully decoding underlying data' | ||
time: 2023-06-27T12:59:12.941648-04:00 | ||
custom: | ||
Issue: "305" |
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,107 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package tfprotov5_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-go/tfprotov5" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
func TestDynamicValueIsNull(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
dynamicValue tfprotov5.DynamicValue | ||
expected bool | ||
expectedError error | ||
}{ | ||
"empty-dynamic-value": { | ||
dynamicValue: tfprotov5.DynamicValue{}, | ||
expected: false, | ||
expectedError: fmt.Errorf("unable to read DynamicValue: DynamicValue had no JSON or msgpack data set"), | ||
}, | ||
"null": { | ||
dynamicValue: testNewDynamicValueMust(t, | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"test_string_attribute": tftypes.String, | ||
}, | ||
}, | ||
tftypes.NewValue( | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"test_string_attribute": tftypes.String, | ||
}, | ||
}, | ||
nil, | ||
), | ||
), | ||
expected: true, | ||
}, | ||
"known": { | ||
dynamicValue: testNewDynamicValueMust(t, | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"test_string_attribute": tftypes.String, | ||
}, | ||
}, | ||
tftypes.NewValue( | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"test_string_attribute": tftypes.String, | ||
}, | ||
}, | ||
map[string]tftypes.Value{ | ||
"test_string_attribute": tftypes.NewValue(tftypes.String, "test-value"), | ||
}, | ||
), | ||
), | ||
expected: false, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
name, testCase := name, testCase | ||
|
||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
got, err := testCase.dynamicValue.IsNull() | ||
|
||
if err != nil { | ||
if testCase.expectedError == nil { | ||
t.Fatalf("wanted no error, got error: %s", err) | ||
} | ||
|
||
if !strings.Contains(err.Error(), testCase.expectedError.Error()) { | ||
t.Fatalf("wanted error %q, got error: %s", testCase.expectedError.Error(), err.Error()) | ||
} | ||
} | ||
|
||
if err == nil && testCase.expectedError != nil { | ||
t.Fatalf("got no error, wanted err: %s", testCase.expectedError) | ||
} | ||
|
||
if got != testCase.expected { | ||
t.Errorf("expected %t, got %t", testCase.expected, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func testNewDynamicValueMust(t *testing.T, typ tftypes.Type, value tftypes.Value) tfprotov5.DynamicValue { | ||
t.Helper() | ||
|
||
dynamicValue, err := tfprotov5.NewDynamicValue(typ, value) | ||
|
||
if err != nil { | ||
t.Fatalf("unable to create DynamicValue: %s", err) | ||
} | ||
|
||
return dynamicValue | ||
} |
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,107 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package tfprotov6_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
func TestDynamicValueIsNull(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
dynamicValue tfprotov6.DynamicValue | ||
expected bool | ||
expectedError error | ||
}{ | ||
"empty-dynamic-value": { | ||
dynamicValue: tfprotov6.DynamicValue{}, | ||
expected: false, | ||
expectedError: fmt.Errorf("unable to read DynamicValue: DynamicValue had no JSON or msgpack data set"), | ||
}, | ||
"null": { | ||
dynamicValue: testNewDynamicValueMust(t, | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"test_string_attribute": tftypes.String, | ||
}, | ||
}, | ||
tftypes.NewValue( | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"test_string_attribute": tftypes.String, | ||
}, | ||
}, | ||
nil, | ||
), | ||
), | ||
expected: true, | ||
}, | ||
"known": { | ||
dynamicValue: testNewDynamicValueMust(t, | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"test_string_attribute": tftypes.String, | ||
}, | ||
}, | ||
tftypes.NewValue( | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"test_string_attribute": tftypes.String, | ||
}, | ||
}, | ||
map[string]tftypes.Value{ | ||
"test_string_attribute": tftypes.NewValue(tftypes.String, "test-value"), | ||
}, | ||
), | ||
), | ||
expected: false, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
name, testCase := name, testCase | ||
|
||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
got, err := testCase.dynamicValue.IsNull() | ||
|
||
if err != nil { | ||
if testCase.expectedError == nil { | ||
t.Fatalf("wanted no error, got error: %s", err) | ||
} | ||
|
||
if !strings.Contains(err.Error(), testCase.expectedError.Error()) { | ||
t.Fatalf("wanted error %q, got error: %s", testCase.expectedError.Error(), err.Error()) | ||
} | ||
} | ||
|
||
if err == nil && testCase.expectedError != nil { | ||
t.Fatalf("got no error, wanted err: %s", testCase.expectedError) | ||
} | ||
|
||
if got != testCase.expected { | ||
t.Errorf("expected %t, got %t", testCase.expected, got) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func testNewDynamicValueMust(t *testing.T, typ tftypes.Type, value tftypes.Value) tfprotov6.DynamicValue { | ||
t.Helper() | ||
|
||
dynamicValue, err := tfprotov6.NewDynamicValue(typ, value) | ||
|
||
if err != nil { | ||
t.Fatalf("unable to create DynamicValue: %s", err) | ||
} | ||
|
||
return dynamicValue | ||
} |