Use any instead of interface

Signed-off-by: Daniel Weiße <dw@edgeless.systems>
This commit is contained in:
Daniel Weiße 2022-04-28 10:28:28 +02:00 committed by Daniel Weiße
parent 8153390a57
commit 29206ac845
10 changed files with 19 additions and 19 deletions

View file

@ -20,7 +20,7 @@ type Marshaler interface {
}
// MarshalK8SResources marshals every field of a struct into a k8s resource YAML.
func MarshalK8SResources(resources interface{}) ([]byte, error) {
func MarshalK8SResources(resources any) ([]byte, error) {
if resources == nil {
return nil, errors.New("marshal on nil called")
}
@ -37,7 +37,7 @@ func MarshalK8SResources(resources interface{}) ([]byte, error) {
// iterate over all struct fields
for i := 0; i < elem.NumField(); i++ {
field := elem.Field(i)
var inter interface{}
var inter any
// check if value can be converted to interface
if field.CanInterface() {
inter = field.Addr().Interface()
@ -65,7 +65,7 @@ func MarshalK8SResources(resources interface{}) ([]byte, error) {
}
// UnmarshalK8SResources takes YAML and converts it into a k8s resources struct.
func UnmarshalK8SResources(data []byte, into interface{}) error {
func UnmarshalK8SResources(data []byte, into any) error {
if into == nil {
return errors.New("unmarshal on nil called")
}
@ -86,7 +86,7 @@ func UnmarshalK8SResources(data []byte, into interface{}) error {
for i := 0; i < value.NumField(); i++ {
field := value.Field(i)
var inter interface{}
var inter any
// check if value can be converted to interface
if !field.CanInterface() {
return fmt.Errorf("cannot use struct field %v as interface", i)
@ -131,7 +131,7 @@ func splitYAML(resources []byte) ([][]byte, error) {
dec := yaml.NewDecoder(bytes.NewReader(resources))
var res [][]byte
for {
var value interface{}
var value any
err := dec.Decode(&value)
if errors.Is(err, io.EOF) {
break

View file

@ -13,7 +13,7 @@ import (
func TestMarshalK8SResources(t *testing.T) {
testCases := map[string]struct {
resources interface{}
resources any
wantErr bool
wantYAML string
}{
@ -119,8 +119,8 @@ metadata:
func TestUnmarshalK8SResources(t *testing.T) {
testCases := map[string]struct {
data string
into interface{}
wantObj interface{}
into any
wantObj any
wantErr bool
}{
"ConfigMap as only field can be unmarshaled": {