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
}
func writeFmt(sb *strings.Builder, fmtStr string, args ...interface{}) {
func writeFmt(sb *strings.Builder, fmtStr string, args ...any) {
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.
// 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)
if err != nil {
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.
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")
if err != nil {
return err

View File

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

View File

@ -142,7 +142,7 @@ func stubNewConnFunc(errStub error) func(ctx context.Context, target string, opt
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
}

View File

@ -148,7 +148,7 @@ type Validator interface {
Validate(attDoc []byte, nonce []byte) ([]byte, error)
}
func hashPublicKey(pub interface{}) ([]byte, error) {
func hashPublicKey(pub any) ([]byte, error) {
pubBytes, err := x509.MarshalPKIXPublicKey(pub)
if err != nil {
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)
require.NoError(err)
return out

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": {

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 {
return func(format string, v ...interface{}) {
return func(format string, v ...any) {
if err := send(fmt.Sprintf(format, v...)); err != nil {
a.logger.Error("logging to CLI failed", zap.Error(err))
}
@ -460,4 +460,4 @@ func (a *API) triggerNodeUpdate(nodePublicIP string) error {
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 {
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)
if err != nil {
return nil, err