Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: insert a separator between each encoded document #318

Merged
merged 1 commit into from Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions encode.go
Expand Up @@ -39,6 +39,7 @@ type Encoder struct {
anchorPtrToNameMap map[uintptr]string
useLiteralStyleIfMultiline bool
commentMap map[*Path]*Comment
written bool

line int
column int
Expand Down Expand Up @@ -86,6 +87,12 @@ func (e *Encoder) EncodeContext(ctx context.Context, v interface{}) error {
if err := e.setCommentByCommentMap(node); err != nil {
return errors.Wrapf(err, "failed to set comment by comment map")
}
if !e.written {
e.written = true
} else {
// write document separator
e.writer.Write([]byte("---\n"))
}
var p printer.Printer
e.writer.Write(p.PrintNode(node))
return nil
Expand Down
14 changes: 14 additions & 0 deletions encode_test.go
Expand Up @@ -1094,6 +1094,20 @@ a:
}
}

func TestEncoder_MultipleDocuments(t *testing.T) {
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
if err := enc.Encode(1); err != nil {
t.Fatalf("failed to encode: %s", err)
}
if err := enc.Encode(2); err != nil {
t.Fatalf("failed to encode: %s", err)
}
if actual, expect := buf.String(), "1\n---\n2\n"; actual != expect {
t.Errorf("expect:\n%s\nactual\n%s\n", expect, actual)
}
}

func Example_Marshal_Node() {
type T struct {
Text ast.Node `yaml:"text"`
Expand Down