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

Passing a double pointer to decoder.Decode has unexpected result #619

Open
zfLQ2qx2 opened this issue Jan 16, 2025 · 0 comments
Open

Passing a double pointer to decoder.Decode has unexpected result #619

zfLQ2qx2 opened this issue Jan 16, 2025 · 0 comments

Comments

@zfLQ2qx2
Copy link

I am migrating from gopkg.in/yaml.v2 to github.com/goccy/go-yaml and I've found an odd difference in behavior:

    inFile, err := os.Open("config_goccy.yml")
    if err != nil {
        log.Fatalf("Can't open config_goccy.yml: %v", err)
    }
    defer inFile.Close()

    var roundTrippedConfig AppConfig

    decoder := gyaml.NewDecoder(inFile)
    if err := decoder.Decode(&roundTrippedConfig); err != nil {
        log.Fatalf("Unable to unmarshal data from config_goccy.yml: %v", err)
    }

    log.Printf("Decoded from config_goccy.yml: %+v", roundTrippedConfig)

This code works as written. However if I change "var roundTrippedConfig AppConfig" to "var roundTrippedConfig *AppConfig" then after decoder.Decode runs the value of *AppConfig is nil - this also differs from the behavior of encoding/json. This only seems to be an issue with the top most structure - I have a number of sub types that are pointers to structures or pointers to arrays of pointers.

I tried adding a "roundTrippedConfig = new(AppConfig)" before the decoder.Decode and that generated more errors, but if I change &roundTrippedConfig to roundTrippedConfig in that case it does work. So the issue seems to be if decode.Decoder is passed a pointer to a pointer (**AppConfig). So I see what the workaround is but if go-yaml accepts a double pointer then it would be a drop-in replacement.

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