-
Notifications
You must be signed in to change notification settings - Fork 26
Feature allow unset pointer #80
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 allow unset pointer #80
Conversation
if !(d.config.AllowUnsetPointer && fieldValue.Kind() == reflect.Ptr) { | ||
targetValKeysUnused[fieldName] = struct{}{} | ||
} |
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.
I think it would be more elegant to continue here and leave setting the map after the condition.
WDYT?
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.
Hi! Did you mean like this?
if !rawMapVal.IsValid() {
// There was no matching key in the map for the value in
// the struct. Remember it for potential errors and metadata.
if !(d.config.AllowUnsetPointer && fieldValue.Kind() == reflect.Ptr) {
targetValKeysUnused[fieldName] = struct{}{}
continue
}
continue
}
If yes, I am fine with this
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.
I think the correct implementation would be:
if !rawMapVal.IsValid() {
if d.config.AllowUnsetPointer && fieldValue.Kind() == reflect.Ptr {
continue
}
// There was no matching key in the map for the value in
// the struct. Remember it for potential errors and metadata.
targetValKeysUnused[fieldName] = struct{}{}
continue
}
Or did I misread what it does?
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.
Your implementation did the thing. However, if we set AllowUnsetPointer == true and use "continue" we won't add the fieldName to the set and it seems to be formally incorrect, because there is still no "matching key in the map for the value in the struct".
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.
@sagikazarmark What do you think?
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.
@sagikazarmark could you please have a look?
Description
This pull request adds a config flag
AllowUnsetPointer
to allow pointer fields in structs to be treated as optional during decoding. When enabled, missing pointer fields will not be reported as unset even ifErrorUnset
istrue
.This is useful when decoding into structs with optional pointer fields, while still maintaining strict handling (
ErrorUnset
) for required non-pointer fields.Current behavior example
New behavior with AllowUnsetPointer: true