go-yaml icon indicating copy to clipboard operation
go-yaml copied to clipboard

Encode `ast.StringNode` line comment after header

Open gabe565 opened this issue 2 years ago • 1 comments

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 https://github.com/goccy/go-yaml/pull/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

gabe565 avatar May 18 '22 23:05 gabe565

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           

codecov-commenter avatar Jun 06 '22 04:06 codecov-commenter