-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27395 from GlennChia/f-aws_evidently_feature
r/aws_evidently_feature
- Loading branch information
Showing
10 changed files
with
2,014 additions
and
3 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,3 @@ | ||
```release-note:new-resource | ||
aws_evidently_feature | ||
``` |
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,50 @@ | ||
package nullable | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
const ( | ||
TypeNullableFloat = schema.TypeString | ||
) | ||
|
||
type Float string | ||
|
||
func (i Float) IsNull() bool { | ||
return i == "" | ||
} | ||
|
||
func (i Float) Value() (float64, bool, error) { | ||
if i.IsNull() { | ||
return 0, true, nil | ||
} | ||
|
||
value, err := strconv.ParseFloat(string(i), 64) | ||
if err != nil { | ||
return 0, false, err | ||
} | ||
return value, false, nil | ||
} | ||
|
||
// ValidateTypeStringNullableFloat provides custom error messaging for TypeString floats | ||
// Some arguments require an float value or unspecified, empty field. | ||
func ValidateTypeStringNullableFloat(v interface{}, k string) (ws []string, es []error) { | ||
value, ok := v.(string) | ||
if !ok { | ||
es = append(es, fmt.Errorf("expected type of %s to be string", k)) | ||
return | ||
} | ||
|
||
if value == "" { | ||
return | ||
} | ||
|
||
if _, err := strconv.ParseFloat(value, 64); err != nil { | ||
es = append(es, fmt.Errorf("%s: cannot parse '%s' as float: %w", k, value, err)) | ||
} | ||
|
||
return | ||
} |
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,95 @@ | ||
package nullable | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func TestNullableFloat(t *testing.T) { | ||
cases := []struct { | ||
val string | ||
expectNull bool | ||
expectedValue float64 | ||
expectedErr error | ||
}{ | ||
{ | ||
val: "1", | ||
expectNull: false, | ||
expectedValue: 1, | ||
}, | ||
{ | ||
val: "1.1", | ||
expectNull: false, | ||
expectedValue: 1.1, | ||
}, | ||
{ | ||
val: "0", | ||
expectNull: false, | ||
expectedValue: 0, | ||
}, | ||
{ | ||
val: "", | ||
expectNull: true, | ||
expectedValue: 0, | ||
}, | ||
{ | ||
val: "A", | ||
expectNull: false, | ||
expectedValue: 0, | ||
expectedErr: strconv.ErrSyntax, | ||
}, | ||
} | ||
|
||
for i, tc := range cases { | ||
v := Float(tc.val) | ||
|
||
if null := v.IsNull(); null != tc.expectNull { | ||
t.Fatalf("expected test case %d IsNull to return %t, got %t", i, null, tc.expectNull) | ||
} | ||
|
||
value, null, err := v.Value() | ||
if value != tc.expectedValue { | ||
t.Fatalf("expected test case %d Value to be %f, got %f", i, tc.expectedValue, value) | ||
} | ||
if null != tc.expectNull { | ||
t.Fatalf("expected test case %d Value null flag to be %t, got %t", i, tc.expectNull, null) | ||
} | ||
if tc.expectedErr == nil && err != nil { | ||
t.Fatalf("expected test case %d to succeed, got error %s", i, err) | ||
} | ||
if tc.expectedErr != nil { | ||
if !errors.Is(err, tc.expectedErr) { | ||
t.Fatalf("expected test case %d to have error matching \"%s\", got %s", i, tc.expectedErr, err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestValidationFloat(t *testing.T) { | ||
runTestCases(t, []testCase{ | ||
{ | ||
val: "1", | ||
f: ValidateTypeStringNullableFloat, | ||
}, | ||
{ | ||
val: "1.1", | ||
f: ValidateTypeStringNullableFloat, | ||
}, | ||
{ | ||
val: "0", | ||
f: ValidateTypeStringNullableFloat, | ||
}, | ||
{ | ||
val: "A", | ||
f: ValidateTypeStringNullableFloat, | ||
expectedErr: regexp.MustCompile(`[\w]+: cannot parse 'A' as float: .*`), | ||
}, | ||
{ | ||
val: 1, | ||
f: ValidateTypeStringNullableFloat, | ||
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be string`), | ||
}, | ||
}) | ||
} |
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.