Skip to content

Commit 5b05a55

Browse files
authored
Merge pull request #27395 from GlennChia/f-aws_evidently_feature
r/aws_evidently_feature
2 parents bc0c9f7 + 0ff3f57 commit 5b05a55

File tree

10 files changed

+2014
-3
lines changed

10 files changed

+2014
-3
lines changed

.changelog/27395.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
aws_evidently_feature
3+
```
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package nullable
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
const (
11+
TypeNullableFloat = schema.TypeString
12+
)
13+
14+
type Float string
15+
16+
func (i Float) IsNull() bool {
17+
return i == ""
18+
}
19+
20+
func (i Float) Value() (float64, bool, error) {
21+
if i.IsNull() {
22+
return 0, true, nil
23+
}
24+
25+
value, err := strconv.ParseFloat(string(i), 64)
26+
if err != nil {
27+
return 0, false, err
28+
}
29+
return value, false, nil
30+
}
31+
32+
// ValidateTypeStringNullableFloat provides custom error messaging for TypeString floats
33+
// Some arguments require an float value or unspecified, empty field.
34+
func ValidateTypeStringNullableFloat(v interface{}, k string) (ws []string, es []error) {
35+
value, ok := v.(string)
36+
if !ok {
37+
es = append(es, fmt.Errorf("expected type of %s to be string", k))
38+
return
39+
}
40+
41+
if value == "" {
42+
return
43+
}
44+
45+
if _, err := strconv.ParseFloat(value, 64); err != nil {
46+
es = append(es, fmt.Errorf("%s: cannot parse '%s' as float: %w", k, value, err))
47+
}
48+
49+
return
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package nullable
2+
3+
import (
4+
"errors"
5+
"regexp"
6+
"strconv"
7+
"testing"
8+
)
9+
10+
func TestNullableFloat(t *testing.T) {
11+
cases := []struct {
12+
val string
13+
expectNull bool
14+
expectedValue float64
15+
expectedErr error
16+
}{
17+
{
18+
val: "1",
19+
expectNull: false,
20+
expectedValue: 1,
21+
},
22+
{
23+
val: "1.1",
24+
expectNull: false,
25+
expectedValue: 1.1,
26+
},
27+
{
28+
val: "0",
29+
expectNull: false,
30+
expectedValue: 0,
31+
},
32+
{
33+
val: "",
34+
expectNull: true,
35+
expectedValue: 0,
36+
},
37+
{
38+
val: "A",
39+
expectNull: false,
40+
expectedValue: 0,
41+
expectedErr: strconv.ErrSyntax,
42+
},
43+
}
44+
45+
for i, tc := range cases {
46+
v := Float(tc.val)
47+
48+
if null := v.IsNull(); null != tc.expectNull {
49+
t.Fatalf("expected test case %d IsNull to return %t, got %t", i, null, tc.expectNull)
50+
}
51+
52+
value, null, err := v.Value()
53+
if value != tc.expectedValue {
54+
t.Fatalf("expected test case %d Value to be %f, got %f", i, tc.expectedValue, value)
55+
}
56+
if null != tc.expectNull {
57+
t.Fatalf("expected test case %d Value null flag to be %t, got %t", i, tc.expectNull, null)
58+
}
59+
if tc.expectedErr == nil && err != nil {
60+
t.Fatalf("expected test case %d to succeed, got error %s", i, err)
61+
}
62+
if tc.expectedErr != nil {
63+
if !errors.Is(err, tc.expectedErr) {
64+
t.Fatalf("expected test case %d to have error matching \"%s\", got %s", i, tc.expectedErr, err)
65+
}
66+
}
67+
}
68+
}
69+
70+
func TestValidationFloat(t *testing.T) {
71+
runTestCases(t, []testCase{
72+
{
73+
val: "1",
74+
f: ValidateTypeStringNullableFloat,
75+
},
76+
{
77+
val: "1.1",
78+
f: ValidateTypeStringNullableFloat,
79+
},
80+
{
81+
val: "0",
82+
f: ValidateTypeStringNullableFloat,
83+
},
84+
{
85+
val: "A",
86+
f: ValidateTypeStringNullableFloat,
87+
expectedErr: regexp.MustCompile(`[\w]+: cannot parse 'A' as float: .*`),
88+
},
89+
{
90+
val: 1,
91+
f: ValidateTypeStringNullableFloat,
92+
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be string`),
93+
},
94+
})
95+
}

internal/provider/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -1523,6 +1523,7 @@ func New(_ context.Context) (*schema.Provider, error) {
15231523

15241524
"aws_emrserverless_application": emrserverless.ResourceApplication(),
15251525

1526+
"aws_evidently_feature": evidently.ResourceFeature(),
15261527
"aws_evidently_project": evidently.ResourceProject(),
15271528
"aws_evidently_segment": evidently.ResourceSegment(),
15281529

0 commit comments

Comments
 (0)