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

Embedded struct with prefix does not work well with kong.ConfigFlag #181

Open
midnightexigent opened this issue Jun 29, 2021 · 1 comment

Comments

@midnightexigent
Copy link

midnightexigent commented Jun 29, 2021

The following examples:

package main

import (
        "fmt"

        "github.com/alecthomas/kong"
)

func main() {
        var cli1 struct {
                Module struct {
                        ConfigFile kong.ConfigFlag
                        Val1       int
                        Val2       int
                } `embed:"" prefix:"module."`
                // other params
        }

        kong.Parse(&cli1, kong.Configuration(kong.JSON))

        fmt.Println(cli1)

        var cli2 struct {
                Module struct {
                        ConfigFile kong.ConfigFlag
                        Module     struct {
                                Val1 int
                                Val2 int
                        } `embed:"" prefix:"module."`
                } `embed:"" prefix:"module."`
                // other params
        }
        kong.Parse(&cli2, kong.Configuration(kong.JSON))

        fmt.Println(cli2)
}

With a config file that looks like

{
    "val1": 1,
    "val2": 2
}

When run with the command
go run main.go --module.config-file ./config.json

Will output:

{{./config.json 0 0}}
{{./config.json {0 0}}}

Am I doing something wrong ? Or this is a bug/side effect ?

@alecthomas
Copy link
Owner

alecthomas commented Jun 29, 2021

I think you might be expecting Module.ConfigFile to only load config for Module, but that's not how it works. Config file keys are global in all existing configuration loaders, not scoped to sub-structures, so the correct keys would be module.val1 etc.

Because they're global it also doesn't help to have ConfigFile underneath Module.

I think you could probably achieve what you want by writing your own custom resolver.

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