goccy / go-yaml

YAML support for the Go language
MIT License
1.17k stars 131 forks source link

Parser returns two documents instead of one #374

Open andrewkroh opened 1 year ago

andrewkroh commented 1 year ago

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:

```go 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 commented 19 hours ago

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 commented 19 hours ago

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