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

Encode ast.StringNode line comment after header #296

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gabe565
Copy link

@gabe565 gabe565 commented May 18, 2022

Currently, if a ast.StringNode is multiline, it does not encode its line comment. This PR adds the comment after the block header similarly to ast.LiteralNode.

Block comments are allowed in the YAML 1.2 spec: https://yaml.org/spec/1.2-old/spec.html#id2793718, but block header comments must only be a single line. This PR conforms to the logic added in #234.

Example

package main

import (
	"fmt"
	"github.com/goccy/go-yaml/ast"
	"github.com/goccy/go-yaml/parser"
)

func main() {
	source := "test: hello world #comment\n"

	file, err := parser.ParseBytes([]byte(source), parser.ParseComments)
	if err != nil {
		panic(err)
	}

	file.Docs[0].Body.(*ast.MappingValueNode).Value.(*ast.StringNode).Value = "test\nmultiline"
	fmt.Println(file)
}

Output

test: |-
        test
        multiline

Expected

test: |- #comment
        test
        multiline

@codecov-commenter
Copy link

Codecov Report

Merging #296 (5856918) into master (a353932) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master     #296   +/-   ##
=======================================
  Coverage   75.90%   75.90%           
=======================================
  Files          13       13           
  Lines        4300     4300           
=======================================
  Hits         3264     3264           
  Misses        799      799           
  Partials      237      237           

Copy link
Owner

@goccy goccy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add the test cases in parser/parser_test.go for this fixing ?

@goccy goccy added the reviewed label Mar 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants