Skip to content

Commit 2e71322

Browse files
option to wrap changes in object (#692)
1 parent 6a9122b commit 2e71322

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

formatters/format_json.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (f JSONFormatter) RenderSummary(diff *diff.Diff, opts RenderOpts) ([]byte,
3030
}
3131

3232
func (f JSONFormatter) RenderChangelog(changes checker.Changes, opts RenderOpts, specInfoPair *load.SpecInfoPair) ([]byte, error) {
33-
return printJSON(NewChanges(changes, f.Localizer))
33+
return printJSON(adaptStructure(NewChanges(changes, f.Localizer), opts.WrapInObject))
3434
}
3535

3636
func (f JSONFormatter) RenderChecks(checks Checks, opts RenderOpts) ([]byte, error) {
@@ -57,3 +57,11 @@ func printJSON(output interface{}) ([]byte, error) {
5757

5858
return bytes, nil
5959
}
60+
61+
func adaptStructure(output any, wrapInObject bool) any {
62+
if wrapInObject {
63+
output = map[string]any{"changes": output}
64+
}
65+
66+
return output
67+
}

formatters/format_yaml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (f YAMLFormatter) RenderSummary(diff *diff.Diff, opts RenderOpts) ([]byte,
3030
}
3131

3232
func (f YAMLFormatter) RenderChangelog(changes checker.Changes, opts RenderOpts, specInfoPair *load.SpecInfoPair) ([]byte, error) {
33-
return printYAML(NewChanges(changes, f.Localizer))
33+
return printYAML(adaptStructure(NewChanges(changes, f.Localizer), opts.WrapInObject))
3434
}
3535

3636
func (f YAMLFormatter) RenderChecks(checks Checks, opts RenderOpts) ([]byte, error) {

formatters/format_yaml_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package formatters_test
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/oasdiff/oasdiff/checker"
@@ -31,6 +32,19 @@ func TestYamlFormatter_RenderChangelog(t *testing.T) {
3132
require.Equal(t, "- id: change_id\n text: This is a breaking change.\n level: 3\n section: components\n", string(out))
3233
}
3334

35+
func TestYamlFormatter_RenderChangelogWithWrapInObject(t *testing.T) {
36+
testChanges := checker.Changes{
37+
checker.ComponentChange{
38+
Id: "change_id",
39+
Level: checker.ERR,
40+
},
41+
}
42+
43+
out, err := yamlFormatter.RenderChangelog(testChanges, formatters.RenderOpts{WrapInObject: true}, nil)
44+
require.NoError(t, err)
45+
require.True(t, strings.HasPrefix(string(out), "changes:"))
46+
}
47+
3448
func TestYamlFormatter_RenderChecks(t *testing.T) {
3549
checks := formatters.Checks{
3650
{

formatters/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ type FormatterOpts struct {
3939

4040
// RenderOpts can be used to pass properties to the renderer method
4141
type RenderOpts struct {
42-
ColorMode checker.ColorMode
42+
ColorMode checker.ColorMode
43+
WrapInObject bool // wrap the output in a JSON object with the key "changes"
4344
}
4445

4546
func NewRenderOpts() RenderOpts {

0 commit comments

Comments
 (0)