Skip to content

Commit

Permalink
Adding the ability to allow for easy converting. (#568)
Browse files Browse the repository at this point in the history
* 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
MovieStoreGuy authored Feb 19, 2025
1 parent 35ff781 commit b08c5fb
Show file tree
Hide file tree
Showing 12 changed files with 580 additions and 284 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60
version: v1.64
only-new-issues: true
23 changes: 6 additions & 17 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@ run:
# include test files or not, default is true
tests: true

# which dirs to skip: issues from them won't be reported;
# can use regexp here: generated.*, regexp is applied on full path;
# default value is empty list, but default dirs are skipped independently
# from this option's value (see exclude-dirs-use-default).
exclude-dirs:
# This package is what is being migrated across to `internal/definition`
# so ignoring existing issues that exist and
- signalfx

# default is true. Enables skipping of directories:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
exclude-dirs-use-default: false

# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
# If invoked with -mod=readonly, the go command is disallowed from the implicit
# automatic updating of go.mod described above. Instead, it fails when any changes
Expand All @@ -38,7 +25,8 @@ run:
# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
formats: colored-line-number
formats:
- format: colored-line-number

# print lines of code with issue, default is true
print-issued-lines: true
Expand Down Expand Up @@ -71,8 +59,7 @@ linters-settings:
- loopclosure

revive:
# minimal confidence for issues, default is 0.8
min-confidence: 0.8
confidence: 0.8

gofmt:
# simplify code: gofmt with `-s` option, true by default
Expand Down Expand Up @@ -146,4 +133,6 @@ issues:
- gosec
- text: "G402:"
linters:
- gosec
- gosec
exclude-dirs:
- signalfx
47 changes: 47 additions & 0 deletions internal/convert/convert_aws_integration.go
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
}
99 changes: 99 additions & 0 deletions internal/convert/convert_aws_integration_test.go
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")
})
}
}
12 changes: 12 additions & 0 deletions internal/convert/convert_string.go
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)
}
27 changes: 27 additions & 0 deletions internal/convert/convert_string_test.go
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")
}
34 changes: 34 additions & 0 deletions internal/convert/converter.go
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)
}
47 changes: 47 additions & 0 deletions internal/convert/converter_test.go
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))
})
}
}
9 changes: 9 additions & 0 deletions internal/convert/func.go
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
4 changes: 4 additions & 0 deletions internal/convert/func_test.go
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
Loading

0 comments on commit b08c5fb

Please sign in to comment.