Skip to content

Commit acb9322

Browse files
committedSep 26, 2022
feat: add enum for official media types
Signed-off-by: nscuro <nscuro@protonmail.com>
1 parent 1655b7d commit acb9322

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed
 

‎cyclonedx.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ package cyclonedx
1919

2020
import (
2121
"encoding/xml"
22+
"fmt"
2223
"regexp"
2324
)
2425

25-
//go:generate stringer -linecomment -output cyclonedx_string.go -type SpecVersion
26+
//go:generate stringer -linecomment -output cyclonedx_string.go -type MediaType,SpecVersion
2627

2728
const (
2829
BOMFormat = "CycloneDX"
@@ -319,6 +320,24 @@ type LicenseChoice struct {
319320
Expression string `json:"expression,omitempty" xml:"-"`
320321
}
321322

323+
// MediaType defines the official media types for CycloneDX BOMs.
324+
// See https://cyclonedx.org/specification/overview/#registered-media-types
325+
type MediaType int
326+
327+
const (
328+
MediaTypeJSON MediaType = iota + 1 // application/vnd.cyclonedx+json
329+
MediaTypeXML // application/vnd.cyclonedx+xml
330+
MediaTypeProtobuf // application/x.vnd.cyclonedx+protobuf
331+
)
332+
333+
func (mt MediaType) WithVersion(specVersion SpecVersion) (string, error) {
334+
if mt == MediaTypeJSON && specVersion < SpecVersion1_2 {
335+
return "", fmt.Errorf("json format is not supported for specification versions lower than %s", SpecVersion1_2)
336+
}
337+
338+
return fmt.Sprintf("%s; version=%s", mt, specVersion), nil
339+
}
340+
322341
type Metadata struct {
323342
Timestamp string `json:"timestamp,omitempty" xml:"timestamp,omitempty"`
324343
Tools *[]Tool `json:"tools,omitempty" xml:"tools>tool,omitempty"`

‎cyclonedx_string.go

+21-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎cyclonedx_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/bradleyjkemp/cupaloy/v2"
2727
"github.com/stretchr/testify/assert"
28+
"github.com/stretchr/testify/require"
2829
)
2930

3031
var snapShooter = cupaloy.NewDefaultConfig().
@@ -35,6 +36,19 @@ func TestBool(t *testing.T) {
3536
assert.Equal(t, false, *Bool(false))
3637
}
3738

39+
func TestMediaType_WithVersion(t *testing.T) {
40+
t.Run("ShouldReturnVersionedMediaType", func(t *testing.T) {
41+
res, err := MediaTypeJSON.WithVersion(SpecVersion1_2)
42+
require.NoError(t, err)
43+
require.Equal(t, "application/vnd.cyclonedx+json; version=1.2", res)
44+
})
45+
46+
t.Run("ShouldReturnErrorForSpecLowerThan1.2AndJSON", func(t *testing.T) {
47+
_, err := MediaTypeJSON.WithVersion(SpecVersion1_1)
48+
require.Error(t, err)
49+
})
50+
}
51+
3852
func TestVulnerability_Properties(t *testing.T) {
3953
// GIVEN
4054
properties := []Property{}

‎encode.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type jsonBOMEncoder struct {
5252
// Encode implements the BOMEncoder interface.
5353
func (j jsonBOMEncoder) Encode(bom *BOM) error {
5454
if bom.SpecVersion < SpecVersion1_2 {
55-
return fmt.Errorf("json format is not supported for specification versions lower than 1.2")
55+
return fmt.Errorf("json format is not supported for specification versions lower than %s", SpecVersion1_2)
5656
}
5757

5858
encoder := json.NewEncoder(j.writer)

0 commit comments

Comments
 (0)
Please sign in to comment.