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

Handling reserved character #336

Closed
kanata2 opened this issue Dec 19, 2022 · 2 comments · Fixed by #514
Closed

Handling reserved character #336

kanata2 opened this issue Dec 19, 2022 · 2 comments · Fixed by #514
Labels
bug Something isn't working parser

Comments

@kanata2
Copy link

kanata2 commented Dec 19, 2022

Description

I found a behavior that I am curious about. Please let me confirm if this is the intended behavior.

The "@" (x40, at) and "`" (x60, grave accent) are reserved for future use.
-- https://yaml.org/spec/1.2.2/

According to the YAML 1.2.2 specification, "@" is a reserved character, but when using goccy/go-yaml, it seemed to be treated as a non-reserved character.
The following is the result of the verification.

Behavior

I have checked the behavior of both with the following code.

package main

import "github.com/goccy/go-yaml" // or "gopkg.in/yaml.v3"

type s struct {
	Ss []string
}

var b = []byte(`ss: [@foo, @bar]`)

func main() {
	var s s
	if err := yaml.Unmarshal(b, &s); err != nil {
		panic(err)
	}
	for _, o := range s.Ss {
		println(o)
	}
}

goccy/go-yaml v1.9.2

$ go run main.go
@foo
@bar

gopkg.in/yaml.v3 v3.0.1

$ go run main.go
panic: yaml: found character that cannot start any token
...
@shuheiktgw shuheiktgw added the bug Something isn't working label Nov 6, 2024
@shuheiktgw
Copy link
Collaborator

I confirmed both go-yaml/yaml and Online YAML Parser failed parsing the characters "@" and "`" while the latest goccy/go-yaml successfully parsing the characters.

import (
	"testing"

	"gopkg.in/yaml.v3"
)

func Test(t *testing.T) {
	tests := []struct {
		desc  string
		input string
	}{
		{
			desc:  "@",
			input: "key: [@val1, @val2]",
		},
		{
			desc:  "`",
			input: "key: [`val1, `val2]",
		},
	}

	for _, tc := range tests {
		t.Run(tc.desc, func(t *testing.T) {
			unmarshalled := map[string]any{}
			if err := yaml.Unmarshal([]byte(tc.input), &unmarshalled); err != nil {
				t.Fatal(err)
			}
		})
	}
}
=== RUN   Test
=== RUN   Test/@
    test_test.go:29: yaml: found character that cannot start any token
--- FAIL: Test/@ (0.00s)

=== RUN   Test/`
    test_test.go:29: yaml: found character that cannot start any token
--- FAIL: Test/` (0.00s)

--- FAIL: Test (0.00s)

@goccy
Copy link
Owner

goccy commented Nov 6, 2024

Thank you for your reporting !

I understand this problem.

The “@” (x40, at) and “`” (x60, grave accent) are reserved for future use.

https://yaml.org/spec/1.2.2/#10213-integer:~:text=Example%205.9%20Directive%20Indicator

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