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

16 - excluding known typed fields from returned map #20

Merged
merged 7 commits into from
Aug 31, 2022

Conversation

galepx
Copy link
Contributor

@galepx galepx commented Aug 29, 2022

Adding WithExcludeKnownFieldsFromMap flag to Unmarshal and UnmarshalFromJSONMap functions

@avivpxi avivpxi linked an issue Aug 29, 2022 that may be closed by this pull request
options.go Outdated
skipPopulateStruct bool
mode Mode
skipPopulateStruct bool
excludeKnownTypesFromMap bool
Copy link
Collaborator

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?

Copy link
Collaborator

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

Copy link
Contributor Author

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

@galepx galepx marked this pull request as ready for review August 29, 2022 08:37
change 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
Copy link

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"


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))
Copy link

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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree

Copy link
Contributor Author

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 {
Copy link
Collaborator

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 {
Copy link
Collaborator

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

Copy link
Contributor Author

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

@@ -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 {
Copy link
Collaborator

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

Copy link
Contributor Author

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


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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, fixed the naming


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))
Copy link

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"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed the naming

Copy link

@itayPX itayPX left a 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

Copy link
Collaborator

@avivpxi avivpxi left a 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

@galepx galepx requested a review from avivpxi August 30, 2022 08:31
Copy link
Collaborator

@avivpxi avivpxi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome 💪

@galepx galepx merged commit 457669a into main Aug 31, 2022
@galepx galepx deleted the 16_Excluding_known_typed_fields_from_returned_map branch August 31, 2022 09:50
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

Successfully merging this pull request may close these issues.

Excluding known typed fields from returned map
3 participants