Skip to content

Commit

Permalink
chore: use any instead of interface
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly committed Jan 8, 2024
1 parent d5e9eb1 commit 937635b
Show file tree
Hide file tree
Showing 23 changed files with 260 additions and 260 deletions.
4 changes: 2 additions & 2 deletions pkg/engine/assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
)

func Assert(ctx context.Context, assertion Assertion, value interface{}, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {
func Assert(ctx context.Context, assertion Assertion, value any, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {

Check warning on line 11 in pkg/engine/assert/assert.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/assert.go#L11

Added line #L11 was not covered by tests
return assert(ctx, nil, assertion, value, bindings, opts...)
}

func assert(ctx context.Context, path *field.Path, assertion Assertion, value interface{}, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {
func assert(ctx context.Context, path *field.Path, assertion Assertion, value any, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {

Check warning on line 15 in pkg/engine/assert/assert.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/assert.go#L15

Added line #L15 was not covered by tests
return assertion.assert(ctx, path, value, bindings, opts...)
}
2 changes: 1 addition & 1 deletion pkg/engine/assert/assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (
)

type Assertion interface {
assert(context.Context, *field.Path, interface{}, binding.Bindings, ...template.Option) (field.ErrorList, error)
assert(context.Context, *field.Path, any, binding.Bindings, ...template.Option) (field.ErrorList, error)
}
6 changes: 3 additions & 3 deletions pkg/engine/assert/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
)

func NewContextBindings(bindings binding.Bindings, value interface{}, entries ...v1alpha1.ContextEntry) binding.Bindings {
func NewContextBindings(bindings binding.Bindings, value any, entries ...v1alpha1.ContextEntry) binding.Bindings {

Check warning on line 12 in pkg/engine/assert/binding.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/binding.go#L12

Added line #L12 was not covered by tests
var path *field.Path
path = path.Child("context")
for i, entry := range entries {
Expand All @@ -18,9 +18,9 @@ func NewContextBindings(bindings binding.Bindings, value interface{}, entries ..
return bindings
}

func NewContextBinding(path *field.Path, bindings binding.Bindings, value interface{}, entry v1alpha1.ContextEntry) binding.Binding {
func NewContextBinding(path *field.Path, bindings binding.Bindings, value any, entry v1alpha1.ContextEntry) binding.Binding {

Check warning on line 21 in pkg/engine/assert/binding.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/binding.go#L21

Added line #L21 was not covered by tests
return template.NewLazyBinding(
func() (interface{}, error) {
func() (any, error) {

Check warning on line 23 in pkg/engine/assert/binding.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/binding.go#L23

Added line #L23 was not covered by tests
expression := parseExpression(context.TODO(), entry.Variable.Value)
if expression != nil && expression.engine != "" {
if expression.foreach {
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/assert/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func parseExpressionRegex(ctx context.Context, in string) *expression {
return expression
}

func parseExpression(ctx context.Context, value interface{}) *expression {
func parseExpression(ctx context.Context, value any) *expression {
if reflectutils.GetKind(value) != reflect.String {
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/engine/assert/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// We should remove this dependency.
// Either move the Match struct in this package or move this file in a more specific package.

func MatchAssert(ctx context.Context, path *field.Path, match *v1alpha1.Assert, actual interface{}, bindings binding.Bindings, opts ...template.Option) ([]error, error) {
func MatchAssert(ctx context.Context, path *field.Path, match *v1alpha1.Assert, actual any, bindings binding.Bindings, opts ...template.Option) ([]error, error) {

Check warning on line 17 in pkg/engine/assert/match.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/match.go#L17

Added line #L17 was not covered by tests
if match == nil || (len(match.Any) == 0 && len(match.All) == 0) {
return nil, field.Invalid(path, match, "an empty assert is not valid")
} else {
Expand Down Expand Up @@ -66,7 +66,7 @@ func MatchAssert(ctx context.Context, path *field.Path, match *v1alpha1.Assert,
}
}

func Match(ctx context.Context, path *field.Path, match *v1alpha1.Match, actual interface{}, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {
func Match(ctx context.Context, path *field.Path, match *v1alpha1.Match, actual any, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {

Check warning on line 69 in pkg/engine/assert/match.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/match.go#L69

Added line #L69 was not covered by tests
if match == nil || (len(match.Any) == 0 && len(match.All) == 0) {
return nil, field.Invalid(path, match, "an empty match is not valid")
} else {
Expand All @@ -89,7 +89,7 @@ func Match(ctx context.Context, path *field.Path, match *v1alpha1.Match, actual
}
}

func MatchAny(ctx context.Context, path *field.Path, assertions []v1alpha1.Any, actual interface{}, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {
func MatchAny(ctx context.Context, path *field.Path, assertions []v1alpha1.Any, actual any, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {

Check warning on line 92 in pkg/engine/assert/match.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/match.go#L92

Added line #L92 was not covered by tests
var errs field.ErrorList
for i, assertion := range assertions {
_errs, err := assert(ctx, path.Index(i), Parse(ctx, assertion.Value), actual, bindings, opts...)
Expand All @@ -104,7 +104,7 @@ func MatchAny(ctx context.Context, path *field.Path, assertions []v1alpha1.Any,
return errs, nil
}

func MatchAll(ctx context.Context, path *field.Path, assertions []v1alpha1.Any, actual interface{}, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {
func MatchAll(ctx context.Context, path *field.Path, assertions []v1alpha1.Any, actual any, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {

Check warning on line 107 in pkg/engine/assert/match.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/match.go#L107

Added line #L107 was not covered by tests
var errs field.ErrorList
for i, assertion := range assertions {
_errs, err := assert(ctx, path.Index(i), Parse(ctx, assertion.Value), actual, bindings, opts...)
Expand Down
14 changes: 7 additions & 7 deletions pkg/engine/assert/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
)

func Parse(ctx context.Context, assertion interface{}) Assertion {
func Parse(ctx context.Context, assertion any) Assertion {

Check warning on line 16 in pkg/engine/assert/parse.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/parse.go#L16

Added line #L16 was not covered by tests
switch reflectutils.GetKind(assertion) {
case reflect.Slice:
node := sliceNode{}
Expand All @@ -36,9 +36,9 @@ func Parse(ctx context.Context, assertion interface{}) Assertion {

// mapNode is the assertion type represented by a map.
// it is responsible for projecting the analysed resource and passing the result to the descendant
type mapNode map[interface{}]Assertion
type mapNode map[any]Assertion

func (n mapNode) assert(ctx context.Context, path *field.Path, value interface{}, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {
func (n mapNode) assert(ctx context.Context, path *field.Path, value any, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {

Check warning on line 41 in pkg/engine/assert/parse.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/parse.go#L41

Added line #L41 was not covered by tests
var errs field.ErrorList
for k, v := range n {
projection, err := project(ctx, k, value, bindings, opts...)
Expand Down Expand Up @@ -97,7 +97,7 @@ func (n mapNode) assert(ctx context.Context, path *field.Path, value interface{}
// if lengths match all descendants are evaluated with their corresponding items.
type sliceNode []Assertion

func (n sliceNode) assert(ctx context.Context, path *field.Path, value interface{}, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {
func (n sliceNode) assert(ctx context.Context, path *field.Path, value any, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {

Check warning on line 100 in pkg/engine/assert/parse.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/parse.go#L100

Added line #L100 was not covered by tests
var errs field.ErrorList
if value == nil {
errs = append(errs, field.Invalid(path, value, "value is null"))
Expand All @@ -124,10 +124,10 @@ func (n sliceNode) assert(ctx context.Context, path *field.Path, value interface
// it receives a value and compares it with an expected value.
// the expected value can be the result of an expression.
type scalarNode struct {
rhs interface{}
rhs any
}

func (n *scalarNode) assert(ctx context.Context, path *field.Path, value interface{}, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {
func (n *scalarNode) assert(ctx context.Context, path *field.Path, value any, bindings binding.Bindings, opts ...template.Option) (field.ErrorList, error) {

Check warning on line 130 in pkg/engine/assert/parse.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/parse.go#L130

Added line #L130 was not covered by tests
rhs := n.rhs
expression := parseExpression(ctx, rhs)
// we only project if the expression uses the engine syntax
Expand All @@ -154,7 +154,7 @@ func (n *scalarNode) assert(ctx context.Context, path *field.Path, value interfa
return errs, nil
}

func expectValueMessage(value interface{}) string {
func expectValueMessage(value any) string {

Check warning on line 157 in pkg/engine/assert/parse.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/parse.go#L157

Added line #L157 was not covered by tests
switch t := value.(type) {
case int64, int32, float64, float32, bool:
// use simple printer for simple types
Expand Down
8 changes: 4 additions & 4 deletions pkg/engine/assert/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ type projection struct {
foreach bool
foreachName string
binding string
result interface{}
result any
}

func project(ctx context.Context, key interface{}, value interface{}, bindings binding.Bindings, opts ...template.Option) (*projection, error) {
func project(ctx context.Context, key any, value any, bindings binding.Bindings, opts ...template.Option) (*projection, error) {
expression := parseExpression(ctx, key)
if expression != nil {
if expression.engine != "" {
Expand All @@ -33,7 +33,7 @@ func project(ctx context.Context, key interface{}, value interface{}, bindings b
} else {
if reflectutils.GetKind(value) == reflect.Map {
mapValue := reflect.ValueOf(value).MapIndex(reflect.ValueOf(expression.statement))
var value interface{}
var value any
if mapValue.IsValid() {
value = mapValue.Interface()
}
Expand All @@ -48,7 +48,7 @@ func project(ctx context.Context, key interface{}, value interface{}, bindings b
}
if reflectutils.GetKind(value) == reflect.Map {
mapValue := reflect.ValueOf(value).MapIndex(reflect.ValueOf(key))
var value interface{}
var value any

Check warning on line 51 in pkg/engine/assert/project.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/project.go#L51

Added line #L51 was not covered by tests
if mapValue.IsValid() {
value = mapValue.Interface()
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/engine/assert/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
func Test_project(t *testing.T) {
tests := []struct {
name string
key interface{}
value interface{}
key any
value any
bindings binding.Bindings
want *projection
wantErr bool
}{{
name: "map index not found",
key: "foo",
value: map[string]interface{}{
value: map[string]any{
"bar": 42,
},
bindings: nil,
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/match/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
reflectutils "github.com/kyverno/kyverno-json/pkg/utils/reflect"
)

func Match(ctx context.Context, expected, actual interface{}) (bool, error) {
func Match(ctx context.Context, expected, actual any) (bool, error) {
if expected != nil {
switch reflectutils.GetKind(expected) {
case reflect.Slice:
Expand Down
12 changes: 6 additions & 6 deletions pkg/engine/template/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ import (
"github.com/jmespath-community/go-jmespath/pkg/binding"
)

type resolverFunc = func() (interface{}, error)
type resolverFunc = func() (any, error)

type lazyBinding struct {
resolver resolverFunc
}

func (b *lazyBinding) Value() (interface{}, error) {
func (b *lazyBinding) Value() (any, error) {
return b.resolver()
}

func NewLazyBinding(resolver resolverFunc) binding.Binding {
binding := &lazyBinding{}
lock := &sync.Mutex{}
binding.resolver = func() (interface{}, error) {
binding.resolver = func() (any, error) {
lock.Lock()
defer lock.Unlock()
value, err := resolver()
binding.resolver = func() (interface{}, error) {
binding.resolver = func() (any, error) {
return value, err
}
return binding.resolver()
}
return binding
}

func NewLazyBindingWithValue(value interface{}) binding.Binding {
return NewLazyBinding(func() (interface{}, error) {
func NewLazyBindingWithValue(value any) binding.Binding {
return NewLazyBinding(func() (any, error) {
return value, nil
})
}
4 changes: 2 additions & 2 deletions pkg/engine/template/functions/at.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"errors"
)

func jpfAt(arguments []interface{}) (interface{}, error) {
if slice, ok := arguments[0].([]interface{}); !ok {
func jpfAt(arguments []any) (any, error) {
if slice, ok := arguments[0].([]any); !ok {
return nil, errors.New("invalid type, first argument must be an array")
} else if index, ok := arguments[1].(int); !ok {
return nil, errors.New("invalid type, second argument must be an int")
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/template/functions/concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
)

func jpfConcat(arguments []interface{}) (interface{}, error) {
func jpfConcat(arguments []any) (any, error) {
if left, ok := arguments[0].(string); !ok {
return nil, errors.New("invalid type, first argument must be a string")
} else if right, ok := arguments[1].(string); !ok {
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/template/functions/json_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"errors"
)

func jpfJsonParse(arguments []interface{}) (interface{}, error) {
func jpfJsonParse(arguments []any) (any, error) {
if data, ok := arguments[0].(string); !ok {
return nil, errors.New("invalid type, first argument must be a string")
} else {
var result interface{}
var result any
err := json.Unmarshal([]byte(data), &result)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/template/functions/wildcard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/kyverno/kyverno/ext/wildcard"
)

func jpfWildcard(arguments []interface{}) (interface{}, error) {
func jpfWildcard(arguments []any) (any, error) {
if pattern, ok := arguments[0].(string); !ok {
return nil, errors.New("invalid type, first argument must be a string")
} else if name, ok := arguments[1].(string); !ok {
Expand Down
Loading

0 comments on commit 937635b

Please sign in to comment.