Skip to content

Commit

Permalink
Merge pull request #27395 from GlennChia/f-aws_evidently_feature
Browse files Browse the repository at this point in the history
r/aws_evidently_feature
  • Loading branch information
ewbankkit authored Nov 11, 2022
2 parents bc0c9f7 + 0ff3f57 commit 5b05a55
Show file tree
Hide file tree
Showing 10 changed files with 2,014 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/27395.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_evidently_feature
```
50 changes: 50 additions & 0 deletions internal/experimental/nullable/float.go
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
}
95 changes: 95 additions & 0 deletions internal/experimental/nullable/float_test.go
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`),
},
})
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,7 @@ func New(_ context.Context) (*schema.Provider, error) {

"aws_emrserverless_application": emrserverless.ResourceApplication(),

"aws_evidently_feature": evidently.ResourceFeature(),
"aws_evidently_project": evidently.ResourceProject(),
"aws_evidently_segment": evidently.ResourceSegment(),

Expand Down
Loading

0 comments on commit 5b05a55

Please sign in to comment.