koanf
koanf copied to clipboard
No pointer support
Hi,
I'm trying to integrate cobra and koanf in a way that I can provide flags, env variables or an optional .env config file path in such a way that there is a hierarchy between those three input variants.
I'm trying to have a struct and struct tags which map to (dot)env variables as well as to flag names. Pointer types in the struct are supposed to be somewhat optional but they seem to create some weird crash that I cannot exactly grasp.
I have created a minimal example of the problem and a launch.json for vs code: https://github.com/jxsl13/koanf-test
Starting this program instantly crashes. Maybe my approach with pointer values is incorrect and there might be a better approach.
Hi @jxsl13. k.Get("config")
is *string
here where as it should ideally be string
. The panic is happening when mitchellh/copystructure tries to make a deep copy of this reference. Any idea why it's a pointer to a string that's being loaded into the conf map?
Will see if koanf can detect this and avoid it internally.
I do define it as string pointer in the config (RootConfig) struct to indicate that it's optional.
this might be my bad :). Guess this should be able to check both so I should probably use Get and check if it's a string pointer or a string.
Initially I do load the default values from the struct pointer passed to the RegisterFlags function, so by default it assumes that this is a string pointer. (line 88 in koanf.go)
Well, in general there is no pointer support. This is more of an enhencement requirement than a bug label, I guess.
I wonder if this can be implemented with custom mapstructure hook function where for example time.Time is parsed as well.
https://github.com/knadh/koanf/issues/262 Perhaps this newly merged fork has support for this? I can see that a bunch of pending PRs from the old repo have been merged on the fork.
Closing this as I am not in a position to consider adding this functionality to the underlying lib.