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

Keys with underscores are not parsed correctly #12

Closed
kernel-sanders opened this issue Dec 12, 2023 · 2 comments
Closed

Keys with underscores are not parsed correctly #12

kernel-sanders opened this issue Dec 12, 2023 · 2 comments

Comments

@kernel-sanders
Copy link

This library does not parse keys with underscores correctly. The yaml.v3 library has no issue with them.

import (
    "log"
    "fmt"
    invopop "github.com/invopop/yaml"
    yamlv3 "gopkg.in/yaml.v3"
)

type Config struct {
    Normal             string `yaml:"normal"`
    OneUnderscore      string `yaml:"one_underscore"`
    TwoUnderscores     string `yaml:"two_under_scores"`
    OneUnderscoreInt   int    `yaml:"one_underscoreint"`
    TwoUnderscoresInt  int    `yaml:"two_under_scoresint"`
}

func main() {
    yamlData := `
---
normal: test
one_underscore: test1
two_under_scores: test2
one_underscoreint: 1
two_under_scoresint: 2
`
    var config Config
    err := invopop.Unmarshal([]byte(yamlData), &config)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("invopop - normal: %s\n", config.Normal)
    fmt.Printf("invopop - one_underscore: %s\n", config.OneUnderscore)
    fmt.Printf("invopop - two_under_scores: %s\n", config.TwoUnderscores)
    fmt.Printf("invopop - one_underscoreint: %d\n", config.OneUnderscoreInt)
    fmt.Printf("invopop - two_under_scoresint: %d\n", config.TwoUnderscoresInt)


    var config2 Config
    err = yamlv3.Unmarshal([]byte(yamlData), &config2)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("yamlv3 - normal: %s\n", config2.Normal)
    fmt.Printf("yamlv3 - one_underscore: %s\n", config2.OneUnderscore)
    fmt.Printf("yamlv3 - two_under_scores: %s\n", config2.TwoUnderscores)
    fmt.Printf("yamlv3 - one_underscoreint: %d\n", config2.OneUnderscoreInt)
    fmt.Printf("yamlv3 - two_under_scoresint: %d\n", config2.TwoUnderscoresInt)
}

Output:

invopop - normal: test
invopop - one_underscore:
invopop - two_under_scores:
invopop - one_underscoreint: 0
invopop - two_under_scoresint: 0
yamlv3 - normal: test
yamlv3 - one_underscore: test1
yamlv3 - two_under_scores: test2
yamlv3 - one_underscoreint: 1
yamlv3 - two_under_scoresint: 2
@mcblair
Copy link

mcblair commented Dec 29, 2023

Just ran into this myself. Turns out that invopop/yaml marshals to JSON first, which strips the YAML tags. Adding JSON tags fixes this — please let me know if this works for you @kernel-sanders.

This isn't ideal by any means.

@kernel-sanders
Copy link
Author

Yes, and apparently this is intended behavior. The Go yaml repo most people seem to use is https://github.com/kubernetes-sigs/yaml which stated as much: kubernetes-sigs/yaml#102 (comment)

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

2 participants