Last active
July 4, 2025 06:13
-
-
Save manuelarte/6a68d270174edea452f4f8d07d69c17d to your computer and use it in GitHub Desktop.
Round trip yaml not honoring space distance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ISSUE: https://github.com/goccy/go-yaml/issues/772 | |
package main | |
import ( | |
"fmt" | |
"github.com/goccy/go-yaml" | |
"log" | |
) | |
func main() { | |
input := []byte(` | |
version: 2 | |
key: | |
prop: value0 # double space between value and comment | |
list: | |
- hello # hello | |
- bye # bye | |
`) | |
var fields yaml.MapSlice | |
cm := make(map[string][]*yaml.Comment) | |
err := yaml.UnmarshalWithOptions(input, &fields, yaml.UseOrderedMap(), yaml.CommentToMap(cm)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
output, err := yaml.MarshalWithOptions(fields, yaml.IndentSequence(true), yaml.WithComment(cm)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(string(output)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment