Skip to content

Conversation

@julio-lopez
Copy link
Collaborator

Replaces ineffective omitempty JSON tag in various fields of type

  • time.Time
  • time.Duration
  • struct

The original intent was to avoid serializing these fields when they were "empty", however omitempty has no effect on fields that are structs.

@julio-lopez julio-lopez requested a review from Copilot October 24, 2025 05:07
Copy link

Copilot AI left a 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 multiple files by replacing omitempty with omitzero for fields where omitempty was ineffective. The change targets struct-typed fields (object.ID, StorageUsageDetails, epoch.Parameters) and time-related types (time.Time, jsonencoding.Duration) where the zero value needs explicit handling during JSON serialization.

Key Changes:

  • Replaced omitempty with omitzero for struct fields and time types where zero-value omission is intended
  • Removed omitempty entirely for struct fields where serialization should always occur

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
snapshot/manifest.go Updated JSON tags for ObjectID (struct) and storage stat fields to remove ineffective omitempty
repo/object/indirect.go Removed omitempty from Object field (struct type)
repo/format/upgrade_lock_intent.go Changed CreationTime from omitempty to omitzero for proper time.Time handling
repo/format/content_format.go Changed EpochParameters from omitempty to omitzero for struct field
repo/blob/rclone/rclone_options.go Changed StartupTimeout from omitempty to omitzero for Duration type

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@codecov
Copy link

codecov bot commented Oct 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.94%. Comparing base (cb455c6) to head (8247f85).
⚠️ Report is 700 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4907      +/-   ##
==========================================
+ Coverage   75.86%   77.94%   +2.07%     
==========================================
  Files         470      537      +67     
  Lines       37301    31205    -6096     
==========================================
- Hits        28299    24323    -3976     
+ Misses       7071     4838    -2233     
- Partials     1931     2044     +113     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

The `omitempty` JSON tag is ineffective for fields of 
type `Time` and `Duration`.
Also, this struct is only used during the "upgrade" protocol, and it is OK to explicitly serialize 0 values, as it was being done.
@julio-lopez julio-lopez marked this pull request as ready for review October 24, 2025 05:59
@julio-lopez julio-lopez merged commit 5bc467e into kopia:master Oct 24, 2025
27 checks passed
@julio-lopez julio-lopez deleted the refactor/omitzero branch October 24, 2025 05:59
julio-lopez added a commit that referenced this pull request Nov 13, 2025
The `omitempty` JSON tag is ineffective on fields of struct type, among others.

The fields with tags changed from `omitempty` to `omitzero` can be classified into 2 categories:
- fields for which the intent was to avoid serializing the value when it was zero or empty (such as structs with the zero value for the struct);
- policy-definition fields that have the `omitempty` to match the corresponding policy fields.

The fields are changed such that
- the fields are not serialized when they have the zero (or empty) value; and
- consistency is maintained between policy fields and the corresponding policy definition fields.

---

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.Policy` and `policy.Definition` structs.
In these cases, 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 omitted fields.

**Fields of pointer types**:
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 are no defined `IsZero()` function for these pointers, so
the semantics are preserved in this case.

The `omitzero` JSON tag in the fields definition structs, such as the `ActionPolicyDefinition struct`, does not change the semantics, it simply makes the marshaled representation more compact.

**Fields of type slice**:
The behavior for `omitempty` and `omitzero` differs for slices, and maps as well.

The struct fields of slice type, such as `[]string`, are left with the `omitempty` tag 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, the `omitempty` tag is left unmodified out of abundance of caution.

---

Ref:

- #4973
- #4907
julio-lopez added a commit to julio-lopez/kopia that referenced this pull request Nov 25, 2025
julio-lopez added a commit that referenced this pull request Nov 25, 2025
- Revert "chore(ci): enable modernize:omitzero linter setting (#4981)"
   reverts 06845c7
- Revert "refactor(general): omitzero JSON tag in policy structs (#4910)"
  reverts 033b4b1
- Revert "refactor(general): `omitzero` JSON tag (#4907)"
   reverts 5bc467e
- Ref: #5006
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.

1 participant