Skip to content

Commit

Permalink
feat(spec1-5): add support for formulation
Browse files Browse the repository at this point in the history
Signed-off-by: nscuro <nscuro@protonmail.com>
  • Loading branch information
nscuro committed Jul 30, 2023
1 parent e4d6a8f commit 8c9798c
Show file tree
Hide file tree
Showing 10 changed files with 1,615 additions and 41 deletions.
5 changes: 5 additions & 0 deletions convert.go
Expand Up @@ -50,6 +50,7 @@ func (b *BOM) convert(specVersion SpecVersion) {
}
if specVersion < SpecVersion1_5 {
b.Annotations = nil
b.Formulation = nil
}

if b.Metadata != nil {
Expand Down Expand Up @@ -389,9 +390,13 @@ func (sv SpecVersion) supportsExternalReferenceType(ert ExternalReferenceType) b
ERTypeCertificationReport,
ERTypeCodifiedInfrastructure,
ERTypeComponentAnalysisReport,
ERTypeConfiguration,
ERTypeDistributionIntake,
ERTypeDynamicAnalysisReport,
ERTypeEvidence,
ERTypeExploitabilityStatement,
ERTypeFormulation,
ERTypeLog,
ERTypeMaturityReport,
ERTypeModelCard,
ERTypePentestReport,
Expand Down
214 changes: 214 additions & 0 deletions cyclonedx.go

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions cyclonedx_json.go
Expand Up @@ -19,8 +19,47 @@ package cyclonedx

import (
"encoding/json"
"errors"
)

func (ev EnvironmentVariableChoice) MarshalJSON() ([]byte, error) {
if ev.Property != nil && *ev.Property != (Property{}) {
return json.Marshal(ev.Property)
} else if ev.Value != "" {
return json.Marshal(ev.Value)
}

return []byte("{}"), nil
}

func (ev *EnvironmentVariableChoice) UnmarshalJSON(bytes []byte) error {
var property Property
err := json.Unmarshal(bytes, &property)
if err != nil {
var ute *json.UnmarshalTypeError
if !errors.As(err, &ute) || ute.Value != "string" {
return err
}
}

if property != (Property{}) {
ev.Property = &property
return nil
}

var value string
err = json.Unmarshal(bytes, &value)
if err != nil {
var ute *json.UnmarshalTypeError
if !errors.As(err, &ute) || ute.Value != "object" {
return err
}
}

ev.Value = value
return nil
}

type mlDatasetChoiceRefJSON struct {
Ref string `json:"ref" xml:"-"`
}
Expand Down
68 changes: 68 additions & 0 deletions cyclonedx_json_test.go
Expand Up @@ -23,6 +23,74 @@ import (
"testing"
)

func TestEnvironmentVariableChoice_MarshalJSON(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
choice := EnvironmentVariableChoice{}
jsonBytes, err := json.Marshal(choice)
require.NoError(t, err)
require.Equal(t, "{}", string(jsonBytes))
})

t.Run("WithProperty", func(t *testing.T) {
choice := EnvironmentVariableChoice{
Property: &Property{
Name: "foo",
Value: "bar",
},
}
jsonBytes, err := json.Marshal(choice)
require.NoError(t, err)
require.Equal(t, `{"name":"foo","value":"bar"}`, string(jsonBytes))
})

t.Run("WithValue", func(t *testing.T) {
choice := EnvironmentVariableChoice{Value: "foo"}
jsonBytes, err := json.Marshal(choice)
require.NoError(t, err)
require.Equal(t, `"foo"`, string(jsonBytes))
})

t.Run("WithPropertyAndValue", func(t *testing.T) {
choice := EnvironmentVariableChoice{
Property: &Property{
Name: "foo",
Value: "bar",
},
Value: "baz",
}
jsonBytes, err := json.Marshal(choice)
require.NoError(t, err)
require.Equal(t, `{"name":"foo","value":"bar"}`, string(jsonBytes))
})
}

func TestEnvironmentVariableChoice_UnmarshalJSON(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
var choice EnvironmentVariableChoice
err := json.Unmarshal([]byte(`{}`), &choice)
require.NoError(t, err)
require.Equal(t, EnvironmentVariableChoice{}, choice)
})

t.Run("WithProperty", func(t *testing.T) {
var choice EnvironmentVariableChoice
err := json.Unmarshal([]byte(`{"name":"foo","value":"bar"}`), &choice)
require.NoError(t, err)
require.NotNil(t, choice.Property)
require.Equal(t, "foo", choice.Property.Name)
require.Equal(t, "bar", choice.Property.Value)
require.Empty(t, choice.Value)
})

t.Run("WithValue", func(t *testing.T) {
var choice EnvironmentVariableChoice
err := json.Unmarshal([]byte(`"foo"`), &choice)
require.NoError(t, err)
require.Nil(t, choice.Property)
require.Equal(t, "foo", choice.Value)
})
}

func TestMLDatasetChoice_MarshalJSON(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
choice := MLDatasetChoice{}
Expand Down
69 changes: 69 additions & 0 deletions cyclonedx_xml.go
Expand Up @@ -97,6 +97,75 @@ func (d *Dependency) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) erro
return nil
}

func (ev EnvironmentVariables) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if len(ev) == 0 {
return nil
}

err := e.EncodeToken(start)
if err != nil {
return err
}

for _, choice := range ev {
if choice.Property != nil && choice.Value != "" {
return fmt.Errorf("either property or value must be set, but not both")
}

if choice.Property != nil {
err = e.EncodeElement(choice.Property, xml.StartElement{Name: xml.Name{Local: "environmentVar"}})
if err != nil {
return err
}
} else if choice.Value != "" {
err = e.EncodeElement(choice.Value, xml.StartElement{Name: xml.Name{Local: "value"}})
if err != nil {
return err
}
}
}

return e.EncodeToken(start.End())
}

func (ev *EnvironmentVariables) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
envVars := make([]EnvironmentVariableChoice, 0)

for {
token, err := d.Token()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return err
}

switch tokenType := token.(type) {
case xml.StartElement:
if tokenType.Name.Local == "value" {
var value string
err = d.DecodeElement(&value, &tokenType)
if err != nil {
return err
}
envVars = append(envVars, EnvironmentVariableChoice{Value: value})
} else if tokenType.Name.Local == "environmentVar" {
var property Property
err = d.DecodeElement(&property, &tokenType)
if err != nil {
return err
}
envVars = append(envVars, EnvironmentVariableChoice{Property: &property})
} else {
return fmt.Errorf("unknown element: %s", tokenType.Name.Local)
}
}
}

*ev = envVars
return nil
}

func (l Licenses) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if len(l) == 0 {
return nil
Expand Down

0 comments on commit 8c9798c

Please sign in to comment.