Skip to content

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

Open
@zfLQ2qx2

Description

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.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions