-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Make features.BindStruct a Variable or an Option, Instead of Build Constraints #1851
Comments
👋 Thanks for reporting! A maintainer will take a look at your issue shortly. 👀 In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues. ⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9 📣 If you've already given us your feedback, you can still help by spreading the news, https://twitter.com/sagikazarmark/status/1306904078967074816 Thank you! ❤️ |
How do you feel about #1854? I'd imagine it would become the default behavior at some point, so the experimental flag would become noop. |
Thank you for reviewing this quickly! I looked at the PR changes, it looks mostly OK, but I guess the build tag will remain to control the behavior of the global viper instance? I think it's best not to let users depend on custom build tags, even if they're experimental. So I think it would be more natural if the global and custom viper instances had similar ways to configure the behavior. Lines 1404 to 1410 in 3266e43
so how about: func main() {
// Enable for the global instance
viper.ExperimentalBindStruct()
// Enable for an instance
v := viper.New()
v.ExperimentalBindStruct()
} Perhaps we can change all existing option names into something along |
The build tag controls the default behavior. Incidentally, you are right: it also controls the global Viper behavior. I try to discourage everyone from using the global Viper, though. I don't think it's good practice to use the global instance. I'm also eliminating the state modification pattern: all new options should be passed through Let me think about that. Is the global instance something you use? If so, why? Would it be difficult to migrate to a non-global instance? |
Yes, I do use global instance of viper to build some applications.
I agree on that, but removing the current package-level methods and global instance would be too much of a change. Perhaps we can achieve that in v2. |
Yes, that's a goal for v2. What do you think about #1856 ? |
I see, I think it's looking good as an option for configuring the global instance for v1. |
Copied from #1854 (comment):
|
Preflight Checklist
Problem Description
The features.BindStruct feature flag (which is also the only one feature flag in this package) is currently implemented as a build constraint here:
viper/internal/features/bind_struct.go
Lines 1 to 5 in 272344e
Being implemented as a build constraint, users have to add a whole new
viper_bind_struct
build tag to their build scripts, in order to enable this feature.This was quite surprising (to me, at least), and I think it's making harder for people to use this feature.
Proposed Solution
Option 1: Make this feature flag a dynamically configurable variable instead of a const regulated by build constraints, and move the
features
package outside of theinternal
package.This way, users of viper can dynamically enable this feature from normal golang source code, like as follows:
Option 2: Make this to an option that you can pass to
viper.NewWithOptions
, or something that you can configure later with viper instances.I believe this was the intention of this original TODO comment here:
viper/viper.go
Line 979 in 272344e
or something similar to
AutomaticEnv()
usage:Alternatives Considered
No response
Additional Information
Related issues and PRs:
The text was updated successfully, but these errors were encountered: