Skip to content

Commit 8b3cc12

Browse files
committed
add SetIndent and tests
1 parent 72e4629 commit 8b3cc12

2 files changed

Lines changed: 97 additions & 4 deletions

File tree

encode.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,23 @@ type BOMEncoder interface {
3838

3939
// SetEscapeHTML toggles escaped HTML output.
4040
SetEscapeHTML(escapeHTML bool) BOMEncoder
41+
42+
// SetIndent sets the indent string for pretty printing.
43+
SetIndent(indent string) BOMEncoder
4144
}
4245

4346
func NewBOMEncoder(writer io.Writer, format BOMFileFormat) BOMEncoder {
4447
if format == BOMFileFormatJSON {
45-
return &jsonBOMEncoder{writer: writer, escapeHTML: true}
48+
return &jsonBOMEncoder{writer: writer, escapeHTML: true, indent: " "}
4649
}
47-
return &xmlBOMEncoder{writer: writer}
50+
return &xmlBOMEncoder{writer: writer, indent: " "}
4851
}
4952

5053
type jsonBOMEncoder struct {
5154
writer io.Writer
5255
pretty bool
5356
escapeHTML bool
57+
indent string
5458
}
5559

5660
// Encode implements the BOMEncoder interface.
@@ -62,7 +66,7 @@ func (j jsonBOMEncoder) Encode(bom *BOM) error {
6266
encoder := json.NewEncoder(j.writer)
6367
encoder.SetEscapeHTML(j.escapeHTML)
6468
if j.pretty {
65-
encoder.SetIndent("", " ")
69+
encoder.SetIndent("", j.indent)
6670
}
6771

6872
return encoder.Encode(bom)
@@ -78,6 +82,12 @@ func (j jsonBOMEncoder) EncodeVersion(bom *BOM, specVersion SpecVersion) (err er
7882
return j.Encode(bom)
7983
}
8084

85+
// SetIndent implements the BOMEncoder interface.
86+
func (j *jsonBOMEncoder) SetIndent(indent string) BOMEncoder {
87+
j.indent = indent
88+
return j
89+
}
90+
8191
// SetPretty implements the BOMEncoder interface.
8292
func (j *jsonBOMEncoder) SetPretty(pretty bool) BOMEncoder {
8393
j.pretty = pretty
@@ -93,6 +103,7 @@ func (j *jsonBOMEncoder) SetEscapeHTML(escapeHTML bool) BOMEncoder {
93103
type xmlBOMEncoder struct {
94104
writer io.Writer
95105
pretty bool
106+
indent string
96107
}
97108

98109
// Encode implements the BOMEncoder interface.
@@ -103,7 +114,7 @@ func (x xmlBOMEncoder) Encode(bom *BOM) error {
103114

104115
encoder := xml.NewEncoder(x.writer)
105116
if x.pretty {
106-
encoder.Indent("", " ")
117+
encoder.Indent("", x.indent)
107118
}
108119

109120
return encoder.Encode(bom)
@@ -130,3 +141,9 @@ func (j *xmlBOMEncoder) SetEscapeHTML(escapeHTML bool) BOMEncoder {
130141
// NOOP -- XML always needs to escape HTML
131142
return j
132143
}
144+
145+
// SetIndent implements the BOMEncoder interface.
146+
func (x *xmlBOMEncoder) SetIndent(indent string) BOMEncoder {
147+
x.indent = indent
148+
return x
149+
}

encode_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,39 @@ func TestJsonBOMEncoder_SetPretty(t *testing.T) {
6565
`, buf.String())
6666
}
6767

68+
func TestJsonBOMEncoder_SetIndentTab(t *testing.T) {
69+
buf := new(bytes.Buffer)
70+
encoder := NewBOMEncoder(buf, BOMFileFormatJSON)
71+
encoder.SetPretty(true)
72+
encoder.SetIndent("\t")
73+
74+
bom := NewBOM()
75+
bom.Metadata = &Metadata{
76+
Authors: &[]OrganizationalContact{
77+
{
78+
Name: "authorName",
79+
},
80+
},
81+
}
82+
83+
require.NoError(t, encoder.Encode(bom))
84+
85+
assert.Equal(t, `{
86+
"$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
87+
"bomFormat": "CycloneDX",
88+
"specVersion": "1.6",
89+
"version": 1,
90+
"metadata": {
91+
"authors": [
92+
{
93+
"name": "authorName"
94+
}
95+
]
96+
}
97+
}
98+
`, buf.String())
99+
}
100+
68101
func TestJsonBOMEncoder_SetEscapeHTML_true(t *testing.T) {
69102
buf := new(bytes.Buffer)
70103
encoder := NewBOMEncoder(buf, BOMFileFormatJSON)
@@ -173,6 +206,49 @@ func TestXmlBOMEncoder_SetPretty(t *testing.T) {
173206
</bom>`, buf.String())
174207
}
175208

209+
func TestXmlBOMEncoder_SetIndentTab(t *testing.T) {
210+
buf := new(bytes.Buffer)
211+
encoder := NewBOMEncoder(buf, BOMFileFormatXML)
212+
encoder.SetPretty(true)
213+
encoder.SetIndent("\t")
214+
215+
bom := NewBOM()
216+
bom.Metadata = &Metadata{
217+
Authors: &[]OrganizationalContact{
218+
{
219+
Name: "authorName",
220+
},
221+
},
222+
Properties: &[]Property{
223+
{
224+
Name: "XML",
225+
Value: "<xml>in here</xml>",
226+
},
227+
{
228+
Name: "Specials",
229+
Value: "Special chars: < & > \"",
230+
},
231+
},
232+
}
233+
234+
require.NoError(t, encoder.Encode(bom))
235+
236+
assert.Equal(t, `<?xml version="1.0" encoding="UTF-8"?>
237+
<bom xmlns="http://cyclonedx.org/schema/bom/1.6" version="1">
238+
<metadata>
239+
<authors>
240+
<author>
241+
<name>authorName</name>
242+
</author>
243+
</authors>
244+
<properties>
245+
<property name="XML">&lt;xml&gt;in here&lt;/xml&gt;</property>
246+
<property name="Specials">Special chars: &lt; &amp; &gt; &#34;</property>
247+
</properties>
248+
</metadata>
249+
</bom>`, buf.String())
250+
}
251+
176252
func TestJsonBOMEncoder_EncodeVersion(t *testing.T) {
177253
t.Run(SpecVersion1_0.String(), func(t *testing.T) {
178254
err := NewBOMEncoder(io.Discard, BOMFileFormatJSON).EncodeVersion(NewBOM(), SpecVersion1_0)

0 commit comments

Comments
 (0)