-
Notifications
You must be signed in to change notification settings - Fork 594
refactor(general): omitzero JSON tag in policy structs
#4910
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
refactor(general): omitzero JSON tag in policy structs
#4910
Conversation
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.
Pull Request Overview
This PR refactors JSON struct tags across policy definition structs by replacing omitempty with omitzero. This change affects how zero-valued fields are serialized in JSON output, ensuring that zero values of structs are omitted rather than just nil/empty values.
Key Changes:
- Updated all
*PolicyDefinitionstruct JSON tags fromomitemptytoomitzero - Updated nested policy fields in the main
PolicyandDefinitionstructs
Reviewed Changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| snapshot/policy/upload_policy.go | Updated JSON tags in UploadPolicyDefinition struct |
| snapshot/policy/splitter_policy.go | Updated JSON tags in SplitterPolicyDefinition struct |
| snapshot/policy/scheduling_policy.go | Updated JSON tags in SchedulingPolicyDefinition struct |
| snapshot/policy/retention_policy.go | Updated JSON tags in RetentionPolicyDefinition struct |
| snapshot/policy/policy.go | Updated JSON tags in Policy and Definition structs |
| snapshot/policy/os_snapshot_policy.go | Updated JSON tags in OSSnapshotPolicyDefinition and VolumeShadowCopyPolicyDefinition structs |
| snapshot/policy/logging_policy.go | Updated JSON tags in LoggingPolicy, LoggingPolicyDefinition, DirLoggingPolicyDefinition, and EntryLoggingPolicyDefinition structs |
| snapshot/policy/files_policy.go | Updated JSON tags in FilesPolicyDefinition struct |
| snapshot/policy/error_handling_policy.go | Updated JSON tags in ErrorHandlingPolicyDefinition struct |
| snapshot/policy/compression_policy.go | Updated JSON tags in CompressionPolicyDefinition and MetadataCompressionPolicyDefinition structs |
| snapshot/policy/actions_policy.go | Updated JSON tags in ActionsPolicyDefinition struct |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
fb3fbe4 to
bd0fe1e
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4910 +/- ##
==========================================
+ Coverage 75.86% 78.03% +2.16%
==========================================
Files 470 548 +78
Lines 37301 31412 -5889
==========================================
- Hits 28299 24512 -3787
+ Misses 7071 4851 -2220
- Partials 1931 2049 +118 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Pull Request Overview
Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
cf22a99 to
653a1e7
Compare
The fields in `policy.Policy` and `policy.Definition` are of type `struct`. The `omitempty` JSON tag is ineffective for structs. The `omitzero` JSON tag avoids marshaling empty fields, making the serialized representation more compact and less noisy, while preserving the same behavior and thus semantic when unmarshaling ommitted fields.
The `omitempty` and `omitzero` have practically the same effect on fields of pointer types: - the field is omitted when it is null - the field is included when it is not null, even if the value that it points to is "the zero value" for the non-pointer type. Note: when the pointer type defines an `IsZero` member function, then that field would also be omitted during marshaling. There is no defined `IsZero` function for these pointers, so the sematics are preserved in this case. The `omitzero` JSON tag in the fields in `ActionPolicyDefinition` does not change the semantics, it simply makes the marshaled representation more compact.
The behavior for `omitempty` and `omitzero` differs for slices, and maps as well. The struct fields of slice type (e.g., []string) are tagged with `omitempty` to be able to tell the difference between a nil slice and a non-nil zero-length slice.
The behavior for `omitempty` and `omitzero` differs for slices, and maps as well. The struct fields of slice type (e.g., []string) are tagged with `omitempty` to be able to tell the difference between a nil slice and a non-nil zero-length slice.
653a1e7 to
581bd2a
Compare
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.
Pull Request Overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.
omitzero JSON tag in policy structs
The
omitemptyJSON tag is ineffective on fields of struct type, among others.The fields with tags changed from
omitemptytoomitzerocan be classified into 2 categories:omitemptyto match the corresponding policy fields.The fields are changed such that
In the context of policy definitions:
Fields of type
struct:In some cases, the fields in the policy definition and values are of type struct, such as is the case for the
policy.Policyandpolicy.Definitionstructs.In these cases, the
omitzeroJSON tag avoids marshaling empty fields, making the serialized representation more compact and less noisy, while preserving the same behavior and thus semantic when unmarshaling omitted fields.Fields of pointer types:
The
omitemptyandomitzerohave practically the same effect on fields of pointer types:Note: when the pointer type defines an
IsZero()member function, then that field would also be omitted during marshaling. There are no definedIsZero()function for these pointers, sothe semantics are preserved in this case.
The
omitzeroJSON tag in the fields definition structs, such as theActionPolicyDefinition struct, does not change the semantics, it simply makes the marshaled representation more compact.Fields of type slice:
The behavior for
omitemptyandomitzerodiffers for slices, and maps as well.The struct fields of slice type, such as
[]string, are left with theomitemptytag to be able to tell the difference between a nil slice and a non-nil, zero-length slice. Even though, currently most code paths do not explicitly differentiate between a nil slice and an empty slice, theomitemptytag is left unmodified out of abundance of caution.Ref:
omitzeroJSON tag #4907