Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions formatters/basic/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ map:
map:
*a : 1`,
},
{
name: "retains implicit null in flow mapping",
input: `a: {b: }`,
expect: `a: {b:}`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -397,12 +402,7 @@ func TestRetainLineBreaksLongLine(t *testing.T) {

func stripTrailingNewline(s string) string {
// strip trailing \n or \r\n characters
if len(s) > 0 {
if s[len(s)-1] == '\n' {
s = s[:len(s)-1]
} else if len(s) > 1 && s[len(s)-2] == '\r' {
s = s[:len(s)-2]
}
}
s = strings.TrimSuffix(s, "\r\n")
s = strings.TrimSuffix(s, "\n")
return s
}
4 changes: 2 additions & 2 deletions pkg/yaml/emitterc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ func yaml_emitter_select_scalar_style(emitter *yaml_emitter_t, event *yaml_event
emitter.flow_level == 0 && !emitter.scalar_data.block_plain_allowed {
style = yaml_SINGLE_QUOTED_SCALAR_STYLE
}
if len(emitter.scalar_data.value) == 0 && (emitter.flow_level > 0 || emitter.simple_key_context) {
if len(emitter.scalar_data.value) == 0 && emitter.simple_key_context {
style = yaml_SINGLE_QUOTED_SCALAR_STYLE
}
if no_tag && !event.implicit {
Expand Down Expand Up @@ -1312,7 +1312,7 @@ func yaml_emitter_analyze_scalar(emitter *yaml_emitter_t, value []byte) bool {

if len(value) == 0 {
emitter.scalar_data.multiline = false
emitter.scalar_data.flow_plain_allowed = false
emitter.scalar_data.flow_plain_allowed = true
emitter.scalar_data.block_plain_allowed = true
emitter.scalar_data.single_quoted_allowed = true
emitter.scalar_data.block_allowed = false
Expand Down
11 changes: 11 additions & 0 deletions pkg/yaml/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2856,6 +2856,17 @@ func (s *S) TestNodeOmitEmpty(c *C) {
c.Assert(err, ErrorMatches, "yaml: cannot encode node with unknown kind 0")
}

func (s *S) TestFlowMappingImplicitNull(c *C) {
input := "a: {b: }\n"
var n yaml.Node
err := yaml.Unmarshal([]byte(input), &n)
c.Assert(err, IsNil)

data, err := yaml.Marshal(&n)
c.Assert(err, IsNil)
c.Assert(string(data), Equals, "a: {b:}\n")
}

func fprintComments(out io.Writer, node *yaml.Node, indent string) {
switch node.Kind {
case yaml.ScalarNode:
Expand Down
Loading