-
-
Notifications
You must be signed in to change notification settings - Fork 72
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
ISSUE-210 - Add Required property to indicate a config registration c… #211
Conversation
…an't be empty or nil (+ tests)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look like a breaking change to me if we drop the "empty" validation. It would only be breaking for codebases not always using keyed struct fields, which is very rare and usually not recommended anyway. The behavior for entries without Required: true
stays the same as before.
I would prefer not adding NewEntry()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very minor adjustments to make, otherwise LGTM! 👍🏻
config/entry.go
Outdated
if err := e.tryEnvVarConversion(key); err != nil { | ||
return err | ||
} | ||
|
||
v := reflect.ValueOf(e.Value) | ||
if e.Required && (!v.IsValid() || e.Value == nil) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if e.Required && (!v.IsValid() || e.Value == nil) { | |
if e.Required && (!v.IsValid() || v.IsNil()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IsNil
reports whether its argument v is nil. The argument must be a chan, func, interface, map, pointer, or slice value; if it is not, IsNil panics.
I've tested this when you first suggested it, but it will not work if the value is not one of the above. This happens e.g. when an inputted value is actually nil
.
So !v.IsValid() || e.Value == nil
would cover the functionality we want.
If it's not a value (IsValid
) it will return true. If it is, it will check for explicit nil
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. Here is what I suggest to cover the scenario I showed in the playground link earlier.
if e.Required && (!v.IsValid() || e.Value == nil) { | |
if e.Required && (!v.IsValid() || e.Value == nil || (v.Kind() == reflect.Pointer && v.IsNil())) { |
Pull Request Test Coverage Report for Build 9608948098Details
💛 - Coveralls |
Pull Request Test Coverage Report for Build 9610067852Details
💛 - Coveralls |
Pull Request Test Coverage Report for Build 9610325654Details
💛 - Coveralls |
Thank you very much! 🎉 |
Add Required property to indicate a config registration can't be omitted or nil (+ tests)
References
Issue(s): #210
Description
Changes in this PR make it possible to, when registering a config value, to indicate that the value can not be
nil
and also validates omitting the complete config line. Both will trigger a validation message.Possible drawbacks/suggestions
Imo we should provide a 'constructor' like
config.NewEntry()
in which we can set default values to prevent this change to theEntry
struct to be a breaking change for people not using keyed initialization on structs. This was discussed and marked as not breaking.Proposal for constructor
NewEntry()
: