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

kong.JSON doesn't load camelCase values in embedded structs #489

Open
jraby opened this issue Jan 22, 2025 · 0 comments
Open

kong.JSON doesn't load camelCase values in embedded structs #489

jraby opened this issue Jan 22, 2025 · 0 comments

Comments

@jraby
Copy link

jraby commented Jan 22, 2025

Hi,

I haven't dug into this much, but I was seeing some strange behavior when trying to load values from a json file when using embedded structs.

for some reason, camelCase keys in the json file do not get picked up when they are used in embedded structs.

The following testcase shows the problem:


func TestConfigReallyLoadsValues(t *testing.T) {
	type Level struct {
		Flag      string `json:"flag" required:""`
		WithSnake string `json:"with_snake,omitempty" required:""`
		WithCamel string `json:"withCamel,omitempty" required:""`
	}
	var cli struct {
		TopSnake string `json:"top_snake" required:""`
		TopCamel string `json:"topCamel" required:""`
		Level    `json:"level" prefix:"level." embed:""`
	}

	cli.TopCamel = "filled"
	cli.TopSnake = "filled"
	cli.Level.WithCamel = "filled"
	cli.Level.WithSnake = "filled"
	cli.Level.Flag = "filled"
	conf, cleanConf := makeConfig(t, &cli)
	defer cleanConf()

	b, err := os.ReadFile(conf)
	assert.NoError(t, err)
	log.Printf("data: %s", b)

	p := mustNew(t, &cli, kong.Configuration(kong.JSON, conf))
	_, err = p.Parse(nil)
	assert.NoError(t, err)
}
=== RUN   TestConfigFlag
--- PASS: TestConfigFlag (0.00s)
=== RUN   TestConfigValidation
--- PASS: TestConfigValidation (0.00s)
=== RUN   TestConfigReallyLoadsValues
2025/01/21 22:51:11 data: {"top_snake":"filled","topCamel":"filled","level":{"flag":"filled","with_snake":"filled","withCamel":"filled"}}
    config_test.go:72: Did not expect an error but got:
        missing flags: --level.with-camel=STRING
--- FAIL: TestConfigReallyLoadsValues (0.00s)
FAIL
FAIL    github.com/alecthomas/kong      0.003s
FAIL

Top level keys expressed as snake or camel are properly parsed, but for the embedded struct, only the snake case version is picked up.

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