Skip to content

Commit 3308b35

Browse files
committed
chore: replace interface{} with any (Go 1.18+ alias) and fix reflect type checks
- modules/* and internal/lib/formatting/format.go: switch to any alias - modules/terraform/var-file.go: replace reflect.TypeOf string check with type assertion Linear: OSS-3296
1 parent d982515 commit 3308b35

15 files changed

Lines changed: 75 additions & 73 deletions

File tree

internal/lib/formatting/format.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
// FormatBackendConfigAsArgs formats backend configuration as Terraform CLI args.
1212
// Example: {"bucket": "my-bucket"} -> ["-backend-config=bucket=my-bucket"]
13-
func FormatBackendConfigAsArgs(vars map[string]interface{}) []string {
13+
func FormatBackendConfigAsArgs(vars map[string]any) []string {
1414
return formatTerraformArgs(vars, "-backend-config", false, true)
1515
}
1616

@@ -25,7 +25,7 @@ func FormatPluginDirAsArgs(pluginDir string) []string {
2525
}
2626

2727
// formatTerraformArgs formats vars as CLI args with the given prefix.
28-
func formatTerraformArgs(vars map[string]interface{}, prefix string, useSpaceAsSeparator bool, omitNil bool) []string {
28+
func formatTerraformArgs(vars map[string]any, prefix string, useSpaceAsSeparator bool, omitNil bool) []string {
2929
var args []string
3030

3131
for key, value := range vars {
@@ -49,7 +49,7 @@ func formatTerraformArgs(vars map[string]interface{}, prefix string, useSpaceAsS
4949

5050
// ToHCLString converts Go values to HCL-formatted strings for Terraform CLI arguments.
5151
// Handles primitives, slices, and maps. Example: []int{1,2,3} -> "[1, 2, 3]"
52-
func ToHCLString(value interface{}, isNested bool) string {
52+
func ToHCLString(value any, isNested bool) string {
5353
if slice, isSlice := tryToConvertToGenericSlice(value); isSlice {
5454
return sliceToHclString(slice)
5555
} else if m, isMap := tryToConvertToGenericMap(value); isMap {
@@ -59,14 +59,14 @@ func ToHCLString(value interface{}, isNested bool) string {
5959
}
6060
}
6161

62-
// tryToConvertToGenericSlice converts any slice type to []interface{} using reflection.
63-
func tryToConvertToGenericSlice(value interface{}) ([]interface{}, bool) {
62+
// tryToConvertToGenericSlice converts any slice type to []any using reflection.
63+
func tryToConvertToGenericSlice(value any) ([]any, bool) {
6464
reflectValue := reflect.ValueOf(value)
6565
if reflectValue.Kind() != reflect.Slice {
66-
return []interface{}{}, false
66+
return []any{}, false
6767
}
6868

69-
genericSlice := make([]interface{}, reflectValue.Len())
69+
genericSlice := make([]any, reflectValue.Len())
7070

7171
for i := 0; i < reflectValue.Len(); i++ {
7272
genericSlice[i] = reflectValue.Index(i).Interface()
@@ -75,19 +75,19 @@ func tryToConvertToGenericSlice(value interface{}) ([]interface{}, bool) {
7575
return genericSlice, true
7676
}
7777

78-
// tryToConvertToGenericMap converts any map[string]T to map[string]interface{} using reflection.
79-
func tryToConvertToGenericMap(value interface{}) (map[string]interface{}, bool) {
78+
// tryToConvertToGenericMap converts any map[string]T to map[string]any using reflection.
79+
func tryToConvertToGenericMap(value any) (map[string]any, bool) {
8080
reflectValue := reflect.ValueOf(value)
8181
if reflectValue.Kind() != reflect.Map {
82-
return map[string]interface{}{}, false
82+
return map[string]any{}, false
8383
}
8484

8585
reflectType := reflect.TypeOf(value)
8686
if reflectType.Key().Kind() != reflect.String {
87-
return map[string]interface{}{}, false
87+
return map[string]any{}, false
8888
}
8989

90-
genericMap := make(map[string]interface{}, reflectValue.Len())
90+
genericMap := make(map[string]any, reflectValue.Len())
9191

9292
mapKeys := reflectValue.MapKeys()
9393
for _, key := range mapKeys {
@@ -97,7 +97,7 @@ func tryToConvertToGenericMap(value interface{}) (map[string]interface{}, bool)
9797
return genericMap, true
9898
}
9999

100-
func sliceToHclString(slice []interface{}) string {
100+
func sliceToHclString(slice []any) string {
101101
hclValues := make([]string, 0, len(slice))
102102

103103
for _, value := range slice {
@@ -108,7 +108,7 @@ func sliceToHclString(slice []interface{}) string {
108108
return fmt.Sprintf("[%s]", strings.Join(hclValues, ", "))
109109
}
110110

111-
func mapToHclString(m map[string]interface{}) string {
111+
func mapToHclString(m map[string]any) string {
112112
keyValuePairs := make([]string, 0, len(m))
113113

114114
for key, value := range m {
@@ -119,7 +119,7 @@ func mapToHclString(m map[string]interface{}) string {
119119
return fmt.Sprintf("{%s}", strings.Join(keyValuePairs, ", "))
120120
}
121121

122-
func primitiveToHclString(value interface{}, isNested bool) string {
122+
func primitiveToHclString(value any, isNested bool) string {
123123
if value == nil {
124124
return "null"
125125
}

internal/lib/formatting/format_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ func TestFormatBackendConfigAsArgs(t *testing.T) {
1212

1313
tests := []struct {
1414
name string
15-
input map[string]interface{}
15+
input map[string]any
1616
expect []string
1717
}{
1818
{
1919
name: "empty config",
20-
input: map[string]interface{}{},
20+
input: map[string]any{},
2121
expect: []string{},
2222
},
2323
{
2424
name: "string value",
25-
input: map[string]interface{}{"bucket": "my-bucket"},
25+
input: map[string]any{"bucket": "my-bucket"},
2626
expect: []string{"-backend-config=bucket=my-bucket"},
2727
},
2828
{
2929
name: "nil value omitted",
30-
input: map[string]interface{}{"key": nil},
30+
input: map[string]any{"key": nil},
3131
expect: []string{"-backend-config=key"},
3232
},
3333
{
3434
name: "multiple values",
35-
input: map[string]interface{}{"region": "us-east-1", "bucket": "state"},
35+
input: map[string]any{"region": "us-east-1", "bucket": "state"},
3636
expect: []string{"-backend-config=bucket=state", "-backend-config=region=us-east-1"},
3737
},
3838
}
@@ -82,7 +82,7 @@ func TestToHclString(t *testing.T) {
8282

8383
tests := []struct {
8484
name string
85-
input interface{}
85+
input any
8686
expect string
8787
}{
8888
{"nil", nil, "null"},
@@ -93,7 +93,7 @@ func TestToHclString(t *testing.T) {
9393
{"list of strings", []string{"a", "b"}, `["a", "b"]`},
9494
{"list of ints", []int{1, 2, 3}, "[1, 2, 3]"},
9595
{"map", map[string]string{"key": "value"}, `{"key" = "value"}`},
96-
{"nested list", []interface{}{[]int{1, 2}}, "[[1, 2]]"},
96+
{"nested list", []any{[]int{1, 2}}, "[[1, 2]]"},
9797
}
9898

9999
for _, tt := range tests {

modules/aws/lambda.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type LambdaOptions struct {
5050
InvocationType *InvocationTypeOption
5151

5252
// Lambda function input; will be converted to JSON.
53-
Payload interface{}
53+
Payload any
5454
}
5555

5656
// LambdaOutput contains the output from InvokeFunctionWithParams(). The
@@ -68,7 +68,7 @@ type LambdaOutput struct {
6868

6969
// InvokeFunctionContextE invokes a lambda function.
7070
// The ctx parameter supports cancellation and timeouts.
71-
func InvokeFunctionContextE(t testing.TestingT, ctx context.Context, region, functionName string, payload interface{}) ([]byte, error) {
71+
func InvokeFunctionContextE(t testing.TestingT, ctx context.Context, region, functionName string, payload any) ([]byte, error) {
7272
lambdaClient, err := NewLambdaClientContextE(t, ctx, region)
7373
if err != nil {
7474
return nil, err
@@ -102,7 +102,7 @@ func InvokeFunctionContextE(t testing.TestingT, ctx context.Context, region, fun
102102
// InvokeFunctionContext invokes a lambda function.
103103
// This function will fail the test if there is an error.
104104
// The ctx parameter supports cancellation and timeouts.
105-
func InvokeFunctionContext(t testing.TestingT, ctx context.Context, region, functionName string, payload interface{}) []byte {
105+
func InvokeFunctionContext(t testing.TestingT, ctx context.Context, region, functionName string, payload any) []byte {
106106
t.Helper()
107107
out, err := InvokeFunctionContextE(t, ctx, region, functionName, payload)
108108
require.NoError(t, err)
@@ -113,15 +113,15 @@ func InvokeFunctionContext(t testing.TestingT, ctx context.Context, region, func
113113
// InvokeFunction invokes a lambda function.
114114
//
115115
// Deprecated: Use [InvokeFunctionContext] instead.
116-
func InvokeFunction(t testing.TestingT, region, functionName string, payload interface{}) []byte {
116+
func InvokeFunction(t testing.TestingT, region, functionName string, payload any) []byte {
117117
t.Helper()
118118
return InvokeFunctionContext(t, context.Background(), region, functionName, payload)
119119
}
120120

121121
// InvokeFunctionE invokes a lambda function.
122122
//
123123
// Deprecated: Use [InvokeFunctionContextE] instead.
124-
func InvokeFunctionE(t testing.TestingT, region, functionName string, payload interface{}) ([]byte, error) {
124+
func InvokeFunctionE(t testing.TestingT, region, functionName string, payload any) ([]byte, error) {
125125
return InvokeFunctionContextE(t, context.Background(), region, functionName, payload)
126126
}
127127

modules/helm/template.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func UnmarshalK8SYamlsE[T any](t testing.TestingT, yamlData string, destinationO
233233
}
234234

235235
// UnmarshalK8SYaml is the same as UnmarshalK8SYamlE, but will fail the test if there is an error.
236-
func UnmarshalK8SYaml(t testing.TestingT, yamlData string, destinationObj interface{}) {
236+
func UnmarshalK8SYaml(t testing.TestingT, yamlData string, destinationObj any) {
237237
require.NoError(t, UnmarshalK8SYamlE(t, yamlData, destinationObj))
238238
}
239239

@@ -244,7 +244,7 @@ func UnmarshalK8SYaml(t testing.TestingT, yamlData string, destinationObj interf
244244
// UnmarshalK8SYamlE(t, renderedOutput, &deployment)
245245
//
246246
// At the end of this, the deployment variable will be populated.
247-
func UnmarshalK8SYamlE(t testing.TestingT, yamlData string, destinationObj interface{}) error {
247+
func UnmarshalK8SYamlE(t testing.TestingT, yamlData string, destinationObj any) error {
248248
decoder := goyaml.NewDecoder(strings.NewReader(yamlData))
249249

250250
// Ensure destinationObj is a pointer
@@ -258,7 +258,7 @@ func UnmarshalK8SYamlE(t testing.TestingT, yamlData string, destinationObj inter
258258
// Handle single object or list as root
259259
if destElem.Kind() != reflect.Slice {
260260
// Decode only the first document
261-
var rawYaml interface{}
261+
var rawYaml any
262262
if err := decoder.Decode(&rawYaml); err != nil {
263263
return goerrors.WithStackTrace(err)
264264
}
@@ -285,7 +285,7 @@ func UnmarshalK8SYamlE(t testing.TestingT, yamlData string, destinationObj inter
285285
sliceVal := slicePtr.Elem()
286286

287287
for {
288-
var rawYaml interface{}
288+
var rawYaml any
289289
if err := decoder.Decode(&rawYaml); err != nil {
290290
if errors.Is(err, io.EOF) {
291291
break // No more documents

modules/packer/packer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ type packerManifestBuild struct {
367367
BuilderType string `json:"builder_type"`
368368
ArtifactID string `json:"artifact_id"`
369369
PackerRunUUID string `json:"packer_run_uuid"`
370-
CustomData map[string]interface{} `json:"custom_data"`
370+
CustomData map[string]any `json:"custom_data"`
371371
Files []packerManifestBuildFile `json:"files"`
372372
BuildTime int64 `json:"build_time"`
373373
}

modules/ssh/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,6 @@ func Close(t testing.TestingT, closeable Closeable, ignoreErrors ...string) {
114114
// interfaceIsNil checks whether the given interface value is nil. A direct nil comparison does not work for interface
115115
// values that wrap a typed nil pointer, so reflection is used.
116116
// See https://go.dev/doc/faq#nil_error for details.
117-
func interfaceIsNil(i interface{}) bool {
117+
func interfaceIsNil(i any) bool {
118118
return i == nil || reflect.ValueOf(i).IsNil()
119119
}

modules/terraform/var-file.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ func GetVariableAsMapFromVarFileE(t testing.TestingT, fileName string, key strin
7272
return nil, InputFileKeyNotFound{FilePath: fileName, Key: key}
7373
}
7474

75-
if reflect.TypeOf(variable).String() != "map[string]interface {}" {
76-
return nil, UnexpectedOutputType{Key: key, ExpectedType: "map[string]interface {}", ActualType: reflect.TypeOf(variable).String()}
75+
mapVariable, ok := variable.(map[string]any)
76+
if !ok {
77+
return nil, UnexpectedOutputType{Key: key, ExpectedType: "map[string]any", ActualType: reflect.TypeOf(variable).String()}
7778
}
7879

7980
resultMap := make(map[string]string)
80-
for mapKey, mapVal := range variable.(map[string]any) {
81+
for mapKey, mapVal := range mapVariable {
8182
resultMap[mapKey] = fmt.Sprintf("%v", mapVal)
8283
}
8384

@@ -109,12 +110,13 @@ func GetVariableAsListFromVarFileE(t testing.TestingT, fileName string, key stri
109110
return nil, InputFileKeyNotFound{FilePath: fileName, Key: key}
110111
}
111112

112-
if reflect.TypeOf(variable).String() != "[]interface {}" {
113-
return nil, UnexpectedOutputType{Key: key, ExpectedType: "[]interface {}", ActualType: reflect.TypeOf(variable).String()}
113+
listVariable, ok := variable.([]any)
114+
if !ok {
115+
return nil, UnexpectedOutputType{Key: key, ExpectedType: "[]any", ActualType: reflect.TypeOf(variable).String()}
114116
}
115117

116118
resultArray := []string{}
117-
for _, item := range variable.([]any) {
119+
for _, item := range listVariable {
118120
resultArray = append(resultArray, fmt.Sprintf("%v", item))
119121
}
120122

modules/terragrunt/json_helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func CleanTerragruntJSON(input string) (string, error) {
161161
}
162162

163163
// Parse JSON
164-
var jsonObj interface{}
164+
var jsonObj any
165165
if err := json.Unmarshal([]byte(cleaned), &jsonObj); err != nil {
166166
return "", err
167167
}

modules/terragrunt/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ type Options struct {
7171
Logger *logger.Logger // Logger for command output
7272

7373
// Complex configuration that requires special formatting (NOT raw command-line args)
74-
BackendConfig map[string]interface{} // Backend configuration (formatted specially)
74+
BackendConfig map[string]any // Backend configuration (formatted specially)
7575

7676
// Test framework configuration (NOT passed to tg command line)
7777
TerragruntBinary string // The tg binary to use (should be "terragrunt")

modules/terragrunt/render_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestRenderJSON(t *testing.T) {
4040
TerragruntBinary: "terragrunt",
4141
})
4242

43-
var parsed map[string]interface{}
43+
var parsed map[string]any
4444
require.NoError(t, json.Unmarshal([]byte(output), &parsed), "output should be valid JSON")
4545
require.Contains(t, parsed, "terraform")
4646
}

0 commit comments

Comments
 (0)