-
Notifications
You must be signed in to change notification settings - Fork 11
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
16 - excluding known typed fields from returned map #20
16 - excluding known typed fields from returned map #20
Conversation
…d_map # Conflicts: # unmarshal_from_json_map_test.go # unmarshal_test.go
options.go
Outdated
skipPopulateStruct bool | ||
mode Mode | ||
skipPopulateStruct bool | ||
excludeKnownTypesFromMap bool |
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'd call it excludeKnownFieldsFromMap
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.
and then also align this func
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.
👍 changed in all places excludeKnownTypes
to excludeKnownFields
example_test.go
Outdated
@@ -53,13 +53,22 @@ func ExampleUnmarshal() { | |||
result, err = marshmallow.Unmarshal([]byte(`{"foo":2,"boo":[1,2,3]}`), &v, | |||
marshmallow.WithMode(marshmallow.ModeFailOverToOriginalValue)) | |||
fmt.Printf("ModeFailOverToOriginalValue and invalid value: result=%+v, err=%T\n", result, err) | |||
|
|||
// unmarshal with mode marshmallow.ModeExcludeKnownFieldsFromReturnedMap and valid value | |||
// this will return unmarshalled result excludes the typed fields |
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.
what do you think about "unmarshalled result that contains unknown fields only"
or "unmarshalled result without known fields"
unmarshal_from_json_map_test.go
Outdated
|
||
t.Run("test_exclude_known_fields_from_map", func(t *testing.T) { | ||
p := Person{} | ||
result, err := UnmarshalFromJSONMap(map[string]interface{}{"firstName": "string_firstName", "lastName": "string_LastName", "field_to_exclude": "untyped"}, &p, WithExcludeKnownFieldsFromMap(true)) |
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.
"field_to_exclude" here is not excluded because it is not known, so it is rather "field_not_excluded". but maybe it is better to name it "unknown":"string_unknown" to align to the known/unknown fields terminology and examples
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.
Agree
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.
right, fixed the naming
@@ -44,11 +44,20 @@ func WithSkipPopulateStruct(skipPopulateStruct bool) UnmarshalOption { | |||
} | |||
} | |||
|
|||
// WithExcludeKnownFieldsFromMap is an UnmarshalOption function to set the excludeKnownFieldsFromMap option. | |||
// Exclude known fields flag is set to false by default. | |||
func WithExcludeKnownFieldsFromMap(excludeKnownFields bool) UnmarshalOption { |
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.
Maybe add a description of what the flag does despite the fact that it's pretty send explainatory?
Something like: when this flag is set to true, fields specified in the input struct will be omitted from the result map
unmarshal.go
Outdated
@@ -82,7 +82,7 @@ func (d *decoder) populateStruct(structInstance interface{}, result map[string]i | |||
field := refInfo.field(structValue) | |||
assignValue(field, value) | |||
} | |||
if result != nil { | |||
if result != nil && !d.options.excludeKnownFieldsFromMap { |
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.
Changing the logic of the if statement will also affect the else logic. To prevent it I'd either move the new condition inside the if block or reverse the if else order
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.
fixed. moved the condition inside the if block
unmarshal_from_json_map.go
Outdated
@@ -80,7 +80,7 @@ func (m *mapDecoder) populateStruct(path []string, data map[string]interface{}, | |||
field := refInfo.field(structValue) | |||
assignValue(field, value) | |||
} | |||
if result != nil { | |||
if result != nil && !m.options.excludeKnownFieldsFromMap { |
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.
Same comment as the if-else block above
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.
fixed. moved the condition inside the if block
unmarshal_test.go
Outdated
|
||
t.Run("test_exclude_known_fields_from_map", func(t *testing.T) { | ||
p := Person{} | ||
result, err := Unmarshal([]byte(`{"firstName": "string_firstName", "lastName": "string_lastName", "field_to_exclude": "untyped"}`), &p, WithExcludeKnownFieldsFromMap(true)) |
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.
Same here
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.
right, fixed the naming
unmarshal_test.go
Outdated
|
||
t.Run("test_exclude_known_fields_from_map", func(t *testing.T) { | ||
p := Person{} | ||
result, err := Unmarshal([]byte(`{"firstName": "string_firstName", "lastName": "string_lastName", "field_to_exclude": "untyped"}`), &p, WithExcludeKnownFieldsFromMap(true)) |
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.
same thing, i suggest rename field_to_exclude to "unknown":"string_unknown"
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.
fixed the naming
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.
LGTM, just some naming suggestions
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.
We must fix the if-else logic before merging
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.
Awesome 💪
Adding
WithExcludeKnownFieldsFromMap
flag toUnmarshal
andUnmarshalFromJSONMap
functions