Repository: goccy/go-yaml
Pull request #523 (2024-11-12 01:22:47)

Fix validation of map at flow mode
fix https://github.com/goccy/go-yaml/issues/519

codecov-commenter: :warning: Please install the !['codecov app svg image'](https://github.com/codecov/engineering-team/assets/152432831/e90313f4-9d3a-4b63-8b54-cfe14e7ec20d) to ensure uploads and comments are reliably processed by Codecov.

## [Codecov](https://app.codecov.io/gh/goccy/go-yaml/pull/523?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 79.21%. Comparing base [(`c82f89f`)](https://app.codecov.io/gh/goccy/go-yaml/commit/c82f89f6e033c2d2c101a0c0395cdca2b9c07644?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima) to head [(`0ba8d9c`)](https://app.codecov.io/gh/goccy/go-yaml/commit/0ba8d9c07c289ef502c571207fdf3d8ea187f3e9?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Masaaki+Goshima).

:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master     #523      +/-   ##
==========================================
+ Coverage   79.16%   79.21%   +0.05%     
==========================================
  Files          17       17              
  Lines        6129     6144      +15     
==========================================
+ Hits         4852     4867      +15     
  Misses        965      965              
  Partials      312      312              
```

</details>

Patch:
diff --git a/parser/parser.go b/parser/parser.go
index 846469b..60f9db5 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -202,7 +202,7 @@ func (p *parser) parseSequence(ctx *context) (*ast.SequenceNode, error) {
 			break
 		}
 
-		value, err := p.parseToken(ctx.withIndex(uint(len(node.Values))), p.currentToken())
+		value, err := p.parseToken(ctx.withIndex(uint(len(node.Values))).withFlow(true), p.currentToken())
 		if err != nil {
 			return nil, err
 		}
@@ -481,14 +481,8 @@ func (p *parser) parseMappingValue(ctx *context) (ast.Node, error) {
 		ntk = p.nextNotCommentToken()
 		antk = p.afterNextNotCommentToken()
 	}
-	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)
+	if err := p.validateMapNextToken(ctx, node); err != nil {
+		return nil, err
 	}
 	if len(node.Values) == 1 {
 		mapKeyCol := mvnode.Key.GetToken().Position.Column
@@ -512,6 +506,31 @@ func (p *parser) parseMappingValue(ctx *context) (ast.Node, error) {
 	return node, nil
 }
 
+func (p *parser) validateMapNextToken(ctx *context, node *ast.MappingNode) error {
+	keyTk := node.Start
+	if len(node.Values) != 0 {
+		keyTk = node.Values[len(node.Values)-1].Key.GetToken()
+	}
+	tk := p.nextNotCommentToken()
+	if tk == nil {
+		return nil
+	}
+
+	if ctx.isFlow && (tk.Type == token.CollectEntryType || tk.Type == token.SequenceEndType || tk.Type == token.MappingEndType) {
+		// a: {
+		//  key: value
+		// } , <= if context is flow mode, "," or "]" or "}" is allowed.
+		return nil
+	}
+
+	if tk.Position.Line > keyTk.Position.Line && tk.Position.Column > keyTk.Position.Column {
+		// a: b
+		//   c <= this token is invalid.
+		return errors.ErrSyntax("value is not allowed in this context", tk)
+	}
+	return nil
+}
+
 func (p *parser) parseFlowMapNullValue(ctx *context, key ast.MapKeyNode) (*ast.MappingValueNode, error) {
 	tk := p.currentToken()
 	if tk == nil {


Test patch:
diff --git a/parser/parser_test.go b/parser/parser_test.go
index 78baba9..b6153bf 100644
--- a/parser/parser_test.go
+++ b/parser/parser_test.go
@@ -96,6 +96,26 @@ func TestParser(t *testing.T) {
 		"a:\n-",
 		"a: {foo}",
 		"a: {foo,bar}",
+		`
+{
+  a: {
+    b: c
+  },
+  d: e
+}
+`,
+		`
+[
+  a: {
+    b: c
+  }]
+`,
+		`
+{
+  a: {
+    b: c
+  }}
+`,
 	}
 	for _, src := range sources {
 		if _, err := parser.Parse(lexer.Tokenize(src), 0); err != nil {