-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the ability to allow for easy converting. (#568)
* Adding the ability to allow for easy converting. I have found in a few places that it becoming repeatative needing to convert from the terraform type to the expected type from a set. This allows for simple methods to be applied to convert between different values. * Bump golang ci version * Fixing up golangci-lint issue
- Loading branch information
1 parent
35ff781
commit b08c5fb
Showing
12 changed files
with
580 additions
and
284 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
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,47 @@ | ||
// Copyright Splunk, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package convert | ||
|
||
import "github.com/signalfx/signalfx-go/integration" | ||
|
||
func ToAWSNamespaceRule(in any) *integration.AwsNameSpaceSyncRule { | ||
data := in.(map[string]any) | ||
|
||
rule := &integration.AwsNameSpaceSyncRule{ | ||
Namespace: integration.AwsService(data["namespace"].(string)), | ||
} | ||
|
||
if action, ok := data["default_action"].(string); ok { | ||
rule.DefaultAction = integration.AwsSyncRuleFilterAction(action) | ||
} | ||
|
||
if action, ok := data["filter_action"].(string); ok { | ||
rule.Filter = &integration.AwsSyncRuleFilter{ | ||
Action: integration.AwsSyncRuleFilterAction(action), | ||
Source: data["filter_source"].(string), | ||
} | ||
} | ||
|
||
return rule | ||
} | ||
|
||
func ToAWSCustomNamespaceRule(in any) *integration.AwsCustomNameSpaceSyncRule { | ||
data := in.(map[string]any) | ||
sync := &integration.AwsCustomNameSpaceSyncRule{ | ||
Namespace: data["namespace"].(string), | ||
} | ||
|
||
if action, ok := data["default_action"].(string); ok && action != "" { | ||
sync.DefaultAction = integration.AwsSyncRuleFilterAction(action) | ||
} | ||
|
||
if action, ok := data["filter_action"].(string); ok && action != "" { | ||
sync.Filter = &integration.AwsSyncRuleFilter{ | ||
Action: integration.AwsSyncRuleFilterAction(action), | ||
Source: data["filter_source"].(string), | ||
} | ||
} | ||
|
||
return sync | ||
} |
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,99 @@ | ||
// Copyright Splunk, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package convert | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/signalfx/signalfx-go/integration" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestToAWSNamespaceRule(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range []struct { | ||
name string | ||
input map[string]any | ||
expect *integration.AwsNameSpaceSyncRule | ||
}{ | ||
{ | ||
name: "min. required details", | ||
input: map[string]any{ | ||
"namespace": "my-awesome-namespace", | ||
}, | ||
expect: &integration.AwsNameSpaceSyncRule{ | ||
Namespace: integration.AwsService("my-awesome-namespace"), | ||
}, | ||
}, | ||
{ | ||
name: "actions set", | ||
input: map[string]any{ | ||
"namespace": "AWS/linux", | ||
"default_action": "Include", | ||
"filter_action": "Exclude", | ||
"filter_source": "source", | ||
}, | ||
expect: &integration.AwsNameSpaceSyncRule{ | ||
Namespace: integration.AwsService("AWS/linux"), | ||
DefaultAction: integration.INCLUDE, | ||
Filter: &integration.AwsSyncRuleFilter{ | ||
Action: integration.EXCLUDE, | ||
Source: "source", | ||
}, | ||
}, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
actual := ToAWSNamespaceRule(tc.input) | ||
assert.Equal(t, tc.expect, actual, "Must match the expected value") | ||
}) | ||
} | ||
} | ||
|
||
func TestToAWSCustomNamespaceRule(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range []struct { | ||
name string | ||
in map[string]any | ||
expect *integration.AwsCustomNameSpaceSyncRule | ||
}{ | ||
{ | ||
name: "min. required values", | ||
in: map[string]any{ | ||
"namespace": "namespace", | ||
}, | ||
expect: &integration.AwsCustomNameSpaceSyncRule{ | ||
Namespace: "namespace", | ||
}, | ||
}, | ||
{ | ||
name: "all fields", | ||
in: map[string]any{ | ||
"namespace": "ns", | ||
"default_action": "Include", | ||
"filter_action": "Exclude", | ||
"filter_source": "source", | ||
}, | ||
expect: &integration.AwsCustomNameSpaceSyncRule{ | ||
Namespace: "ns", | ||
DefaultAction: integration.INCLUDE, | ||
Filter: &integration.AwsSyncRuleFilter{ | ||
Action: integration.EXCLUDE, | ||
Source: "source", | ||
}, | ||
}, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
actual := ToAWSCustomNamespaceRule(tc.in) | ||
assert.Equal(t, tc.expect, actual, "Must match the expected value") | ||
}) | ||
} | ||
} |
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,12 @@ | ||
// Copyright Splunk, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package convert | ||
|
||
func ToString(in any) string { | ||
return ToStringLike[string](in) | ||
} | ||
|
||
func ToStringLike[T ~string](in any) T { | ||
return in.(T) | ||
} |
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,27 @@ | ||
// Copyright Splunk, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package convert | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestToString(t *testing.T) { | ||
t.Parallel() | ||
|
||
const v = "my-string" | ||
|
||
assert.Equal(t, v, ToString(v), "Must match the expected value") | ||
} | ||
|
||
func TestToStringLike(t *testing.T) { | ||
t.Parallel() | ||
|
||
type secret string | ||
|
||
const s = secret("my-secret") | ||
assert.Equal(t, s, ToStringLike[secret](s), "Must match the expected value") | ||
} |
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,34 @@ | ||
// Copyright Splunk, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package convert | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
// SliceAll preallocates the require heap memory for the slice, | ||
// then applies the converter function on each element, | ||
// which is stored in the same positional value in the slice. | ||
func SliceAll[S ~[]In, In any, Out any](s S, converter Func[In, Out]) []Out { | ||
out := make([]Out, len(s)) | ||
for i, v := range s { | ||
out[i] = converter(v) | ||
} | ||
return out | ||
} | ||
|
||
// SchemaListAll will attempt to cast in as a [*schema.Set] | ||
// and if it can not convert or if the set size is zero, nil is returned. | ||
// Otherwise, the converter is applied to each item in the set and returned as a slice. | ||
func SchemaListAll[Out any](in any, converter Func[any, Out]) []Out { | ||
set, ok := in.(*schema.Set) | ||
if !ok || set == nil { | ||
return nil | ||
} | ||
if set.Len() == 0 { | ||
return nil | ||
} | ||
|
||
return SliceAll(set.List(), converter) | ||
} |
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,47 @@ | ||
// Copyright Splunk, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package convert | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSchemaListAll(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range []struct { | ||
name string | ||
schema *schema.Set | ||
F Func[any, any] | ||
expect []any | ||
}{ | ||
{ | ||
name: "nil set", | ||
schema: nil, | ||
F: func(s any) any { return s }, | ||
expect: nil, | ||
}, | ||
{ | ||
name: "no values set", | ||
schema: schema.NewSet(schema.HashInt, nil), | ||
F: func(s any) any { return s }, | ||
expect: nil, | ||
}, | ||
{ | ||
name: "int set", | ||
schema: schema.NewSet(schema.HashInt, []any{1, 2}), | ||
F: func(s any) any { return s }, | ||
expect: []any{1, 2}, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.Equal(t, tc.expect, SchemaListAll(tc.schema, tc.F)) | ||
}) | ||
} | ||
} |
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,9 @@ | ||
// Copyright Splunk, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package convert | ||
|
||
// Func is the operation that converts from the original value | ||
// that is stored as part of the schema set value | ||
// and will convert it to the expected out type. | ||
type Func[In any, Out any] func(s In) Out |
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,4 @@ | ||
// Copyright Splunk, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package convert |
Oops, something went wrong.