Skip to content

Commit

Permalink
Rewrite interface{} to any (#162)
Browse files Browse the repository at this point in the history
Signed-off-by: qmuntal <qmuntaldiaz@microsoft.com>
  • Loading branch information
qmuntal authored Aug 8, 2023
1 parent 160b7e0 commit ed78bf9
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 127 deletions.
2 changes: 1 addition & 1 deletion fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func FuzzSign1Message_UnmarshalCBOR(f *testing.F) {
enc, _ := cbor.CanonicalEncOptions().EncMode()
dec, _ := cbor.DecOptions{IntDec: cbor.IntDecConvertSigned}.DecMode()
isCanonical := func(b []byte) bool {
var tmp interface{}
var tmp any
err := dec.Unmarshal(b, &tmp)
if err != nil {
return false
Expand Down
38 changes: 19 additions & 19 deletions headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (

// ProtectedHeader contains parameters that are to be cryptographically
// protected.
type ProtectedHeader map[interface{}]interface{}
type ProtectedHeader map[any]any

// MarshalCBOR encodes the protected header into a CBOR bstr object.
// A zero-length header is encoded as a zero-length string rather than as a
Expand All @@ -42,7 +42,7 @@ func (h ProtectedHeader) MarshalCBOR() ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("protected header: %w", err)
}
encoded, err = encMode.Marshal(map[interface{}]interface{}(h))
encoded, err = encMode.Marshal(map[any]any(h))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func (h *ProtectedHeader) UnmarshalCBOR(data []byte) error {
if err := validateHeaderLabelCBOR(encoded); err != nil {
return err
}
var header map[interface{}]interface{}
var header map[any]any
if err := decMode.Unmarshal(encoded, &header); err != nil {
return err
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func (h ProtectedHeader) Algorithm() (Algorithm, error) {
// processing a message is required to understand.
//
// Reference: https://datatracker.ietf.org/doc/html/rfc8152#section-3.1
func (h ProtectedHeader) Critical() ([]interface{}, error) {
func (h ProtectedHeader) Critical() ([]any, error) {
value, ok := h[HeaderLabelCritical]
if !ok {
return nil, nil
Expand All @@ -138,12 +138,12 @@ func (h ProtectedHeader) Critical() ([]interface{}, error) {
if err != nil {
return nil, err
}
return value.([]interface{}), nil
return value.([]any), nil
}

// ensureCritical ensures all critical headers are present in the protected bucket.
func ensureCritical(value interface{}, headers map[interface{}]interface{}) error {
labels, ok := value.([]interface{})
func ensureCritical(value any, headers map[any]any) error {
labels, ok := value.([]any)
if !ok {
return errors.New("invalid crit header")
}
Expand All @@ -164,7 +164,7 @@ func ensureCritical(value interface{}, headers map[interface{}]interface{}) erro

// UnprotectedHeader contains parameters that are not cryptographically
// protected.
type UnprotectedHeader map[interface{}]interface{}
type UnprotectedHeader map[any]any

// MarshalCBOR encodes the unprotected header into a CBOR map object.
// A zero-length header is encoded as a zero-length map (encoded as h'a0').
Expand All @@ -175,7 +175,7 @@ func (h UnprotectedHeader) MarshalCBOR() ([]byte, error) {
if err := validateHeaderParameters(h, false); err != nil {
return nil, fmt.Errorf("unprotected header: %w", err)
}
return encMode.Marshal(map[interface{}]interface{}(h))
return encMode.Marshal(map[any]any(h))
}

// UnmarshalCBOR decodes a CBOR map object into UnprotectedHeader.
Expand All @@ -197,7 +197,7 @@ func (h *UnprotectedHeader) UnmarshalCBOR(data []byte) error {
if err := validateHeaderLabelCBOR(data); err != nil {
return err
}
var header map[interface{}]interface{}
var header map[any]any
if err := decMode.Unmarshal(data, &header); err != nil {
return err
}
Expand Down Expand Up @@ -376,14 +376,14 @@ func (h *Headers) ensureIV() error {
}

// hasLabel returns true if h contains label.
func hasLabel(h map[interface{}]interface{}, label interface{}) bool {
func hasLabel(h map[any]any, label any) bool {
_, ok := h[label]
return ok
}

// validateHeaderParameters validates all headers conform to the spec.
func validateHeaderParameters(h map[interface{}]interface{}, protected bool) error {
existing := make(map[interface{}]struct{}, len(h))
func validateHeaderParameters(h map[any]any, protected bool) error {
existing := make(map[any]struct{}, len(h))
for label, value := range h {
// Validate that all header labels are integers or strings.
// Reference: https://datatracker.ietf.org/doc/html/rfc8152#section-1.4
Expand Down Expand Up @@ -443,7 +443,7 @@ func validateHeaderParameters(h map[interface{}]interface{}, protected bool) err
}

// canUint reports whether v can be used as a CBOR uint type.
func canUint(v interface{}) bool {
func canUint(v any) bool {
switch v := v.(type) {
case uint, uint8, uint16, uint32, uint64:
return true
Expand All @@ -462,7 +462,7 @@ func canUint(v interface{}) bool {
}

// canInt reports whether v can be used as a CBOR int type.
func canInt(v interface{}) bool {
func canInt(v any) bool {
switch v.(type) {
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64:
Expand All @@ -472,20 +472,20 @@ func canInt(v interface{}) bool {
}

// canTstr reports whether v can be used as a CBOR tstr type.
func canTstr(v interface{}) bool {
func canTstr(v any) bool {
_, ok := v.(string)
return ok
}

// canBstr reports whether v can be used as a CBOR bstr type.
func canBstr(v interface{}) bool {
func canBstr(v any) bool {
_, ok := v.([]byte)
return ok
}

// normalizeLabel tries to cast label into a int64 or a string.
// Returns (nil, false) if the label type is not valid.
func normalizeLabel(label interface{}) (interface{}, bool) {
func normalizeLabel(label any) (any, bool) {
switch v := label.(type) {
case int:
label = int64(v)
Expand Down Expand Up @@ -517,7 +517,7 @@ func normalizeLabel(label interface{}) (interface{}, bool) {

// headerLabelValidator is used to validate the header label of a COSE header.
type headerLabelValidator struct {
value interface{}
value any
}

// String prints the value without brackets `{}`. Useful in error printing.
Expand Down
18 changes: 9 additions & 9 deletions headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestProtectedHeader_MarshalCBOR(t *testing.T) {
name: "valid header",
h: ProtectedHeader{
HeaderLabelAlgorithm: AlgorithmES256,
HeaderLabelCritical: []interface{}{
HeaderLabelCritical: []any{
HeaderLabelContentType,
"foo",
},
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestProtectedHeader_MarshalCBOR(t *testing.T) {
{
name: "empty critical",
h: ProtectedHeader{
HeaderLabelCritical: []interface{}{},
HeaderLabelCritical: []any{},
},
wantErr: "protected header: header parameter: crit: empty crit header",
},
Expand All @@ -98,7 +98,7 @@ func TestProtectedHeader_MarshalCBOR(t *testing.T) {
{
name: "missing header marked as critical",
h: ProtectedHeader{
HeaderLabelCritical: []interface{}{
HeaderLabelCritical: []any{
HeaderLabelContentType,
},
},
Expand All @@ -107,7 +107,7 @@ func TestProtectedHeader_MarshalCBOR(t *testing.T) {
{
name: "critical header contains non-label element",
h: ProtectedHeader{
HeaderLabelCritical: []interface{}{[]uint8{}},
HeaderLabelCritical: []any{[]uint8{}},
},
wantErr: "protected header: header parameter: crit: require int / tstr type, got '[]uint8': []",
},
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestProtectedHeader_UnmarshalCBOR(t *testing.T) {
},
want: ProtectedHeader{
HeaderLabelAlgorithm: AlgorithmES256,
HeaderLabelCritical: []interface{}{
HeaderLabelCritical: []any{
HeaderLabelContentType,
"foo",
},
Expand Down Expand Up @@ -451,21 +451,21 @@ func TestProtectedHeader_Critical(t *testing.T) {
tests := []struct {
name string
h ProtectedHeader
want []interface{}
want []any
wantErr string
}{
{
name: "valid header",
h: ProtectedHeader{
HeaderLabelAlgorithm: AlgorithmES256,
HeaderLabelCritical: []interface{}{
HeaderLabelCritical: []any{
HeaderLabelContentType,
"foo",
},
HeaderLabelContentType: "text/plain",
"foo": "bar",
},
want: []interface{}{
want: []any{
HeaderLabelContentType,
"foo",
},
Expand All @@ -490,7 +490,7 @@ func TestProtectedHeader_Critical(t *testing.T) {
{
name: "empty critical",
h: ProtectedHeader{
HeaderLabelCritical: []interface{}{},
HeaderLabelCritical: []any{},
},
wantErr: "empty crit header",
},
Expand Down
Loading

0 comments on commit ed78bf9

Please sign in to comment.