Skip to content

Commit 32cd0b1

Browse files
committed
Fix block style strip multi-line issue
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.
1 parent 3d24cf8 commit 32cd0b1

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

output_yaml.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,12 @@ func (p *OutputProcessor) neatYAMLofNode(prefix string, skipIndentOnFirstLine bo
292292
colorName = "multiLineTextColor"
293293
fmt.Fprint(p.out, p.colorize("|", colorName), "\n")
294294
for i, line := range lines {
295-
if i == len(lines)-1 {
296-
continue
297-
}
298-
299-
fmt.Fprint(p.out, prefix, p.colorize(line, colorName))
295+
fmt.Fprint(p.out,
296+
prefix,
297+
p.colorize(line, colorName),
298+
)
300299

301-
if i < len(lines)-2 {
300+
if i != len(lines)-1 {
302301
fmt.Fprint(p.out, "\n")
303302
}
304303
}

output_yaml_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ list: # at list definition
194194
- two # two
195195
196196
# before multiline
197-
multiline: |
197+
multiline: |-
198198
This is
199199
a multi
200200
line te
@@ -333,6 +333,37 @@ yaml:
333333
Expect(err).ToNot(HaveOccurred())
334334
Expect(output).To(BeEquivalentTo(expected))
335335
})
336+
337+
It("should create YAML output of multi-line text", func() {
338+
example := []byte(`---
339+
data:
340+
repos.yaml: |-
341+
repos:
342+
- apply_requirements:
343+
- approved
344+
- mergeable
345+
id: /.*/
346+
test: /.*/
347+
`)
348+
349+
expected := `---
350+
data:
351+
repos.yaml: |
352+
repos:
353+
- apply_requirements:
354+
- approved
355+
- mergeable
356+
id: /.*/
357+
test: /.*/
358+
`
359+
360+
var node yamlv3.Node
361+
Expect(yamlv3.Unmarshal(example, &node)).ToNot(HaveOccurred())
362+
363+
output, err := ToYAMLString(node)
364+
Expect(err).ToNot(HaveOccurred())
365+
Expect(output).To(BeEquivalentTo(expected))
366+
})
336367
})
337368

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

0 commit comments

Comments
 (0)