Skip to content

Commit

Permalink
refactor: reduce indentation to 2 spaces; use builtin xml header
Browse files Browse the repository at this point in the history
  • Loading branch information
nscuro committed Mar 5, 2021
1 parent b227b5f commit 99ea89f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
6 changes: 3 additions & 3 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type jsonBOMEncoder struct {
func (j jsonBOMEncoder) Encode(bom *BOM) error {
encoder := json.NewEncoder(j.writer)
if j.pretty {
encoder.SetIndent("", " ")
encoder.SetIndent("", " ")
}
return encoder.Encode(bom)
}
Expand All @@ -42,13 +42,13 @@ type xmlBOMEncoder struct {
}

func (x xmlBOMEncoder) Encode(bom *BOM) error {
if _, err := fmt.Fprintf(x.writer, "<?xml version=\"1.0\"?>\n"); err != nil {
if _, err := fmt.Fprintf(x.writer, xml.Header); err != nil {
return err
}

encoder := xml.NewEncoder(x.writer)
if x.pretty {
encoder.Indent("", " ")
encoder.Indent("", " ")
}
return encoder.Encode(bom)
}
Expand Down
36 changes: 18 additions & 18 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ func TestJsonBOMEncoder_SetPretty(t *testing.T) {
require.NoError(t, encoder.Encode(bom))

assert.Equal(t, `{
"bomFormat": "CycloneDX",
"specVersion": "1.2",
"version": 1,
"metadata": {
"authors": [
{
"name": "authorName"
}
]
}
"bomFormat": "CycloneDX",
"specVersion": "1.2",
"version": 1,
"metadata": {
"authors": [
{
"name": "authorName"
}
]
}
}
`, buf.String())
}
Expand All @@ -74,15 +74,15 @@ func TestXmlBOMEncoder_SetPretty(t *testing.T) {

require.NoError(t, encoder.Encode(bom))

assert.Equal(t, `<?xml version="1.0"?>
assert.Equal(t, `<?xml version="1.0" encoding="UTF-8"?>
<bom xmlns="http://cyclonedx.org/schema/bom/1.2" version="1">
<metadata>
<authors>
<author>
<name>authorName</name>
</author>
</authors>
</metadata>
<metadata>
<authors>
<author>
<name>authorName</name>
</author>
</authors>
</metadata>
</bom>`, buf.String())
}

Expand Down

0 comments on commit 99ea89f

Please sign in to comment.