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

@ -155,6 +155,6 @@ func (v *Validators) checkPCRs(pcrs map[uint32][]byte) error {
return nil return nil
} }
func writeFmt(sb *strings.Builder, fmtStr string, args ...interface{}) { func writeFmt(sb *strings.Builder, fmtStr string, args ...any) {
sb.WriteString(fmt.Sprintf(fmtStr, args...)) sb.WriteString(fmt.Sprintf(fmtStr, args...))
} }

View File

@ -76,7 +76,7 @@ func (h *Handler) Write(name string, data []byte, options Option) error {
// ReadJSON reads a JSON file from name and unmarshals it into the content interface. // ReadJSON reads a JSON file from name and unmarshals it into the content interface.
// The interface content must be a pointer to a JSON marchalable object. // The interface content must be a pointer to a JSON marchalable object.
func (h *Handler) ReadJSON(name string, content interface{}) error { func (h *Handler) ReadJSON(name string, content any) error {
data, err := h.Read(name) data, err := h.Read(name)
if err != nil { if err != nil {
return err return err
@ -85,7 +85,7 @@ func (h *Handler) ReadJSON(name string, content interface{}) error {
} }
// WriteJSON marshals the content interface to JSON and writes it to the path with the given name. // WriteJSON marshals the content interface to JSON and writes it to the path with the given name.
func (h *Handler) WriteJSON(name string, content interface{}, options Option) error { func (h *Handler) WriteJSON(name string, content any, options Option) error {
jsonData, err := json.MarshalIndent(content, "", "\t") jsonData, err := json.MarshalIndent(content, "", "\t")
if err != nil { if err != nil {
return err return err

View File

@ -25,7 +25,7 @@ func TestReadJSON(t *testing.T) {
fs afero.Fs fs afero.Fs
setupFs func(fs *afero.Afero) error setupFs func(fs *afero.Afero) error
name string name string
wantContent interface{} wantContent any
wantErr bool wantErr bool
}{ }{
"successful read": { "successful read": {
@ -83,7 +83,7 @@ func TestWriteJSON(t *testing.T) {
fs afero.Fs fs afero.Fs
setupFs func(af afero.Afero) error setupFs func(af afero.Afero) error
name string name string
content interface{} content any
options Option options Option
wantErr bool wantErr bool
}{ }{

View File

@ -142,7 +142,7 @@ func stubNewConnFunc(errStub error) func(ctx context.Context, target string, opt
type stubClientConn struct{} type stubClientConn struct{}
func (c *stubClientConn) Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...grpc.CallOption) error { func (c *stubClientConn) Invoke(ctx context.Context, method string, args any, reply any, opts ...grpc.CallOption) error {
return nil return nil
} }

View File

@ -148,7 +148,7 @@ type Validator interface {
Validate(attDoc []byte, nonce []byte) ([]byte, error) Validate(attDoc []byte, nonce []byte) ([]byte, error)
} }
func hashPublicKey(pub interface{}) ([]byte, error) { func hashPublicKey(pub any) ([]byte, error) {
pubBytes, err := x509.MarshalPKIXPublicKey(pub) pubBytes, err := x509.MarshalPKIXPublicKey(pub)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -156,7 +156,7 @@ Y+t5OxL3kL15VzY1Ob0d5cMCAwEAAQ==
} }
} }
func mustMarshal(in interface{}, require *require.Assertions) []byte { func mustMarshal(in any, require *require.Assertions) []byte {
out, err := json.Marshal(in) out, err := json.Marshal(in)
require.NoError(err) require.NoError(err)
return out return out

View File

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

View File

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

View File

@ -393,7 +393,7 @@ func (a *API) assemblePeerStruct(vpnIP string, _ role.Role) (peer.Peer, error) {
} }
func (a *API) newLogToCLIFunc(send func(string) error) logFunc { func (a *API) newLogToCLIFunc(send func(string) error) logFunc {
return func(format string, v ...interface{}) { return func(format string, v ...any) {
if err := send(fmt.Sprintf(format, v...)); err != nil { if err := send(fmt.Sprintf(format, v...)); err != nil {
a.logger.Error("logging to CLI failed", zap.Error(err)) a.logger.Error("logging to CLI failed", zap.Error(err))
} }
@ -460,4 +460,4 @@ func (a *API) triggerNodeUpdate(nodePublicIP string) error {
return err return err
} }
type logFunc func(format string, v ...interface{}) type logFunc func(format string, v ...any)

View File

@ -140,7 +140,7 @@ func (m *fakeAWSClient) GetParametersForImport(ctx context.Context, params *kms.
if err != nil { if err != nil {
return nil, errors.New("Error generating Key Pair") return nil, errors.New("Error generating Key Pair")
} }
var pki interface{} = &m.keyPairStruct.PublicKey var pki any = &m.keyPairStruct.PublicKey
publicKeyBytes, err := x509.MarshalPKIXPublicKey(pki) publicKeyBytes, err := x509.MarshalPKIXPublicKey(pki)
if err != nil { if err != nil {
return nil, err return nil, err