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

No pointer support #183

Closed
jxsl13 opened this issue Nov 18, 2022 · 7 comments
Closed

No pointer support #183

jxsl13 opened this issue Nov 18, 2022 · 7 comments
Labels
enhancement New feature or request

Comments

@jxsl13
Copy link
Contributor

jxsl13 commented Nov 18, 2022

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 jxsl13 added the bug Something isn't working label Nov 18, 2022
@knadh
Copy link
Owner

knadh commented Nov 20, 2022

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.

@jxsl13
Copy link
Contributor Author

jxsl13 commented Nov 20, 2022

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

@jxsl13
Copy link
Contributor Author

jxsl13 commented Nov 20, 2022

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
Copy link
Contributor Author

jxsl13 commented Nov 22, 2022

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

@jxsl13 jxsl13 changed the title Weird crash with pointers No pointer support Nov 22, 2022
@knadh knadh added enhancement New feature or request and removed bug Something isn't working labels Dec 6, 2022
@jxsl13
Copy link
Contributor Author

jxsl13 commented Feb 6, 2023

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

@knadh
Copy link
Owner

knadh commented Jan 10, 2024

#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
Copy link
Owner

knadh commented Jul 1, 2024

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

@knadh knadh closed this as completed Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants