Add "pretty" json configuration and change default behavior to be space-efficient (#2275)

* expose underlying format options

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* remove escape html options and address PR feedback

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* incorporate PR feedback

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* fix cli test

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

---------

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Alex Goodman
2023-11-20 15:29:34 +00:00
committed by GitHub
parent 7cfb5f630a
commit 1676934c63
31 changed files with 723 additions and 204 deletions
@@ -14,9 +14,10 @@ const DefaultVersion = "1.5"
type Encoder struct {
version cyclonedx.SpecVersion
format cyclonedx.BOMFileFormat
pretty bool
}
func NewEncoder(version string, format cyclonedx.BOMFileFormat) (Encoder, error) {
func NewEncoder(version string, format cyclonedx.BOMFileFormat, pretty bool) (Encoder, error) {
specVersion, err := SpecVersionFromString(version)
if err != nil {
return Encoder{}, err
@@ -24,13 +25,14 @@ func NewEncoder(version string, format cyclonedx.BOMFileFormat) (Encoder, error)
return Encoder{
version: specVersion,
format: format,
pretty: pretty,
}, nil
}
func (e Encoder) Encode(writer io.Writer, s sbom.SBOM) error {
bom := cyclonedxhelpers.ToFormatModel(s)
enc := cyclonedx.NewBOMEncoder(writer, e.format)
enc.SetPretty(true)
enc.SetPretty(e.pretty)
enc.SetEscapeHTML(false)
return enc.EncodeVersion(bom, e.version)
+7 -4
View File
@@ -48,12 +48,8 @@ func AssertEncoderAgainstGoldenSnapshot(t *testing.T, cfg EncoderSnapshotTestCon
return
}
var expected []byte
if cfg.Redactor != nil {
actual = cfg.Redactor.Redact(actual)
expected = cfg.Redactor.Redact(testutils.GetGoldenFileContents(t))
} else {
expected = testutils.GetGoldenFileContents(t)
}
if cfg.UpdateSnapshot && cfg.PersistRedactionsInSnapshot {
@@ -62,6 +58,13 @@ func AssertEncoderAgainstGoldenSnapshot(t *testing.T, cfg EncoderSnapshotTestCon
return
}
var expected []byte
if cfg.Redactor != nil {
expected = cfg.Redactor.Redact(testutils.GetGoldenFileContents(t))
} else {
expected = testutils.GetGoldenFileContents(t)
}
if cfg.IsJSON {
require.JSONEq(t, string(expected), string(actual))
} else {