Skip to content

Commit 6e2497e

Browse files
committed
refactor: use interface types for custom metadata
Switch to encoding.BinaryUnmarshaler/encoding.BinaryMarshaler for GetMetadata/OptMetadata rather than "any" type.
1 parent 59a5446 commit 6e2497e

File tree

5 files changed

+58
-50
lines changed

5 files changed

+58
-50
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ linters:
4646
- typecheck
4747
- unconvert
4848
- unparam
49-
- unused
49+
# - unused
5050
- whitespace
5151

5252
linters-settings:

pkg/sif/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018-2022, Sylabs Inc. All rights reserved.
1+
// Copyright (c) 2018-2023, Sylabs Inc. All rights reserved.
22
// Copyright (c) 2017, SingularityWare, LLC. All rights reserved.
33
// Copyright (c) 2017, Yannick Cote <yhcote@gmail.com> All rights reserved.
44
// This software is licensed under a 3-clause BSD license. Please consult the
@@ -69,7 +69,7 @@ func (f *FileImage) writeDataObject(i int, di DescriptorInput, t time.Time) erro
6969

7070
// If this is a primary partition, verify there isn't another primary partition, and update the
7171
// architecture in the global header.
72-
if p, ok := di.opts.extra.(partition); ok && p.Parttype == PartPrimSys {
72+
if p, ok := di.opts.md.(partition); ok && p.Parttype == PartPrimSys {
7373
if ds, err := f.GetDescriptors(WithPartitionType(PartPrimSys)); err == nil && len(ds) > 0 {
7474
return errPrimaryPartition
7575
}

pkg/sif/descriptor.go

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ type partition struct {
4545
Arch archType
4646
}
4747

48+
// MarshalBinary encodes p into binary format.
49+
func (p partition) MarshalBinary() ([]byte, error) {
50+
return binaryMarshaler{p}.MarshalBinary()
51+
}
52+
4853
// signature represents the SIF signature data object descriptor.
4954
type signature struct {
5055
Hashtype hashType
@@ -62,6 +67,26 @@ type sbom struct {
6267
Format SBOMFormat
6368
}
6469

70+
// The binaryMarshaler type is an adapter that allows a type suitable for use with the
71+
// encoding/binary package to be used as an encoding.BinaryMarshaler.
72+
type binaryMarshaler struct{ any }
73+
74+
// MarshalBinary encodes m into binary format.
75+
func (m binaryMarshaler) MarshalBinary() ([]byte, error) {
76+
var b bytes.Buffer
77+
err := binary.Write(&b, binary.LittleEndian, m.any)
78+
return b.Bytes(), err
79+
}
80+
81+
// The binaryUnmarshaler type is an adapter that allows a type suitable for use with the
82+
// encoding/binary package to be used as an encoding.BinaryUnmarshaler.
83+
type binaryUnmarshaler struct{ any }
84+
85+
// UnmarshalBinary decodes b into u.
86+
func (u binaryUnmarshaler) UnmarshalBinary(b []byte) error {
87+
return binary.Read(bytes.NewReader(b), binary.LittleEndian, u.any)
88+
}
89+
6590
var errNameTooLarge = errors.New("name value too large")
6691

6792
// setName encodes name into the name field of d.
@@ -79,27 +104,15 @@ func (d *rawDescriptor) setName(name string) error {
79104

80105
var errExtraTooLarge = errors.New("extra value too large")
81106

82-
// setExtra encodes v into the extra field of d. If the encoding.BinaryMarshaler interface is
83-
// implemented by v, it is used for marshaling. Otherwise, binary.Write() is used.
84-
func (d *rawDescriptor) setExtra(v any) error {
85-
if v == nil {
107+
// setExtra marshals metadata from md into the "extra" field of d.
108+
func (d *rawDescriptor) setExtra(md encoding.BinaryMarshaler) error {
109+
if md == nil {
86110
return nil
87111
}
88112

89-
var extra []byte
90-
91-
if m, ok := v.(encoding.BinaryMarshaler); ok {
92-
b, err := m.MarshalBinary()
93-
if err != nil {
94-
return err
95-
}
96-
extra = b
97-
} else {
98-
b := new(bytes.Buffer)
99-
if err := binary.Write(b, binary.LittleEndian, v); err != nil {
100-
return err
101-
}
102-
extra = b.Bytes()
113+
extra, err := md.MarshalBinary()
114+
if err != nil {
115+
return err
103116
}
104117

105118
if len(extra) > len(d.Extra) {
@@ -113,13 +126,9 @@ func (d *rawDescriptor) setExtra(v any) error {
113126
return nil
114127
}
115128

116-
// getExtra decodes the extra fields of d into v. If the encoding.BinaryUnmarshaler interface is
117-
// implemented by v, it is used for unmarshaling. Otherwise, binary.Read() is used.
118-
func (d *rawDescriptor) getExtra(v any) error {
119-
if u, ok := v.(encoding.BinaryUnmarshaler); ok {
120-
return u.UnmarshalBinary(d.Extra[:])
121-
}
122-
return binary.Read(bytes.NewReader(d.Extra[:]), binary.LittleEndian, v)
129+
// getExtra unmarshals metadata from the "extra" field of d into md.
130+
func (d *rawDescriptor) getExtra(md encoding.BinaryUnmarshaler) error {
131+
return md.UnmarshalBinary(d.Extra[:])
123132
}
124133

125134
// getPartitionMetadata gets metadata for a partition data object.
@@ -130,7 +139,7 @@ func (d rawDescriptor) getPartitionMetadata() (FSType, PartType, string, error)
130139

131140
var p partition
132141

133-
if err := d.getExtra(&p); err != nil {
142+
if err := d.getExtra(binaryUnmarshaler{&p}); err != nil {
134143
return 0, 0, "", err
135144
}
136145

@@ -189,10 +198,9 @@ func (d Descriptor) ModifiedAt() time.Time { return time.Unix(d.raw.ModifiedAt,
189198
// Name returns the name of the data object.
190199
func (d Descriptor) Name() string { return strings.TrimRight(string(d.raw.Name[:]), "\000") }
191200

192-
// GetMetadata reads metadata from d into v. If the encoding.BinaryUnmarshaler interface is
193-
// implemented by v, it is used for unmarshaling. Otherwise, binary.Read() is used.
194-
func (d Descriptor) GetMetadata(v any) error {
195-
if err := d.raw.getExtra(v); err != nil {
201+
// GetMetadata unmarshals metadata from the "extra" field of d into md.
202+
func (d Descriptor) GetMetadata(md encoding.BinaryUnmarshaler) error {
203+
if err := d.raw.getExtra(md); err != nil {
196204
return fmt.Errorf("%w", err)
197205
}
198206
return nil
@@ -238,7 +246,7 @@ func (d Descriptor) SignatureMetadata() (ht crypto.Hash, fp []byte, err error) {
238246

239247
var s signature
240248

241-
if err := d.raw.getExtra(&s); err != nil {
249+
if err := d.raw.getExtra(binaryUnmarshaler{&s}); err != nil {
242250
return ht, fp, fmt.Errorf("%w", err)
243251
}
244252

@@ -265,7 +273,7 @@ func (d Descriptor) CryptoMessageMetadata() (FormatType, MessageType, error) {
265273

266274
var m cryptoMessage
267275

268-
if err := d.raw.getExtra(&m); err != nil {
276+
if err := d.raw.getExtra(binaryUnmarshaler{&m}); err != nil {
269277
return 0, 0, fmt.Errorf("%w", err)
270278
}
271279

@@ -280,7 +288,7 @@ func (d Descriptor) SBOMMetadata() (SBOMFormat, error) {
280288

281289
var s sbom
282290

283-
if err := d.raw.getExtra(&s); err != nil {
291+
if err := d.raw.getExtra(binaryUnmarshaler{&s}); err != nil {
284292
return 0, fmt.Errorf("%w", err)
285293
}
286294

pkg/sif/descriptor_input.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package sif
77

88
import (
99
"crypto"
10+
"encoding"
1011
"errors"
1112
"fmt"
1213
"io"
@@ -19,7 +20,7 @@ type descriptorOpts struct {
1920
linkID uint32
2021
alignment int
2122
name string
22-
extra any
23+
md encoding.BinaryMarshaler
2324
t time.Time
2425
}
2526

@@ -92,11 +93,10 @@ func OptObjectTime(t time.Time) DescriptorInputOpt {
9293
}
9394
}
9495

95-
// OptMetadata sets v as the metadata for a data object. If the encoding.BinaryMarshaler interface
96-
// is implemented by v, it is used for marshaling. Otherwise, binary.Write() is used.
97-
func OptMetadata(v any) DescriptorInputOpt {
96+
// OptMetadata marshals metadata from md into the "extra" field of d.
97+
func OptMetadata(md encoding.BinaryMarshaler) DescriptorInputOpt {
9898
return func(t DataType, opts *descriptorOpts) error {
99-
opts.extra = v
99+
opts.md = md
100100
return nil
101101
}
102102
}
@@ -164,7 +164,7 @@ func OptCryptoMessageMetadata(ft FormatType, mt MessageType) DescriptorInputOpt
164164
Messagetype: mt,
165165
}
166166

167-
opts.extra = m
167+
opts.md = binaryMarshaler{m}
168168
return nil
169169
}
170170
}
@@ -193,7 +193,7 @@ func OptPartitionMetadata(fs FSType, pt PartType, arch string) DescriptorInputOp
193193
Arch: sifarch,
194194
}
195195

196-
opts.extra = p
196+
opts.md = p
197197
return nil
198198
}
199199
}
@@ -230,7 +230,7 @@ func OptSignatureMetadata(ht crypto.Hash, fp []byte) DescriptorInputOpt {
230230
}
231231
copy(s.Entity[:], fp)
232232

233-
opts.extra = s
233+
opts.md = binaryMarshaler{s}
234234
return nil
235235
}
236236
}
@@ -248,7 +248,7 @@ func OptSBOMMetadata(f SBOMFormat) DescriptorInputOpt {
248248
Format: f,
249249
}
250250

251-
opts.extra = s
251+
opts.md = binaryMarshaler{s}
252252
return nil
253253
}
254254
}
@@ -327,5 +327,5 @@ func (di DescriptorInput) fillDescriptor(t time.Time, d *rawDescriptor) error {
327327
return err
328328
}
329329

330-
return d.setExtra(di.opts.extra)
330+
return d.setExtra(di.opts.md)
331331
}

pkg/sif/descriptor_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func TestDescriptor_GetMetadata(t *testing.T) {
129129
tests := []struct {
130130
name string
131131
rd rawDescriptor
132-
wantMD any
132+
wantMD testMetadata
133133
wantErr error
134134
}{
135135
{
@@ -261,7 +261,7 @@ func TestDescriptor_SignatureMetadata(t *testing.T) {
261261
rd := rawDescriptor{
262262
DataType: tt.dt,
263263
}
264-
if err := rd.setExtra(sig); err != nil {
264+
if err := rd.setExtra(binaryMarshaler{sig}); err != nil {
265265
t.Fatal(err)
266266
}
267267

@@ -295,7 +295,7 @@ func TestDescriptor_CryptoMessageMetadata(t *testing.T) {
295295
rd := rawDescriptor{
296296
DataType: DataCryptoMessage,
297297
}
298-
if err := rd.setExtra(m); err != nil {
298+
if err := rd.setExtra(binaryMarshaler{m}); err != nil {
299299
t.Fatal(err)
300300
}
301301

@@ -351,7 +351,7 @@ func TestDescriptor_SBOMMetadata(t *testing.T) {
351351
rd := rawDescriptor{
352352
DataType: DataSBOM,
353353
}
354-
if err := rd.setExtra(m); err != nil {
354+
if err := rd.setExtra(binaryMarshaler{m}); err != nil {
355355
t.Fatal(err)
356356
}
357357

0 commit comments

Comments
 (0)