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

feature: unmarshal with defaults and required fields #321

Closed
thnk2wn opened this issue Sep 20, 2024 · 4 comments
Closed

feature: unmarshal with defaults and required fields #321

thnk2wn opened this issue Sep 20, 2024 · 4 comments

Comments

@thnk2wn
Copy link

thnk2wn commented Sep 20, 2024

It'd be great to have required and default attribute support when unmarshalling koanf config into structs.

While it might not be the primary focus of koanf, I think that unmarshalling config into structs is the primary way most want to reference the config values koanf holds.

Ensuring required configuration items are set and providing reasonable defaults when they're not is very common and we're used to the support from modules like kelseyhightower/envconfig. Without that there's a lot more tedious work to do before and after koanf loading.

I took a quick stab at merging this functionality from kelseyhightower/envconfig into a new unmarshal method in our forked repo. While it's only been quickly tested, the envconfig source is rock solid and I minimized changes for quick start. The performance could probably be improved. At any rate, it's here for reference if it's helpful for the feature intent and/or potential future implementation.

teamjobot/koanf/pull/2

@qianlongzt
Copy link
Contributor

Try using https://github.com/go-playground/validator for validation.

Use go value for defaults

var config *Config = &Config{
    Port: "8989",
}

type Config struct {
    Port string 
}

k.UnmarshalWithConf("", config, koanf.UnmarshalConf{Tag: "tag"})

@thnk2wn
Copy link
Author

thnk2wn commented Sep 30, 2024

@qianlongzt Thanks. I'm not sure that defaults approach works when doing things like generics and dynamically creating. Validator probably works for validation though it's probably overkill and one extra step and dependency.

@OhohLeo
Copy link

OhohLeo commented Oct 2, 2024

Hello @thnk2wn, Do you know when next release is planned ?
This default & required feature would be greatly appreciated!
Thanks.

@thnk2wn
Copy link
Author

thnk2wn commented Oct 9, 2024

@OhohLeo I'm not a maintainer of this project, was just trying to help a bit with one issue and this question.

I believe the position for this project is as @qianlongzt mentioned above

You could certainly try the defaults and required behavior I added using my forked version at https://github.com/teamjobot/koanf but I'm not sure if we'll maintain that yet so it may grow stale or you might run into bugs with the changes.

@thnk2wn thnk2wn closed this as not planned Won't fix, can't repro, duplicate, stale Nov 12, 2024
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

3 participants