From e8674a8c87030d45ee468016f7fdf3290e2840ec Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sat, 9 Nov 2024 12:56:38 +0900 Subject: [PATCH] fix validation --- parser/parser.go | 6 ++++- parser/parser_test.go | 47 +++++++++++++++++++++++++++++++++++++++ scanner/scanner.go | 3 +++ testdata/validate_test.go | 16 ++++++------- 4 files changed, 63 insertions(+), 9 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index 65bff398..36840565 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -481,7 +481,11 @@ func (p *parser) parseMappingValue(ctx *context) (ast.Node, error) { ntk = p.nextNotCommentToken() antk = p.afterNextNotCommentToken() } - if tk := p.nextNotCommentToken(); tk != nil && tk.Position.Line > node.Start.Position.Line && tk.Position.Column > node.Start.Position.Column { + validationTk := node.Start + if len(node.Values) != 0 { + validationTk = node.Values[len(node.Values)-1].Key.GetToken() + } + if tk := p.nextNotCommentToken(); tk != nil && tk.Position.Line > validationTk.Position.Line && tk.Position.Column > validationTk.Position.Column { // a: b // c <= this token is invalid. return nil, errors.ErrSyntax("value is not allowed in this context", tk) diff --git a/parser/parser_test.go b/parser/parser_test.go index ae2fe635..281cd945 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -608,6 +608,20 @@ i: 'j' "e": "f" g: "h" i: 'j' +`, + }, + { + ` +a: + - |2 + b + c: d +`, + ` +a: + - |2 + b + c: d `, }, } @@ -1165,6 +1179,39 @@ b: - 2 ^ `, }, + { + ` +a: + - | + b + c: d +`, + ` +[5:5] value is not allowed in this context + 2 | a: + 3 | - | + 4 | b +> 5 | c: d + ^ +`, + }, + { + ` +a: + - | + b + c: + d: e +`, + ` +[5:5] value is not allowed in this context + 2 | a: + 3 | - | + 4 | b +> 5 | c: + ^ + 6 | d: e`, + }, } for _, test := range tests { t.Run(test.source, func(t *testing.T) { diff --git a/scanner/scanner.go b/scanner/scanner.go index 24d03820..7d7cbbd2 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -571,6 +571,9 @@ func (s *Scanner) scanDocument(ctx *Context, c rune) error { s.progressColumn(ctx, 1) } else { ctx.updateDocumentLineIndentColumn(s.column) + if ctx.docFirstLineIndentColumn > 0 { + s.lastDelimColumn = ctx.docFirstLineIndentColumn - 1 + } if err := ctx.validateDocumentLineIndentColumn(); err != nil { invalidTk := token.Invalid(string(ctx.obuf), s.pos()) s.progressColumn(ctx, 1) diff --git a/testdata/validate_test.go b/testdata/validate_test.go index d73ee3a3..d33f95a2 100644 --- a/testdata/validate_test.go +++ b/testdata/validate_test.go @@ -117,11 +117,11 @@ name: myDocument roles: name: myRole permissions: - - hello - - how - - are - - you - `, + - hello + - how + - are + - you +`, ExpectedErr: `[4:7] mapping was used where sequence is expected 1 | --- 2 | name: myDocument @@ -129,9 +129,9 @@ roles: > 4 | name: myRole ^ 5 | permissions: - 6 | - hello - 7 | - how - 8 | `, + 6 | - hello + 7 | - how + 8 | `, Instance: &struct { Name string `yaml:"name"` Roles []struct {