Skip to content

Commit 13509ad

Browse files
committed
Change which methods get a newline and fix tests
Merge the "regular" and "compact" tests together.
1 parent 59c2c43 commit 13509ad

File tree

2 files changed

+595
-405
lines changed

2 files changed

+595
-405
lines changed

kyaml/kyaml.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func (ky *Encoder) FromYAML(in io.Reader, out io.Writer) error {
9696
if err := ky.renderDocument(&doc, 0, ky.flags(), out); err != nil {
9797
return err
9898
}
99+
fmt.Fprintf(out, "\n")
99100
}
100101

101102
return nil
@@ -112,7 +113,8 @@ func (ky *Encoder) FromObject(obj any, out io.Writer) error {
112113
return ky.FromYAML(bytes.NewReader(jb), out)
113114
}
114115

115-
// Marshal renders a single Go object as KYAML, without the header.
116+
// Marshal renders a single Go object as KYAML, without the header or trailing
117+
// newline.
116118
func (ky *Encoder) Marshal(obj any) ([]byte, error) {
117119
// Convert the object to JSON bytes to take advantage of all the JSON tag
118120
// handling and things like that.
@@ -210,7 +212,7 @@ func (ky *Encoder) renderNode(node *yaml.Node, indent int, flags flagMask, out i
210212

211213
// renderDocument processes a YAML document node, rendering it to the output.
212214
// This function assumes that the output "cursor" is positioned at the start of
213-
// the document and should always emit a final newline.
215+
// the document. This does not emit a final newline.
214216
func (ky *Encoder) renderDocument(doc *yaml.Node, indent int, flags flagMask, out io.Writer) error {
215217
if len(doc.Content) == 0 {
216218
return fmt.Errorf("kyaml internal error: line %d: document has no content node (%d)", doc.Line, len(doc.Content))
@@ -243,18 +245,17 @@ func (ky *Encoder) renderDocument(doc *yaml.Node, indent int, flags flagMask, ou
243245
if len(child.LineComment) > 0 {
244246
ky.renderComments(" "+child.LineComment, 0, out)
245247
}
246-
fmt.Fprint(out, "\n")
247248
if len(child.FootComment) > 0 {
248-
ky.renderComments(child.FootComment, indent, out)
249249
fmt.Fprint(out, "\n")
250+
ky.renderComments(child.FootComment, indent, out)
250251
}
251252
if len(doc.LineComment) > 0 {
252-
ky.renderComments(" "+doc.LineComment, 0, out)
253253
fmt.Fprint(out, "\n")
254+
ky.renderComments(" "+doc.LineComment, 0, out)
254255
}
255256
if len(doc.FootComment) > 0 {
256-
ky.renderComments(doc.FootComment, indent, out)
257257
fmt.Fprint(out, "\n")
258+
ky.renderComments(doc.FootComment, indent, out)
258259
}
259260
}
260261
return nil

0 commit comments

Comments
 (0)