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

Comment Map: Only parses comments on the first key of each map #345

Open
coxley opened this issue Feb 20, 2023 · 2 comments
Open

Comment Map: Only parses comments on the first key of each map #345

coxley opened this issue Feb 20, 2023 · 2 comments

Comments

@coxley
Copy link

coxley commented Feb 20, 2023

It's entirely possible that I'm using this incorrectly, but it seems that yaml.CommentToMap(cm) only retrieves comments if they're on the first key for each map. Should I be decoding to something other than a MapSlice?

package main

import (
	"bytes"
	"fmt"
	"io"

	"github.com/goccy/go-yaml"
)

var contents = []byte(`
top:
  # This comment is captured
  first: true
  # But not this one
  second:
	# But this one is
    first: true
    # But not this one
    second: true
    # Neither this
    third: true
`)

func main() {
	cm := yaml.CommentMap{}
	dec := yaml.NewDecoder(bytes.NewReader(contents), yaml.UseOrderedMap(), yaml.CommentToMap(cm))

	for {
		var mslice yaml.MapSlice
		err := dec.Decode(&mslice)
		if err == io.EOF {
			break
		}

		if err != nil {
			panic(err)
		}
	}

	for _, comment := range cm {
		fmt.Println(comment.Texts)
	}
}

// ➜ go run main.go
// [ But this one is]
// [ This comment is captured]

I need to edit some values in hundreds of files, preserving as much as I can. This is blocking that. 😅

@coxley
Copy link
Author

coxley commented Feb 20, 2023

Looks like it's the combination of UseOrderedMap and CommentToMap that causes it. Works fine without preserving ordering...

coxley added a commit to coxley/go-yaml that referenced this issue Feb 20, 2023
OrderedMap values aren't recording their comments like normal maps.
@coxley
Copy link
Author

coxley commented Feb 20, 2023

Submitted #346 to address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant