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

Support root level map keys in map sources #2037

Open
3 tasks done
lukasbindreiter opened this issue Jan 2, 2025 · 0 comments
Open
3 tasks done

Support root level map keys in map sources #2037

lukasbindreiter opened this issue Jan 2, 2025 · 0 comments
Labels
area/v3 relates to / is being considered for v3 kind/bug describes or fixes a bug status/triage maintainers still need to look into this

Comments

@lukasbindreiter
Copy link

Checklist

  • Are you running the latest v3 release? The list of releases is here.
  • Did you check the manual for your release? The v3 manual is here.
  • Did you perform a search about this feature? Here's the GitHub guide about searching.

What problem does this solve?

This issue is related to this one I opened here: urfave/cli-altsrc#14

I noticed the same issue of root keys not being considered also occurs in urfave/cli itself:

Example:

mapSource := cli.NewMapSource("config", map[any]any{
	"host": "localhost",
	"database": map[any]any{
		"url": "example.com",
	},
})

cmd := &cli.Command{
	Name: "Example",
	Action: func(ctx context.Context, cmd *cli.Command) error {
		fmt.Println("Host:")
		fmt.Println(cmd.String("host"))
		fmt.Println("Database URL:")
		fmt.Println(cmd.String("database-url"))
		return nil
	},
	EnableShellCompletion: true,
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "host",
			Sources: cli.NewValueSourceChain(cli.NewMapValueSource("host", mapSource)),
		},
		&cli.StringFlag{
			Name:    "database-url",
			Sources: cli.NewValueSourceChain(cli.NewMapValueSource("database.url", mapSource)),
		},
	},
}

cmd.Run(context.Background(), os.Args)

will output (host is empty):

Host:

Database URL:
example.com

Solution description

I want the above snipped to set host to localhost and database-url to example.com, instead of only setting the database url.

The issue is located here - because it always returns nil if there is no dot in a key:
https://github.com/urfave/cli/blob/main/value_source.go#L196

@lukasbindreiter lukasbindreiter added area/v2 relates to / is being considered for v2 kind/bug describes or fixes a bug status/triage maintainers still need to look into this labels Jan 2, 2025
@dearchap dearchap added area/v3 relates to / is being considered for v3 and removed area/v2 relates to / is being considered for v2 labels Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v3 relates to / is being considered for v3 kind/bug describes or fixes a bug status/triage maintainers still need to look into this
Projects
None yet
Development

No branches or pull requests

2 participants