Skip to content

Commit d9abe8b

Browse files
Merge pull request #32 from jumppad-labs/erik/cleanup
replaced interface with any, fixed staticcheck problems
2 parents fe9dd28 + 8ea3035 commit d9abe8b

19 files changed

+70
-70
lines changed

convert/convert.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/zclconf/go-cty/cty/gocty"
99
)
1010

11-
func GoToCtyValue(val interface{}) (cty.Value, error) {
11+
func GoToCtyValue(val any) (cty.Value, error) {
1212
typ, err := gocty.ImpliedType(val)
1313
if err != nil {
1414
return cty.False, err
@@ -55,6 +55,6 @@ func GoToCtyValue(val interface{}) (cty.Value, error) {
5555
return ctyVal, nil
5656
}
5757

58-
func CtyToGo(val cty.Value, target interface{}) error {
58+
func CtyToGo(val cty.Value, target any) error {
5959
return gocty.FromCtyValue(val, target)
6060
}

errors/parser_error.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package errors
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"os"
66
"strings"
77

88
"github.com/mitchellh/go-wordwrap"
@@ -35,7 +35,7 @@ func (p *ParserError) Error() string {
3535

3636
err.WriteString(" " + fmt.Sprintf("%s:%d,%d\n", p.Filename, p.Line, p.Column))
3737
// process the file
38-
file, _ := ioutil.ReadFile(wordwrap.WrapString(p.Filename, 80))
38+
file, _ := os.ReadFile(wordwrap.WrapString(p.Filename, 80))
3939

4040
lines := strings.Split(string(file), "\n")
4141

functions.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/zclconf/go-cty/cty/function/stdlib"
1414
)
1515

16-
func createCtyFunctionFromGoFunc(f interface{}) (function.Function, error) {
16+
func createCtyFunctionFromGoFunc(f any) (function.Function, error) {
1717
// get the parameters
1818
inParams := []function.Parameter{}
1919
var outParam function.TypeFunc
@@ -287,7 +287,7 @@ func getDefaultFunctions(filePath string) map[string]function.Function {
287287
return cty.StringVal(""), fmt.Errorf("error parsing template: %s", err)
288288
}
289289

290-
tmpl.RegisterHelpers(map[string]interface{}{
290+
tmpl.RegisterHelpers(map[string]any{
291291
"quote": func(in string) string {
292292
return fmt.Sprintf(`"%s"`, in)
293293
},

functions_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func TestCreateFunctionCallsFunction(t *testing.T) {
8282
func TestCreateFunctionHandlesInputParams(t *testing.T) {
8383
type testCase struct {
8484
name string
85-
f interface{}
85+
f any
8686
}
8787

8888
cases := []testCase{

lookup/lookup.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ const (
2121
)
2222

2323
var (
24-
ErrMalformedIndex = errors.New("Malformed index key")
25-
ErrInvalidIndexUsage = errors.New("Invalid index key usage")
26-
ErrKeyNotFound = errors.New("Unable to find the key")
24+
ErrMalformedIndex = errors.New("malformed index key")
25+
ErrInvalidIndexUsage = errors.New("invalid index key usage")
26+
ErrKeyNotFound = errors.New("unable to find the key")
2727
)
2828

2929
// LookupString performs a lookup into a value, using a string. Same as `Lookup`
3030
// but using a string with the keys separated by `.`
31-
func LookupString(i interface{}, path string, tags []string) (reflect.Value, error) {
31+
func LookupString(i any, path string, tags []string) (reflect.Value, error) {
3232
return Lookup(i, strings.Split(path, SplitToken), tags)
3333
}
3434

3535
// LookupStringI is the same as LookupString, but the path is not case
3636
// sensitive.
37-
func LookupStringI(i interface{}, path string, tags []string) (reflect.Value, error) {
37+
func LookupStringI(i any, path string, tags []string) (reflect.Value, error) {
3838
return LookupI(i, strings.Split(path, SplitToken), tags)
3939
}
4040

@@ -44,16 +44,16 @@ func LookupStringI(i interface{}, path string, tags []string) (reflect.Value, er
4444
// If one key owns to a slice and an index is not
4545
// specified the rest of the path will be applied to eval value of the
4646
// slice, and the value will be merged into a slice.
47-
func Lookup(i interface{}, path []string, tags []string) (reflect.Value, error) {
47+
func Lookup(i any, path []string, tags []string) (reflect.Value, error) {
4848
return lookup(i, false, path, tags)
4949
}
5050

5151
// LookupI is the same as Lookup, but the path keys are not case sensitive.
52-
func LookupI(i interface{}, path []string, tags []string) (reflect.Value, error) {
52+
func LookupI(i any, path []string, tags []string) (reflect.Value, error) {
5353
return lookup(i, true, path, tags)
5454
}
5555

56-
func SetValueStringI[V int64 | int | string | bool | reflect.Value](i interface{}, v V, path string, tags []string) error {
56+
func SetValueStringI[V int64 | int | string | bool | reflect.Value](i any, v V, path string, tags []string) error {
5757
// First find the variable
5858
dest, err := Lookup(i, strings.Split(path, SplitToken), tags)
5959
if err != nil {
@@ -86,7 +86,7 @@ func SetValueStringI[V int64 | int | string | bool | reflect.Value](i interface{
8686
return nil
8787
}
8888

89-
func lookup(i interface{}, caseInsensitive bool, path []string, tags []string) (reflect.Value, error) {
89+
func lookup(i any, caseInsensitive bool, path []string, tags []string) (reflect.Value, error) {
9090
value := reflect.ValueOf(i)
9191
var parent reflect.Value
9292
var err error
@@ -275,7 +275,7 @@ func isMergeable(v reflect.Value) bool {
275275
}
276276

277277
func hasIndex(s string) bool {
278-
return strings.Index(s, IndexOpenChar) != -1
278+
return strings.Contains(s, IndexOpenChar)
279279
}
280280

281281
func parseIndex(s string) (string, int, error) {

lookup/lookup_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ type MyStruct struct {
465465
Map map[string]int `json:"jsontagmap" hcl:"hcltagmap"`
466466
Nested *MyStruct
467467
StructSlice []*MyStruct
468-
Interface interface{}
468+
Interface any
469469
Slice []string
470470
}
471471

@@ -485,11 +485,11 @@ var structFixture = MyStruct{
485485

486486
var emptyFixture = structs.Container{}
487487

488-
var mapComplexFixture = map[string]interface{}{
489-
"map": map[string]interface{}{
488+
var mapComplexFixture = map[string]any{
489+
"map": map[string]any{
490490
"bar": 1,
491491
},
492-
"list": []map[string]interface{}{
492+
"list": []map[string]any{
493493
{"baz": 1},
494494
{"baz": 2},
495495
{"baz": 3},

parse_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ func TestParseResolvesArrayReferences(t *testing.T) {
176176
require.NotNil(t, r)
177177

178178
out = r.(*resources.Output)
179-
require.Equal(t, "10.6.0.200", out.Value.([]interface{})[0].(string))
180-
require.Equal(t, "10.7.0.201", out.Value.([]interface{})[1].(string))
181-
require.Equal(t, float64(12), out.Value.([]interface{})[2].(float64))
179+
require.Equal(t, "10.6.0.200", out.Value.([]any)[0].(string))
180+
require.Equal(t, "10.7.0.201", out.Value.([]any)[1].(string))
181+
require.Equal(t, float64(12), out.Value.([]any)[2].(float64))
182182
}
183183

184184
func TestParseSetsDefaultValues(t *testing.T) {
@@ -299,14 +299,14 @@ func TestResourceReferencesInExpressionsAreEvaluated(t *testing.T) {
299299
r, err = c.FindResource("output.splat")
300300
require.NoError(t, err)
301301
cont := r.(*resources.Output)
302-
require.Equal(t, "/cache", cont.Value.([]interface{})[0])
303-
require.Equal(t, "/cache2", cont.Value.([]interface{})[1])
302+
require.Equal(t, "/cache", cont.Value.([]any)[0])
303+
require.Equal(t, "/cache2", cont.Value.([]any)[1])
304304

305305
r, err = c.FindResource("output.splat_with_null")
306306
require.NoError(t, err)
307307
cont = r.(*resources.Output)
308-
require.Equal(t, "test1", cont.Value.([]interface{})[0])
309-
require.Equal(t, "test2", cont.Value.([]interface{})[1])
308+
require.Equal(t, "test1", cont.Value.([]any)[0])
309+
require.Equal(t, "test2", cont.Value.([]any)[1])
310310

311311
r, err = c.FindResource("output.function")
312312
require.NoError(t, err)
@@ -487,7 +487,7 @@ func TestParseModuleCreatesOutputs(t *testing.T) {
487487

488488
// check element can be obtained from a map of values
489489
// returned in the output
490-
meta := cont.(*resources.Output).Value.(map[string]interface{})["meta"].(map[string]interface{})
490+
meta := cont.(*resources.Output).Value.(map[string]any)["meta"].(map[string]any)
491491
require.Equal(t, "base", meta["name"])
492492
}
493493

parser.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package hclconfig
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"path"
98
"path/filepath"
@@ -116,7 +115,7 @@ func (p *Parser) RegisterType(name string, resource types.Resource) {
116115
// RegisterFunction type registers a custom interpolation function
117116
// with the given name
118117
// the parser uses this list to convert hcl defined resources into concrete types
119-
func (p *Parser) RegisterFunction(name string, f interface{}) error {
118+
func (p *Parser) RegisterFunction(name string, f any) error {
120119
ctyFunc, err := createCtyFunctionFromGoFunc(f)
121120
if err != nil {
122121
return nil
@@ -243,13 +242,13 @@ func (p *Parser) UnmarshalJSON(d []byte) (*Config, error) {
243242
}
244243

245244
for _, m := range rawMessagesForResources {
246-
mm := map[string]interface{}{}
245+
mm := map[string]any{}
247246
err := json.Unmarshal(*m, &mm)
248247
if err != nil {
249248
return nil, err
250249
}
251250

252-
meta := mm["meta"].(map[string]interface{})
251+
meta := mm["meta"].(map[string]any)
253252

254253
r, err := p.registeredTypes.CreateResource(meta["type"].(string), meta["name"].(string))
255254
if err != nil {
@@ -278,7 +277,7 @@ func (p *Parser) parseDirectory(ctx *hcl.EvalContext, dir string, c *Config) []e
278277
return []error{fmt.Errorf("%s is not a directory", dir)}
279278
}
280279

281-
files, err := ioutil.ReadDir(dir)
280+
files, err := os.ReadDir(dir)
282281
if err != nil {
283282
return []error{fmt.Errorf("unable to list files in directory %s, error: %s", dir, err)}
284283
}
@@ -891,7 +890,7 @@ func (p *Parser) parseResource(ctx *hcl.EvalContext, c *Config, file string, b *
891890
Meta: types.Meta{
892891
Name: name,
893892
Type: b.Labels[0],
894-
Properties: map[string]interface{}{},
893+
Properties: map[string]any{},
895894
},
896895
}
897896

@@ -1266,7 +1265,7 @@ func buildContext(filePath string, customFunctions map[string]function.Function)
12661265
return ctx
12671266
}
12681267

1269-
func decodeBody(ctx *hcl.EvalContext, config *Config, path string, b *hclsyntax.Block, p interface{}, ignoreErrors bool) error {
1268+
func decodeBody(ctx *hcl.EvalContext, config *Config, path string, b *hclsyntax.Block, p any, ignoreErrors bool) error {
12701269
dr, err := getDependentResources(b, ctx, config, p, "")
12711270
if err != nil {
12721271
return err
@@ -1319,7 +1318,7 @@ func decodeBody(ctx *hcl.EvalContext, config *Config, path string, b *hclsyntax.
13191318
// i.e. resource.container.foo.network[0].name
13201319
// when a link is found it is replaced with an empty value of the correct type and the
13211320
// dependent resources are returned to be processed later
1322-
func getDependentResources(b *hclsyntax.Block, ctx *hcl.EvalContext, c *Config, resource interface{}, path string) ([]string, error) {
1321+
func getDependentResources(b *hclsyntax.Block, ctx *hcl.EvalContext, c *Config, resource any, path string) ([]string, error) {
13231322
references := []string{}
13241323

13251324
for _, a := range b.Body.Attributes {

resources/fqrn.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package resources
22

33
import (
4+
"errors"
45
"fmt"
56
"regexp"
67
"strings"
@@ -64,14 +65,14 @@ func ParseFQRN(fqrn string) (*FQRN, error) {
6465
}
6566

6667
if len(results) < 2 {
67-
return nil, fmt.Errorf(formatErrorString(fqrn))
68+
return nil, errors.New(formatErrorString(fqrn))
6869
}
6970

7071
switch results["resource"] {
7172
case "resource":
7273
resourceParts := strings.Split(results["attributes"], ".")
7374
if len(resourceParts) < 2 {
74-
return nil, fmt.Errorf(formatErrorString(fqrn))
75+
return nil, errors.New(formatErrorString(fqrn))
7576
}
7677

7778
typeName = resourceParts[0]
@@ -107,7 +108,7 @@ func ParseFQRN(fqrn string) (*FQRN, error) {
107108
case "variable":
108109
varParts := strings.Split(results["attributes"], ".")
109110
if len(varParts) != 1 {
110-
return nil, fmt.Errorf(formatErrorString(fqrn))
111+
return nil, errors.New(formatErrorString(fqrn))
111112
}
112113

113114
typeName = TypeVariable
@@ -116,7 +117,7 @@ func ParseFQRN(fqrn string) (*FQRN, error) {
116117

117118
default:
118119
if results["onlymodules"] == "" || !strings.HasPrefix(results["onlymodules"], "module.") {
119-
return nil, fmt.Errorf(formatErrorString(fqrn))
120+
return nil, errors.New(formatErrorString(fqrn))
120121
}
121122

122123
//module1.module2

resources/locals.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ const TypeLocal = "local"
1111
type Local struct {
1212
types.ResourceBase `hcl:",remain"`
1313

14-
CtyValue cty.Value `hcl:"value,optional"` // value of the output
15-
Value interface{} `json:"value"`
14+
CtyValue cty.Value `hcl:"value,optional"` // value of the output
15+
Value any `json:"value"`
1616
}

resources/module.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Module struct {
1616
Source string `hcl:"source" json:"source"`
1717
Version string `hcl:"version,optional" json:"version,omitempty"`
1818

19-
Variables interface{} `hcl:"variables,optional" json:"variables,omitempty"`
19+
Variables any `hcl:"variables,optional" json:"variables,omitempty"`
2020

2121
// SubContext is used to store the variables as a context that can be
2222
// passed to child resources

resources/output.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const TypeOutput = "output"
1111
type Output struct {
1212
types.ResourceBase `hcl:",remain"`
1313

14-
CtyValue cty.Value `hcl:"value,optional"` // value of the output
15-
Value interface{} `json:"value"`
16-
Description string `hcl:"description,optional" json:"description,omitempty"` // description for the output
14+
CtyValue cty.Value `hcl:"value,optional"` // value of the output
15+
Value any `json:"value"`
16+
Description string `hcl:"description,optional" json:"description,omitempty"` // description for the output
1717
}

resources/variable.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ const TypeVariable = "variable"
77
// Output defines an output variable which can be set by a module
88
type Variable struct {
99
types.ResourceBase `hcl:",remain"`
10-
Default interface{} `hcl:"default" json:"default"` // default value for a variable
11-
Description string `hcl:"description,optional" json:"description,omitempty"` // description of the variable
10+
Default any `hcl:"default" json:"default"` // default value for a variable
11+
Description string `hcl:"description,optional" json:"description,omitempty"` // description of the variable
1212
}

schematest/example.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import (
77
"encoding/json"
88
"fmt"
99
"reflect"
10-
"strings"
1110

12-
"github.com/hashicorp/hcl/v2/gohcl"
1311
"github.com/hashicorp/hcl/v2"
14-
"github.com/hashicorp/hcl/v2/hclsyntax"
12+
"github.com/hashicorp/hcl/v2/gohcl"
1513
"github.com/hashicorp/hcl/v2/hclparse"
14+
"github.com/hashicorp/hcl/v2/hclsyntax"
15+
"golang.org/x/text/cases"
16+
"golang.org/x/text/language"
1617
)
1718

1819
type Schema struct {
@@ -95,7 +96,7 @@ func createDynamicType(s *Schema) reflect.Type {
9596
t := getType(f.Type)
9697
if t != nil {
9798
field := reflect.StructField{
98-
Name: strings.Title(f.Name),
99+
Name: cases.Title(language.English).String(f.Name),
99100
Type: getType(f.Type),
100101
Tag: reflect.StructTag(fmt.Sprintf(`hcl:"%s,optional"`, f.Name)),
101102
}

test_fixtures/structs/template.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ type Template struct {
1414

1515
Depends []string `hcl:"depends_on,optional" json:"depends,omitempty"`
1616

17-
Source string `hcl:"source" json:"source"` // Source template to be processed as string
18-
Destination string `hcl:"destination" json:"destination"` // Destination filename to write
19-
Vars cty.Value `hcl:"vars,optional" json:"vars,omitempty"` // Variables to be processed in the template
20-
InternalVars map[string]interface{} // stores a converted go type version of the hcl.Value types
21-
AppendFile bool `hcl:"append_file,optional" json:"append_file,omitempty"`
17+
Source string `hcl:"source" json:"source"` // Source template to be processed as string
18+
Destination string `hcl:"destination" json:"destination"` // Destination filename to write
19+
Vars cty.Value `hcl:"vars,optional" json:"vars,omitempty"` // Variables to be processed in the template
20+
InternalVars map[string]any // stores a converted go type version of the hcl.Value types
21+
AppendFile bool `hcl:"append_file,optional" json:"append_file,omitempty"`
2222
}
2323

2424
func (t *Template) Process() error {

0 commit comments

Comments
 (0)