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

Parser returns two documents instead of one #374

Closed
andrewkroh opened this issue May 30, 2023 · 2 comments · Fixed by #513
Closed

Parser returns two documents instead of one #374

andrewkroh opened this issue May 30, 2023 · 2 comments · Fixed by #513
Labels
bug Something isn't working parser

Comments

@andrewkroh
Copy link

The documents below both cause parser.ParseBytes to return an ast.File` that contains two documents. In each case it should only return a single document. I have included a test case that reproduces the issue.

---
script:
  line1

  line3
---
config:
  options:
# Comment
    mode: full

Playground: https://go.dev/play/p/Q62ku79uAzr

Test case:

package main

import (
	"testing"

	"github.com/goccy/go-yaml/parser"
)

func TestUnexpectedYAMLDocCountParsing(t *testing.T) {
	testCases := []struct {
		name string
		yaml string
	}{
		// Real examples:
		//  https://github.com/elastic/integrations/blob/5e886328f00bfd1639a61920e1ace60e9d7cc50a/packages/infoblox_nios/data_stream/log/elasticsearch/ingest_pipeline/pipeline_dns.yml#L80
		//  https://github.com/elastic/integrations/blob/5e886328f00bfd1639a61920e1ace60e9d7cc50a/packages/fortinet_fortigate/data_stream/log/elasticsearch/ingest_pipeline/default.yml#L53
		{
			name: "newline in multi-line string",
			yaml: `
---
script:
  line1

  line3
`,
		},
		// Real example https://github.com/elastic/integrations/blob/5e886328f00bfd1639a61920e1ace60e9d7cc50a/packages/zeronetworks/data_stream/audit/elasticsearch/ingest_pipeline/default.yml#L92
		{
			name: "comment without indentation",
			yaml: `
---
config:
  options:
# Comment
    mode: full
`,
		},
	}

	for _, tc := range testCases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			f, err := parser.ParseBytes([]byte(tc.yaml), parser.ParseComments)
			if err != nil {
				t.Fatal(err)
			}

			if len(f.Docs) != 1 {
				t.Fatalf("expected doc count 1, got %d", len(f.Docs))
			}
		})
	}
}
@shuheiktgw
Copy link
Collaborator

Thank you for reporting the issue! For the first example, the yaml does not seem valid. It should be

---
script: |
  line1

  line3

instead if you want to write multi line values. For the second example, I've confirmed the issue has been already resolved so please use the latest version 🙂

@goccy goccy reopened this Nov 5, 2024
@goccy
Copy link
Owner

goccy commented Nov 5, 2024

@shuheiktgw This first example seems to be correct. It appears that even with this format, it must be handled as a multi-line string.

@goccy goccy added bug Something isn't working parser labels Nov 6, 2024
@goccy goccy closed this as completed in #513 Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants