Skip to content

Commit 07ed165

Browse files
author
Adam Hughes
committed
feat: extend unexpectedDataTypeError to describe multiple want types
1 parent 4073c70 commit 07ed165

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

pkg/sif/descriptor.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (d *rawDescriptor) setExtra(v interface{}) error {
9898
// getPartitionMetadata gets metadata for a partition data object.
9999
func (d rawDescriptor) getPartitionMetadata() (fs FSType, pt PartType, arch string, err error) {
100100
if got, want := d.DataType, DataPartition; got != want {
101-
return 0, 0, "", &unexpectedDataTypeError{got, want}
101+
return 0, 0, "", &unexpectedDataTypeError{got, []DataType{want}}
102102
}
103103

104104
var p partition
@@ -188,7 +188,7 @@ func getHashType(ht hashType) (crypto.Hash, error) {
188188
// SignatureMetadata gets metadata for a signature data object.
189189
func (d Descriptor) SignatureMetadata() (ht crypto.Hash, fp []byte, err error) {
190190
if got, want := d.raw.DataType, DataSignature; got != want {
191-
return ht, fp, &unexpectedDataTypeError{got, want}
191+
return ht, fp, &unexpectedDataTypeError{got, []DataType{want}}
192192
}
193193

194194
var s signature
@@ -211,7 +211,7 @@ func (d Descriptor) SignatureMetadata() (ht crypto.Hash, fp []byte, err error) {
211211
// CryptoMessageMetadata gets metadata for a crypto message data object.
212212
func (d Descriptor) CryptoMessageMetadata() (FormatType, MessageType, error) {
213213
if got, want := d.raw.DataType, DataCryptoMessage; got != want {
214-
return 0, 0, &unexpectedDataTypeError{got, want}
214+
return 0, 0, &unexpectedDataTypeError{got, []DataType{want}}
215215
}
216216

217217
var m cryptoMessage

pkg/sif/descriptor_input.go

+36-7
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,49 @@ func OptObjectTime(t time.Time) DescriptorInputOpt {
9595

9696
type unexpectedDataTypeError struct {
9797
got DataType
98-
want DataType
98+
want []DataType
9999
}
100100

101101
func (e *unexpectedDataTypeError) Error() string {
102-
return fmt.Sprintf("unexpected data type %v, expected %v", e.got, e.want)
102+
return fmt.Sprintf("unexpected data type %v, expected one of: %v", e.got, e.want)
103103
}
104104

105105
func (e *unexpectedDataTypeError) Is(target error) bool {
106106
t, ok := target.(*unexpectedDataTypeError)
107107
if !ok {
108108
return false
109109
}
110-
return (e.got == t.got || t.got == 0) &&
111-
(e.want == t.want || t.want == 0)
110+
111+
if len(t.want) > 0 {
112+
// Use a map to check that the "want" errors in e and t contain the same values, ignoring
113+
// any ordering differences.
114+
acc := make(map[DataType]int, len(t.want))
115+
116+
// Increment counter for each data type in e.
117+
for _, dt := range e.want {
118+
if _, ok := acc[dt]; !ok {
119+
acc[dt] = 0
120+
}
121+
acc[dt]++
122+
}
123+
124+
// Decrement counter for each data type in e.
125+
for _, dt := range t.want {
126+
if _, ok := acc[dt]; !ok {
127+
return false
128+
}
129+
acc[dt]--
130+
}
131+
132+
// If the "want" errors in e and t are equivalent, all counters should be zero.
133+
for _, n := range acc {
134+
if n != 0 {
135+
return false
136+
}
137+
}
138+
}
139+
140+
return (e.got == t.got || t.got == 0)
112141
}
113142

114143
// OptCryptoMessageMetadata sets metadata for a crypto message data object. The format type is set
@@ -118,7 +147,7 @@ func (e *unexpectedDataTypeError) Is(target error) bool {
118147
func OptCryptoMessageMetadata(ft FormatType, mt MessageType) DescriptorInputOpt {
119148
return func(t DataType, opts *descriptorOpts) error {
120149
if got, want := t, DataCryptoMessage; got != want {
121-
return &unexpectedDataTypeError{got, want}
150+
return &unexpectedDataTypeError{got, []DataType{want}}
122151
}
123152

124153
m := cryptoMessage{
@@ -141,7 +170,7 @@ var errUnknownArchitcture = errors.New("unknown architecture")
141170
func OptPartitionMetadata(fs FSType, pt PartType, arch string) DescriptorInputOpt {
142171
return func(t DataType, opts *descriptorOpts) error {
143172
if got, want := t, DataPartition; got != want {
144-
return &unexpectedDataTypeError{got, want}
173+
return &unexpectedDataTypeError{got, []DataType{want}}
145174
}
146175

147176
sifarch := getSIFArch(arch)
@@ -184,7 +213,7 @@ func sifHashType(h crypto.Hash) hashType {
184213
func OptSignatureMetadata(ht crypto.Hash, fp []byte) DescriptorInputOpt {
185214
return func(t DataType, opts *descriptorOpts) error {
186215
if got, want := t, DataSignature; got != want {
187-
return &unexpectedDataTypeError{got, want}
216+
return &unexpectedDataTypeError{got, []DataType{want}}
188217
}
189218

190219
s := signature{

pkg/sif/descriptor_input_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func TestNewDescriptorInput(t *testing.T) {
131131
opts: []DescriptorInputOpt{
132132
OptCryptoMessageMetadata(FormatOpenPGP, MessageClearSignature),
133133
},
134-
wantErr: &unexpectedDataTypeError{DataGeneric, DataCryptoMessage},
134+
wantErr: &unexpectedDataTypeError{DataGeneric, []DataType{DataCryptoMessage}},
135135
},
136136
{
137137
name: "OptCryptoMessageMetadata",
@@ -147,7 +147,7 @@ func TestNewDescriptorInput(t *testing.T) {
147147
opts: []DescriptorInputOpt{
148148
OptPartitionMetadata(FsSquash, PartPrimSys, "386"),
149149
},
150-
wantErr: &unexpectedDataTypeError{DataGeneric, DataPartition},
150+
wantErr: &unexpectedDataTypeError{DataGeneric, []DataType{DataPartition}},
151151
},
152152
{
153153
name: "OptPartitionMetadata",
@@ -163,7 +163,7 @@ func TestNewDescriptorInput(t *testing.T) {
163163
opts: []DescriptorInputOpt{
164164
OptSignatureMetadata(crypto.SHA256, fp),
165165
},
166-
wantErr: &unexpectedDataTypeError{DataGeneric, DataSignature},
166+
wantErr: &unexpectedDataTypeError{DataGeneric, []DataType{DataSignature}},
167167
},
168168
{
169169
name: "OptSignatureMetadata",

pkg/sif/descriptor_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func TestDescriptor_PartitionMetadata(t *testing.T) {
133133
rd: rawDescriptor{
134134
DataType: DataGeneric,
135135
},
136-
wantErr: &unexpectedDataTypeError{DataGeneric, DataPartition},
136+
wantErr: &unexpectedDataTypeError{DataGeneric, []DataType{DataPartition}},
137137
},
138138
{
139139
name: "PartPrimSys",
@@ -198,7 +198,7 @@ func TestDescriptor_SignatureMetadata(t *testing.T) {
198198
rd: rawDescriptor{
199199
DataType: DataGeneric,
200200
},
201-
wantErr: &unexpectedDataTypeError{DataGeneric, DataSignature},
201+
wantErr: &unexpectedDataTypeError{DataGeneric, []DataType{DataSignature}},
202202
},
203203
{
204204
name: "OK",
@@ -258,7 +258,7 @@ func TestDescriptor_CryptoMessageMetadata(t *testing.T) {
258258
rd: rawDescriptor{
259259
DataType: DataGeneric,
260260
},
261-
wantErr: &unexpectedDataTypeError{DataGeneric, DataCryptoMessage},
261+
wantErr: &unexpectedDataTypeError{DataGeneric, []DataType{DataCryptoMessage}},
262262
},
263263
{
264264
name: "OK",

0 commit comments

Comments
 (0)