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

scanner: Find quoted token from context #327

Closed
wants to merge 5 commits into from
Closed
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
23 changes: 23 additions & 0 deletions decode_test.go
Expand Up @@ -2144,6 +2144,29 @@ b: *a
t.Fatal("failed to unmarshal")
}
})
t.Run("quoted map keys", func(t *testing.T) {
t.Parallel()
yml := `
a:
"b": 2
'c': true
`
var v struct {
A struct {
B int
C bool
}
}
if err := yaml.Unmarshal([]byte(yml), &v); err != nil {
t.Fatalf("failed to unmarshal %v", err)
}
if v.A.B != 2 {
t.Fatalf("expected a.b to equal 2 but was %d", v.A.B)
}
if !v.A.C {
t.Fatal("expected a.c to be true but was false")
}
})
}

type unmarshalablePtrStringContainer struct {
Expand Down
111 changes: 111 additions & 0 deletions lexer/lexer_test.go
Expand Up @@ -56,6 +56,10 @@ func TestTokenize(t *testing.T) {
"a: 'Hello #comment'\n",
"a: 100.5\n",
"a: bogus\n",
"\"a\": double quoted map key",
"'a': single quoted map key",
"a: \"double quoted\"\nb: \"value map\"",
"a: 'single quoted'\nb: 'value map'",
}
for _, src := range sources {
lexer.Tokenize(src).Dump()
Expand Down Expand Up @@ -231,6 +235,60 @@ func TestSingleLineToken_ValueLineColumnPosition(t *testing.T) {
15: "]",
},
},
{
name: "double quote key",
src: `"a": b`,
expect: map[int]string{
1: "a",
4: ":",
6: "b",
},
},
{
name: "single quote key",
src: `'a': b`,
expect: map[int]string{
1: "a",
4: ":",
6: "b",
},
},
{
name: "double quote key and value",
src: `"a": "b"`,
expect: map[int]string{
1: "a",
4: ":",
6: "b",
},
},
{
name: "single quote key and value",
src: `'a': 'b'`,
expect: map[int]string{
1: "a",
4: ":",
6: "b",
},
},
{
name: "double quote key, single quote value",
src: `"a": 'b'`,
expect: map[int]string{
1: "a",
4: ":",
6: "b",
},
},
{
name: "single quote key, double quote value",
src: `'a': "b"`,
expect: map[int]string{
1: "a",
4: ":",
6: "b",
},
},
}

for _, tc := range tests {
Expand Down Expand Up @@ -432,6 +490,59 @@ foo2: 'bar2'`,
},
},
},
{
name: "single and double quote map keys",
src: `"a": test
'b': 1
c: true`,
expect: []testToken{
{
line: 1,
column: 1,
value: "a",
},
{
line: 1,
column: 4,
value: ":",
},
{
line: 1,
column: 6,
value: "test",
},
{
line: 2,
column: 1,
value: "b",
},
{
line: 2,
column: 4,
value: ":",
},
{
line: 2,
column: 6,
value: "1",
},
{
line: 3,
column: 1,
value: "c",
},
{
line: 3,
column: 2,
value: ":",
},
{
line: 3,
column: 4,
value: "true",
},
},
},
}

for _, tc := range tests {
Expand Down
18 changes: 18 additions & 0 deletions parser/parser_test.go
Expand Up @@ -80,6 +80,8 @@ func TestParser(t *testing.T) {
? !!str "implicit" : !!str "entry",
? !!null "" : !!null "",
}`,
"\"a\": a\n\"b\": b",
"'a': a\n'b': b",
}
for _, src := range sources {
if _, err := parser.Parse(lexer.Tokenize(src), 0); err != nil {
Expand Down Expand Up @@ -562,6 +564,22 @@ b: c
`
- key1: val
key2: ( foo + bar )
`,
},
{
`
"a": b
'c': d
"e": "f"
g: "h"
i: 'j'
`,
`
"a": b
'c': d
"e": "f"
g: "h"
i: 'j'
`,
},
}
Expand Down