Skip to content

Commit 3573e41

Browse files
committed
default true booleans should not be rendered in YAML
1 parent 29f8189 commit 3573e41

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

pkg/pipeline/pipeline.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ func (b *DefaultTrueBool) UnmarshalJSON(data []byte) error {
130130
return nil
131131
}
132132

133+
func (b DefaultTrueBool) IsZero() bool {
134+
return b.Value == nil || *b.Value == true
135+
}
136+
133137
func (b DefaultTrueBool) MarshalJSON() ([]byte, error) {
134138
if b.Value == nil {
135139
return []byte("true"), nil
@@ -158,7 +162,7 @@ func (b *DefaultTrueBool) Bool() bool {
158162
}
159163

160164
func (b DefaultTrueBool) MarshalYAML() (interface{}, error) {
161-
if b.Value == nil {
165+
if b.Value == nil || *b.Value {
162166
return nil, nil
163167
}
164168

pkg/pipeline/pipeline_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package pipeline_test
22

33
import (
4+
"bytes"
45
"encoding/json"
6+
"gopkg.in/yaml.v3"
57
"log"
68
"os"
79
"path/filepath"
@@ -892,3 +894,54 @@ func BenchmarkClearSpacesAtLineEndings(b *testing.B) {
892894
pipeline.ClearSpacesAtLineEndings(content)
893895
}
894896
}
897+
898+
func TestDefaultTrueBool_MarshalYAML(t *testing.T) {
899+
t.Parallel()
900+
901+
type testStruct struct {
902+
Field1 string `yaml:"field1"`
903+
Bool pipeline.DefaultTrueBool `yaml:"bool,omitempty"`
904+
}
905+
906+
trueVal := true
907+
falseVal := false
908+
909+
tests := []struct {
910+
name string
911+
input testStruct
912+
want string
913+
}{
914+
{
915+
name: "true values should be omitted",
916+
input: testStruct{
917+
Field1: "field1",
918+
Bool: pipeline.DefaultTrueBool{Value: &trueVal},
919+
},
920+
want: "field1: field1\n",
921+
},
922+
{
923+
name: "false values should be marshalled",
924+
input: testStruct{
925+
Field1: "field1",
926+
Bool: pipeline.DefaultTrueBool{Value: &falseVal},
927+
},
928+
want: "field1: field1\nbool: false\n",
929+
},
930+
}
931+
932+
for _, tt := range tests {
933+
t.Run(tt.name, func(t *testing.T) {
934+
t.Parallel()
935+
936+
buf := bytes.NewBuffer(nil)
937+
enc := yaml.NewEncoder(buf)
938+
enc.SetIndent(2)
939+
940+
err := enc.Encode(tt.input)
941+
require.NoError(t, err)
942+
943+
yamlConfig := buf.Bytes()
944+
assert.Equal(t, tt.want, string(yamlConfig))
945+
})
946+
}
947+
}

0 commit comments

Comments
 (0)