Skip to content

Commit

Permalink
Fix block style strip multi-line issue
Browse files Browse the repository at this point in the history
As reported in homeport/dyff#180, when using text
that spans over multiple lines using the strip option (`|-`), the output was
not created correctly.

Simplify multi-line output code to properly place a newline at every line with
the last line being an exception.
  • Loading branch information
HeavyWombat committed Sep 17, 2021
1 parent 3d24cf8 commit 32cd0b1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
11 changes: 5 additions & 6 deletions output_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,12 @@ func (p *OutputProcessor) neatYAMLofNode(prefix string, skipIndentOnFirstLine bo
colorName = "multiLineTextColor"
fmt.Fprint(p.out, p.colorize("|", colorName), "\n")
for i, line := range lines {
if i == len(lines)-1 {
continue
}

fmt.Fprint(p.out, prefix, p.colorize(line, colorName))
fmt.Fprint(p.out,
prefix,
p.colorize(line, colorName),
)

if i < len(lines)-2 {
if i != len(lines)-1 {
fmt.Fprint(p.out, "\n")
}
}
Expand Down
33 changes: 32 additions & 1 deletion output_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ list: # at list definition
- two # two
# before multiline
multiline: |
multiline: |-
This is
a multi
line te
Expand Down Expand Up @@ -333,6 +333,37 @@ yaml:
Expect(err).ToNot(HaveOccurred())
Expect(output).To(BeEquivalentTo(expected))
})

It("should create YAML output of multi-line text", func() {
example := []byte(`---
data:
repos.yaml: |-
repos:
- apply_requirements:
- approved
- mergeable
id: /.*/
test: /.*/
`)

expected := `---
data:
repos.yaml: |
repos:
- apply_requirements:
- approved
- mergeable
id: /.*/
test: /.*/
`

var node yamlv3.Node
Expect(yamlv3.Unmarshal(example, &node)).ToNot(HaveOccurred())

output, err := ToYAMLString(node)
Expect(err).ToNot(HaveOccurred())
Expect(output).To(BeEquivalentTo(expected))
})
})

Context("create YAML output for type struct", func() {
Expand Down

0 comments on commit 32cd0b1

Please sign in to comment.