Skip to content

Commit c920631

Browse files
committed
refactor: replace interface{} with any
Signed-off-by: Tiago Silva <tiago.silva@goteleport.com>
1 parent 664e4d3 commit c920631

File tree

5 files changed

+59
-59
lines changed

5 files changed

+59
-59
lines changed

lib.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
// from map[string]string or map[string][]string
3030
// the function returns empty value in case if key not found
3131
// In case if map is nil, returns empty value as well.
32-
func GetStringMapValue(mapVal, keyVal interface{}) (interface{}, error) {
32+
func GetStringMapValue(mapVal, keyVal any) (any, error) {
3333
key, ok := keyVal.(string)
3434
if !ok {
3535
return nil, trace.BadParameter("only string keys are supported")
@@ -58,7 +58,7 @@ type BoolPredicate func() bool
5858

5959
// Equals can compare complex objects, e.g. arrays of strings
6060
// and strings together.
61-
func Equals(a interface{}, b interface{}) BoolPredicate {
61+
func Equals(a any, b any) BoolPredicate {
6262
return func() bool {
6363
switch aval := a.(type) {
6464
case string:
@@ -86,7 +86,7 @@ func Equals(a interface{}, b interface{}) BoolPredicate {
8686

8787
// Contains checks if string slice contains a string
8888
// Contains([]string{"a", "b"}, "b") -> true.
89-
func Contains(a interface{}, b interface{}) BoolPredicate {
89+
func Contains(a any, b any) BoolPredicate {
9090
return func() bool {
9191
aval, ok := a.([]string)
9292
if !ok {
@@ -130,7 +130,7 @@ func Not(a BoolPredicate) BoolPredicate {
130130
}
131131

132132
// GetFieldByTag returns a field from the object based on the tag.
133-
func GetFieldByTag(ival interface{}, tagName string, fieldNames []string) (interface{}, error) {
133+
func GetFieldByTag(ival any, tagName string, fieldNames []string) (any, error) {
134134
i, err := getFieldByTag(reflect.ValueOf(ival), tagName, fieldNames)
135135
if err == nil {
136136
return i, nil
@@ -161,7 +161,7 @@ func (n notFoundError) Error() string {
161161
return fmt.Sprintf("field name %v is not found", strings.Join(n.fieldNames, "."))
162162
}
163163

164-
func getFieldByTag(val reflect.Value, tagName string, fieldNames []string) (interface{}, error) {
164+
func getFieldByTag(val reflect.Value, tagName string, fieldNames []string) (any, error) {
165165
if len(fieldNames) == 0 {
166166
return nil, trace.BadParameter("missing field names")
167167
}

methods_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ func TestMethods(t *testing.T) {
1111
t.Parallel()
1212

1313
p, err := NewParser(Def{
14-
Functions: map[string]interface{}{
14+
Functions: map[string]any{
1515
"set": newSet,
1616
"list": newList,
1717
"append": customAppend,
1818
},
19-
Methods: map[string]interface{}{
19+
Methods: map[string]any{
2020
"add": set.add,
2121
"append": list.append,
2222
"contains": container.contains,
2323
},
24-
GetIdentifier: func(selector []string) (interface{}, error) {
24+
GetIdentifier: func(selector []string) (any, error) {
2525
if len(selector) == 1 && selector[0] == "fruits" {
2626
return newSet("apples", "bananas"), nil
2727
}
@@ -34,7 +34,7 @@ func TestMethods(t *testing.T) {
3434
desc string
3535
input string
3636
expectError bool
37-
expectOutput interface{}
37+
expectOutput any
3838
}{
3939
{
4040
desc: "basic method call",

parse.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type predicateParser struct {
2020
d Def
2121
}
2222

23-
func (p *predicateParser) Parse(in string) (interface{}, error) {
23+
func (p *predicateParser) Parse(in string) (any, error) {
2424
expr, err := parser.ParseExpr(in)
2525
if err != nil {
2626
return nil, err
@@ -29,7 +29,7 @@ func (p *predicateParser) Parse(in string) (interface{}, error) {
2929
return p.parse(expr)
3030
}
3131

32-
func (p *predicateParser) parse(expr ast.Expr) (interface{}, error) {
32+
func (p *predicateParser) parse(expr ast.Expr) (any, error) {
3333
switch n := expr.(type) {
3434
case *ast.BinaryExpr:
3535
val, err := p.parseBinaryExpr(n)
@@ -71,7 +71,7 @@ func (p *predicateParser) parse(expr ast.Expr) (interface{}, error) {
7171
}
7272
}
7373

74-
func (p *predicateParser) parseBinaryExpr(expr *ast.BinaryExpr) (interface{}, error) {
74+
func (p *predicateParser) parseBinaryExpr(expr *ast.BinaryExpr) (any, error) {
7575
x, err := p.parse(expr.X)
7676
if err != nil {
7777
return nil, err
@@ -86,7 +86,7 @@ func (p *predicateParser) parseBinaryExpr(expr *ast.BinaryExpr) (interface{}, er
8686
return val, trace.Wrap(err)
8787
}
8888

89-
func (p *predicateParser) parseUnaryExpr(expr *ast.UnaryExpr) (interface{}, error) {
89+
func (p *predicateParser) parseUnaryExpr(expr *ast.UnaryExpr) (any, error) {
9090
joinFn, err := p.getJoinFunction(expr.Op)
9191
if err != nil {
9292
return nil, err
@@ -97,11 +97,11 @@ func (p *predicateParser) parseUnaryExpr(expr *ast.UnaryExpr) (interface{}, erro
9797
return nil, err
9898
}
9999

100-
val, err := callFunction(joinFn, []interface{}{node})
100+
val, err := callFunction(joinFn, []any{node})
101101
return val, trace.Wrap(err)
102102
}
103103

104-
func (p *predicateParser) parseIndexExpr(expr *ast.IndexExpr) (interface{}, error) {
104+
func (p *predicateParser) parseIndexExpr(expr *ast.IndexExpr) (any, error) {
105105
if p.d.GetProperty == nil {
106106
return nil, trace.NotFound("properties are not supported")
107107
}
@@ -124,8 +124,8 @@ func (p *predicateParser) parseIndexExpr(expr *ast.IndexExpr) (interface{}, erro
124124
return val, nil
125125
}
126126

127-
func (p *predicateParser) evaluateArguments(nodes []ast.Expr) ([]interface{}, error) {
128-
out := make([]interface{}, len(nodes))
127+
func (p *predicateParser) evaluateArguments(nodes []ast.Expr) ([]any, error) {
128+
out := make([]any, len(nodes))
129129
for i, n := range nodes {
130130
val, err := p.parse(n)
131131
if err != nil {
@@ -136,7 +136,7 @@ func (p *predicateParser) evaluateArguments(nodes []ast.Expr) ([]interface{}, er
136136
return out, nil
137137
}
138138

139-
func (p *predicateParser) parseSelectorExpr(expr *ast.SelectorExpr) (interface{}, error) {
139+
func (p *predicateParser) parseSelectorExpr(expr *ast.SelectorExpr) (any, error) {
140140
fields, err := evaluateSelector(expr, []string{})
141141
if err != nil {
142142
return nil, trace.Wrap(err)
@@ -170,7 +170,7 @@ func evaluateSelector(sel *ast.SelectorExpr, fields []string) ([]string, error)
170170
}
171171
}
172172

173-
func (p *predicateParser) parseCallExpr(expr *ast.CallExpr) (interface{}, error) {
173+
func (p *predicateParser) parseCallExpr(expr *ast.CallExpr) (any, error) {
174174
fn, args, err := p.getFunctionAndArgs(expr)
175175
if err != nil {
176176
return nil, trace.Wrap(err)
@@ -185,25 +185,25 @@ func (p *predicateParser) parseCallExpr(expr *ast.CallExpr) (interface{}, error)
185185
return val, trace.Wrap(err)
186186
}
187187

188-
func (p *predicateParser) getFunction(name string) (interface{}, error) {
188+
func (p *predicateParser) getFunction(name string) (any, error) {
189189
v, ok := p.d.Functions[name]
190190
if !ok {
191191
return nil, trace.BadParameter("unsupported function: %s", name)
192192
}
193193
return v, nil
194194
}
195195

196-
func (p *predicateParser) joinPredicates(op token.Token, a, b interface{}) (interface{}, error) {
196+
func (p *predicateParser) joinPredicates(op token.Token, a, b any) (any, error) {
197197
joinFn, err := p.getJoinFunction(op)
198198
if err != nil {
199199
return nil, err
200200
}
201201

202-
return callFunction(joinFn, []interface{}{a, b})
202+
return callFunction(joinFn, []any{a, b})
203203
}
204204

205-
func (p *predicateParser) getJoinFunction(op token.Token) (interface{}, error) {
206-
var fn interface{}
205+
func (p *predicateParser) getJoinFunction(op token.Token) (any, error) {
206+
var fn any
207207
switch op {
208208
case token.NOT:
209209
fn = p.d.Operators.NOT
@@ -230,7 +230,7 @@ func (p *predicateParser) getJoinFunction(op token.Token) (interface{}, error) {
230230
return fn, nil
231231
}
232232

233-
func (p *predicateParser) getFunctionAndArgs(callExpr *ast.CallExpr) (interface{}, []ast.Expr, error) {
233+
func (p *predicateParser) getFunctionAndArgs(callExpr *ast.CallExpr) (any, []ast.Expr, error) {
234234
switch f := callExpr.Fun.(type) {
235235
case *ast.Ident:
236236
// Plain function with a single identifier name.
@@ -261,7 +261,7 @@ func (p *predicateParser) getFunctionAndArgs(callExpr *ast.CallExpr) (interface{
261261
}
262262
}
263263

264-
func literalToValue(a *ast.BasicLit) (interface{}, error) {
264+
func literalToValue(a *ast.BasicLit) (any, error) {
265265
switch a.Kind {
266266
case token.FLOAT:
267267
value, err := strconv.ParseFloat(a.Value, 64)
@@ -288,7 +288,7 @@ func literalToValue(a *ast.BasicLit) (interface{}, error) {
288288
return nil, trace.BadParameter("unsupported function argument type: '%v'", a.Kind)
289289
}
290290

291-
func callFunction(f interface{}, args []interface{}) (v interface{}, err error) {
291+
func callFunction(f any, args []any) (v any, err error) {
292292
defer func() {
293293
if r := recover(); r != nil {
294294
err = trace.BadParameter("%s", r)

parse_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ func (s *PredicateSuite) getParserWithOpts(getID GetIdentifierFn, getProperty Ge
3434
GE: numberGE,
3535
NOT: numberNOT,
3636
},
37-
Functions: map[string]interface{}{
37+
Functions: map[string]any{
3838
"DivisibleBy": divisibleBy,
3939
"Remainder": numberRemainder,
4040
"Len": stringLength,
4141
"number.DivisibleBy": divisibleBy,
4242
"Equals": Equals,
4343
"Contains": Contains,
44-
"fnreturn": func(arg interface{}) (interface{}, error) {
44+
"fnreturn": func(arg any) (any, error) {
4545
return arg, nil
4646
},
47-
"fnerr": func(arg interface{}) (interface{}, error) {
47+
"fnerr": func(arg any) (any, error) {
4848
return nil, trace.BadParameter("don't like this parameter")
4949
},
5050
},
@@ -284,7 +284,7 @@ func (s *PredicateSuite) TestGTFloat64() {
284284
}
285285

286286
func (s *PredicateSuite) TestSelectExpr() {
287-
getID := func(fields []string) (interface{}, error) {
287+
getID := func(fields []string) (any, error) {
288288
s.Equal([]string{"first", "second", "third"}, fields)
289289
return 2, nil
290290
}
@@ -312,11 +312,11 @@ func (s *PredicateSuite) TestSelectExpr() {
312312
}
313313

314314
func (s *PredicateSuite) TestIndexExpr() {
315-
getID := func(fields []string) (interface{}, error) {
315+
getID := func(fields []string) (any, error) {
316316
s.Equal([]string{"first", "second"}, fields)
317317
return map[string]int{"key": 2}, nil
318318
}
319-
getProperty := func(mapVal, keyVal interface{}) (interface{}, error) {
319+
getProperty := func(mapVal, keyVal any) (any, error) {
320320
m := mapVal.(map[string]int)
321321
k := keyVal.(string)
322322
return m[k], nil
@@ -346,7 +346,7 @@ func (s *PredicateSuite) TestIndexExpr() {
346346
}
347347

348348
func (s *PredicateSuite) TestIdentifierExpr() {
349-
getID := func(fields []string) (interface{}, error) {
349+
getID := func(fields []string) (any, error) {
350350
switch fields[0] {
351351
case "firstSlice":
352352
return []string{"a"}, nil
@@ -390,7 +390,7 @@ func (s *PredicateSuite) TestContains() {
390390
val := TestStruct{}
391391
val.Param.Key1 = map[string][]string{"key": {"a", "b", "c"}}
392392

393-
getID := func(fields []string) (interface{}, error) {
393+
getID := func(fields []string) (any, error) {
394394
return GetFieldByTag(val, "json", fields[1:])
395395
}
396396
p := s.getParserWithOpts(getID, GetStringMapValue)
@@ -429,7 +429,7 @@ func (s *PredicateSuite) TestContainsUnexportedFieldAvoidPanic() {
429429
},
430430
}
431431

432-
getID := func(fields []string) (interface{}, error) {
432+
getID := func(fields []string) (any, error) {
433433
return GetFieldByTag(val, "json", fields[1:])
434434
}
435435
p := s.getParserWithOpts(getID, GetStringMapValue)
@@ -443,7 +443,7 @@ func (s *PredicateSuite) TestEquals() {
443443
val := TestStruct{}
444444
val.Param.Key2 = map[string]string{"key": "a"}
445445

446-
getID := func(fields []string) (interface{}, error) {
446+
getID := func(fields []string) (any, error) {
447447
return GetFieldByTag(val, "json", fields[1:])
448448
}
449449
p := s.getParserWithOpts(getID, GetStringMapValue)
@@ -489,8 +489,8 @@ func (s *PredicateSuite) TestGetTagField() {
489489
type testCase struct {
490490
tag string
491491
fields []string
492-
val interface{}
493-
expect interface{}
492+
val any
493+
expect any
494494
err error
495495
}
496496
testCases := []testCase{
@@ -580,7 +580,7 @@ func numberRemainder(divideBy int) numberMapper {
580580
}
581581
}
582582

583-
func numberGT(m numberMapper, value interface{}) (numberPredicate, error) {
583+
func numberGT(m numberMapper, value any) (numberPredicate, error) {
584584
switch value.(type) {
585585
case int:
586586
case float64:
@@ -642,12 +642,12 @@ func TestNestedExpressions(t *testing.T) {
642642
OR: Or,
643643
NOT: Not,
644644
},
645-
Functions: map[string]interface{}{
646-
"fnreturn": func(arg interface{}) interface{} {
645+
Functions: map[string]any{
646+
"fnreturn": func(arg any) any {
647647
return arg
648648
},
649649
},
650-
GetIdentifier: func(fields []string) (interface{}, error) {
650+
GetIdentifier: func(fields []string) (any, error) {
651651
if len(fields) != 1 {
652652
return nil, fmt.Errorf("identifier with multiple fields unsupported")
653653
}
@@ -665,7 +665,7 @@ func TestNestedExpressions(t *testing.T) {
665665
return nil, fmt.Errorf("identifier %q not found", fields[0])
666666
}
667667
},
668-
GetProperty: func(mapVal, keyVal interface{}) (interface{}, error) {
668+
GetProperty: func(mapVal, keyVal any) (any, error) {
669669
m, ok := mapVal.(map[bool]bool)
670670
if !ok {
671671
return nil, fmt.Errorf("only map[bool]bool is supported for this test, got %T", mapVal)
@@ -682,7 +682,7 @@ func TestNestedExpressions(t *testing.T) {
682682
for _, tc := range []struct {
683683
desc string
684684
expr string
685-
expected interface{}
685+
expected any
686686
}{
687687
{
688688
desc: "unary expr as arg",

predicate.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ package predicate
6565
type Def struct {
6666
Operators Operators
6767
// Function matching is case sensitive, e.g. Len is different from len
68-
Functions map[string]interface{}
68+
Functions map[string]any
6969
// Methods is a map of method names to their implementation.
70-
Methods map[string]interface{}
70+
Methods map[string]any
7171
// GetIdentifier returns value of any identifier passed in the form
7272
// []string{"id", "field", "subfield"}
7373
GetIdentifier GetIdentifierFn
@@ -78,28 +78,28 @@ type Def struct {
7878
// GetIdentifierFn function returns identifier based on selector
7979
// e.g. id.field.subfield will be passed as.
8080
// GetIdentifierFn([]string{"id", "field", "subfield"}).
81-
type GetIdentifierFn func(selector []string) (interface{}, error)
81+
type GetIdentifierFn func(selector []string) (any, error)
8282

8383
// GetPropertyFn returns property from a mapVal by key keyVal.
84-
type GetPropertyFn func(mapVal, keyVal interface{}) (interface{}, error)
84+
type GetPropertyFn func(mapVal, keyVal any) (any, error)
8585

8686
// Operators contain functions for equality and logical comparison.
8787
type Operators struct {
88-
EQ interface{}
89-
NEQ interface{}
88+
EQ any
89+
NEQ any
9090

91-
LT interface{}
92-
GT interface{}
91+
LT any
92+
GT any
9393

94-
LE interface{}
95-
GE interface{}
94+
LE any
95+
GE any
9696

97-
OR interface{}
98-
AND interface{}
99-
NOT interface{}
97+
OR any
98+
AND any
99+
NOT any
100100
}
101101

102102
// Parser takes the string with expression and calls the operators and functions.
103103
type Parser interface {
104-
Parse(string) (interface{}, error)
104+
Parse(string) (any, error)
105105
}

0 commit comments

Comments
 (0)