koanf icon indicating copy to clipboard operation
koanf copied to clipboard

No pointer support

Open jxsl13 opened this issue 2 years ago • 6 comments

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.

jxsl13 avatar Nov 18 '22 17:11 jxsl13

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.

knadh avatar Nov 20 '22 06:11 knadh

I do define it as string pointer in the config (RootConfig) struct to indicate that it's optional.

jxsl13 avatar Nov 20 '22 09:11 jxsl13

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)

jxsl13 avatar Nov 20 '22 09:11 jxsl13

Well, in general there is no pointer support. This is more of an enhencement requirement than a bug label, I guess.

jxsl13 avatar Nov 22 '22 16:11 jxsl13

I wonder if this can be implemented with custom mapstructure hook function where for example time.Time is parsed as well.

jxsl13 avatar Feb 06 '23 21:02 jxsl13

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.

knadh avatar Jan 10 '24 17:01 knadh

Closing this as I am not in a position to consider adding this functionality to the underlying lib.

knadh avatar Jul 01 '24 12:07 knadh