Skip to content

Commit

Permalink
feat: add enum for official media types
Browse files Browse the repository at this point in the history
Signed-off-by: nscuro <nscuro@protonmail.com>
  • Loading branch information
nscuro committed Sep 26, 2022
1 parent 1655b7d commit acb9322
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
21 changes: 20 additions & 1 deletion cyclonedx.go
Expand Up @@ -19,10 +19,11 @@ package cyclonedx

import (
"encoding/xml"
"fmt"
"regexp"
)

//go:generate stringer -linecomment -output cyclonedx_string.go -type SpecVersion
//go:generate stringer -linecomment -output cyclonedx_string.go -type MediaType,SpecVersion

const (
BOMFormat = "CycloneDX"
Expand Down Expand Up @@ -319,6 +320,24 @@ type LicenseChoice struct {
Expression string `json:"expression,omitempty" xml:"-"`
}

// MediaType defines the official media types for CycloneDX BOMs.
// See https://cyclonedx.org/specification/overview/#registered-media-types
type MediaType int

const (
MediaTypeJSON MediaType = iota + 1 // application/vnd.cyclonedx+json
MediaTypeXML // application/vnd.cyclonedx+xml
MediaTypeProtobuf // application/x.vnd.cyclonedx+protobuf
)

func (mt MediaType) WithVersion(specVersion SpecVersion) (string, error) {
if mt == MediaTypeJSON && specVersion < SpecVersion1_2 {
return "", fmt.Errorf("json format is not supported for specification versions lower than %s", SpecVersion1_2)
}

return fmt.Sprintf("%s; version=%s", mt, specVersion), nil
}

type Metadata struct {
Timestamp string `json:"timestamp,omitempty" xml:"timestamp,omitempty"`
Tools *[]Tool `json:"tools,omitempty" xml:"tools>tool,omitempty"`
Expand Down
22 changes: 21 additions & 1 deletion cyclonedx_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions cyclonedx_test.go
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/bradleyjkemp/cupaloy/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

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

func TestMediaType_WithVersion(t *testing.T) {
t.Run("ShouldReturnVersionedMediaType", func(t *testing.T) {
res, err := MediaTypeJSON.WithVersion(SpecVersion1_2)
require.NoError(t, err)
require.Equal(t, "application/vnd.cyclonedx+json; version=1.2", res)
})

t.Run("ShouldReturnErrorForSpecLowerThan1.2AndJSON", func(t *testing.T) {
_, err := MediaTypeJSON.WithVersion(SpecVersion1_1)
require.Error(t, err)
})
}

func TestVulnerability_Properties(t *testing.T) {
// GIVEN
properties := []Property{}
Expand Down
2 changes: 1 addition & 1 deletion encode.go
Expand Up @@ -52,7 +52,7 @@ type jsonBOMEncoder struct {
// Encode implements the BOMEncoder interface.
func (j jsonBOMEncoder) Encode(bom *BOM) error {
if bom.SpecVersion < SpecVersion1_2 {
return fmt.Errorf("json format is not supported for specification versions lower than 1.2")
return fmt.Errorf("json format is not supported for specification versions lower than %s", SpecVersion1_2)
}

encoder := json.NewEncoder(j.writer)
Expand Down

0 comments on commit acb9322

Please sign in to comment.