Skip to content

Commit

Permalink
Merge pull request #135 from goccy/feature/fix-literal
Browse files Browse the repository at this point in the history
Fix processing of Literal with NewLineCharacter
  • Loading branch information
goccy authored Jun 15, 2020
2 parents a9c4340 + 9565ed1 commit 1fecb1c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func (n *LiteralNode) GetValue() interface{} {
// String literal to text
func (n *LiteralNode) String() string {
origin := n.Value.GetToken().Origin
return fmt.Sprintf("|\n%s", strings.TrimRight(strings.TrimRight(origin, " "), "\n"))
return fmt.Sprintf("%s\n%s", n.Start.Value, strings.TrimRight(strings.TrimRight(origin, " "), "\n"))
}

// MergeKeyNode type of merge key node
Expand Down
41 changes: 41 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1950,3 +1950,44 @@ b:
t.Fatal("failed to unmarshal with alias")
}
}

func TestDecoder_LiteralWithNewLine(t *testing.T) {
type A struct {
Node string `yaml:"b"`
LastNode string `yaml:"last"`
}
tests := []A{
A{
Node: "hello\nworld",
},
A{
Node: "hello\nworld\n",
},
A{
Node: "hello\nworld\n\n",
},
A{
LastNode: "hello\nworld",
},
A{
LastNode: "hello\nworld\n",
},
A{
LastNode: "hello\nworld\n\n",
},
}
// struct(want) -> Marshal -> Unmarchal -> struct(got)
for _, want := range tests {
bytes, _ := yaml.Marshal(want)
got := A{}
if err := yaml.Unmarshal(bytes, &got); err != nil {
t.Fatal(err)
}
if want.Node != got.Node {
t.Fatalf("expected:%q but got %q", want.Node, got.Node)
}
if want.LastNode != got.LastNode {
t.Fatalf("expected:%q but got %q", want.LastNode, got.LastNode)
}
}
}
24 changes: 24 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,30 @@ a:
d: e
- f: g
h: i
`,
},
{
`
a: |-
value
b: c
`,
`
a: |-
value
b: c
`,
},
{
`
a: |+
value
b: c
`,
`
a: |+
value
b: c
`,
},
}
Expand Down
2 changes: 1 addition & 1 deletion scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (s *Scanner) scanComment(ctx *Context) (tk *token.Token, pos int) {
func (s *Scanner) scanLiteral(ctx *Context, c rune) {
ctx.addOriginBuf(c)
if ctx.isEOS() {
if c != '\r' && c != '\n' {
if ctx.isLiteral {
ctx.addBuf(c)
}
value := ctx.bufferedSrc()
Expand Down

0 comments on commit 1fecb1c

Please sign in to comment.