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

Error when using BytesUnmarshaler together with array aliases #431

Open
jsangmeister opened this issue Mar 5, 2024 · 0 comments
Open

Error when using BytesUnmarshaler together with array aliases #431

jsangmeister opened this issue Mar 5, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@jsangmeister
Copy link

Describe the bug

When an anchor is defined for an array and it is aliased in a node which has a custom unmarshaler, the unmarshaling fails if the indentation of the anchor and the alias do not match.

To Reproduce

main.go:

package main

import (
	"fmt"
	"os"

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

type Document struct {
	Data struct {
		Element struct {
			List []string
			Other bool
		}
	}
}

func (d *Document) UnmarshalYAML(node []byte) error {
	return yaml.Unmarshal(node, &d)
}

func main() {
	r, _ := os.Open("test.yml")
	var d Document
	err := yaml.NewDecoder(r).Decode(&d)
	fmt.Println(err)
}

test.yml:

list: &list
  - a
  - b
  - c

data:
  element:
    list: *list
    other: true

When running go run main.go, the following error is printed:

[10:5] unexpected key name
       7 | null
       8 |   - a
       9 |   - b
    > 10 |   - c
      11 |     other: true
               ^

Expected behavior
The alias should be parsed without error.

Version Variables

  • Go version: 1.22
  • go-yaml's Version: v1.11.3

Additional context
The test.yml file is valid YAML according to, e.g., https://www.yamllint.com/. We strongly suspect that the error stems from a simple string replacement, where aliases are simply replaced with the string content of the anchor before unmarshaling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant