Repository navigation
Add MarshalJSON to xtime.Duration - #242
Conversation
Duration has UnmarshalJSON that expects a human-readable string (it
strips the surrounding quotes and calls ParseDuration), but no
MarshalJSON. So json.Marshal emits the raw int64 nanoseconds, which
UnmarshalJSON cannot read back:
{"a":1000000000,"dur":0,"durationPointer":604801000000000}
missing unit in duration "00000000"
The type is documented as supporting YAML and JSON. #136 added
MarshalYAML to match the existing UnmarshalYAML; this does the same
for JSON, emitting the human-readable string that UnmarshalJSON reads.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughAdds a ChangesDuration JSON Marshaling
Estimated code review effort: 1 (Trivial) | ~5 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Cherry-pick of minio#242 (11e724c). Duration had UnmarshalJSON but no MarshalJSON, so it encoded as a bare int64 of nanoseconds while the decoder unconditionally strips the first and last byte expecting a quoted string. The round trip was broken in both directions: json.Unmarshal on 5400000000000 fails with `missing unit in duration "40000000000"`. Encoding as time.Duration's own string form makes the pair symmetric. Nothing in minio encodes these fields as JSON today -- batch job definitions persist as YAML, DescribeBatchJob answers YAML, and the msgp path is int64 -- but cmd/batch-expire.go and cmd/batch-replicate.go do carry `json:` tags on xtime.Duration fields, so the trap is one refactor away from being live. This changes the JSON wire form. Anything that already persisted these fields as numbers will not read back; nothing found in minio or mc does. (cherry picked from commit 96870e988f011b0075383948cf178d0512beacdf)
Duration has an UnmarshalJSON that expects a human-readable string (it strips the surrounding quotes and calls ParseDuration), but no MarshalJSON. json.Marshal therefore emits the raw int64 nanoseconds, which UnmarshalJSON cannot read back:
The type is documented as supporting YAML and JSON. #136 added MarshalYAML to match the existing UnmarshalYAML; this does the same for JSON, emitting the human-readable string that UnmarshalJSON reads.
Tested with go test -race ./xtime/ (the existing TestDuration_Marshal is extended to cover the JSON marshal output and a round-trip). gofmt, go vet, and golangci-lint are clean.
Summary by CodeRabbit