feat: Allow specific versions of formats to be specified (#1543)

This commit is contained in:
Keith Zantow
2023-02-07 10:40:43 -05:00
committed by GitHub
parent 95201840d2
commit 9650473298
26 changed files with 349 additions and 187 deletions
+30 -5
View File
@@ -2,6 +2,7 @@ package sbom
import (
"errors"
"fmt"
"io"
)
@@ -18,20 +19,41 @@ func (f FormatID) String() string {
return string(f)
}
const AnyVersion = ""
type Format interface {
ID() FormatID
IDs() []FormatID
Version() string
Encode(io.Writer, SBOM) error
Decode(io.Reader) (*SBOM, error)
Validate(io.Reader) error
fmt.Stringer
}
type format struct {
id FormatID
ids []FormatID
version string
encoder Encoder
decoder Decoder
validator Validator
}
func (f format) IDs() []FormatID {
return f.ids
}
func (f format) Version() string {
return f.version
}
func (f format) String() string {
if f.version == AnyVersion {
return f.ID().String()
}
return fmt.Sprintf("%s@%s", f.ID(), f.version)
}
// Decoder is a function that can convert an SBOM document of a specific format from a reader into Syft native objects.
type Decoder func(reader io.Reader) (*SBOM, error)
@@ -47,9 +69,10 @@ type Encoder func(io.Writer, SBOM) error
// really represent a different format that also uses json)
type Validator func(reader io.Reader) error
func NewFormat(id FormatID, encoder Encoder, decoder Decoder, validator Validator) Format {
return &format{
id: id,
func NewFormat(version string, encoder Encoder, decoder Decoder, validator Validator, ids ...FormatID) Format {
return format{
ids: ids,
version: version,
encoder: encoder,
decoder: decoder,
validator: validator,
@@ -57,7 +80,7 @@ func NewFormat(id FormatID, encoder Encoder, decoder Decoder, validator Validato
}
func (f format) ID() FormatID {
return f.id
return f.ids[0]
}
func (f format) Encode(output io.Writer, s SBOM) error {
@@ -81,3 +104,5 @@ func (f format) Validate(reader io.Reader) error {
return f.validator(reader)
}
var _ Format = (*format)(nil)
+1 -1
View File
@@ -15,7 +15,7 @@ func dummyEncoder(io.Writer, SBOM) error {
}
func dummyFormat(name string) Format {
return NewFormat(FormatID(name), dummyEncoder, nil, nil)
return NewFormat(AnyVersion, dummyEncoder, nil, nil, FormatID(name))
}
type writerConfig struct {