Add MarshalText to types.Date - #162
Open
jusabe-kreditz wants to merge 1 commit into
Open
jusabe-kreditz wants to merge 1 commit into
jusabe-kreditz wants to merge 1 commit into
Conversation
Date defines UnmarshalText, which parses the "2006-01-02" DateFormat, but
has no MarshalText, so it inherits time.Time's RFC 3339 one. Every codec
that pairs encoding.TextMarshaler with encoding.TextUnmarshaler therefore
writes a Date it cannot read back:
xml.Marshal -> <date>2019-04-01T00:00:00Z</date>
xml.Unmarshal -> parsing time "2019-04-01T00:00:00Z": extra text: "T00:00:00Z"
Format with DateFormat instead, matching MarshalJSON, String and
UnmarshalText, and matching the method set types.Duration already carries.
This also resolves the XML round trip that oapi-codegen#58 addressed with MarshalXML
and UnmarshalXML, without needing the encoding/xml import.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
jusabe-kreditz
force-pushed
the
fix-date-marshaltext
branch
from
September 14, 2026 12:32
94f494b to
0e08c91
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
types.DatedefinesUnmarshalText, which parses theDateFormatlayout (2006-01-02), but it defines noMarshalText. It therefore inheritstime.Time's, which emits RFC 3339. The two halves ofencoding.TextMarshaler/encoding.TextUnmarshalerdisagree, so any codec that pairs them writes aDateit cannot read back.encoding/xmlshows it with no third-party dependency:The same thing happens in any other text-based encoder. I hit it originally through a DynamoDB mapper (
guregu/dynamo), which selectsTextMarshalerto encode andTextUnmarshalerto decode: rows written with a date field could never be read back.Fix
Add
MarshalText, formatting withDateFormatso it agrees withMarshalJSON,StringandUnmarshalText. This is also the method settypes.Durationalready carries (MarshalJSON/UnmarshalJSON,MarshalText/UnmarshalText,Bind,String) —Datewas missing exactly one method from it.Relationship to #58
#58 fixed the XML symptom with
MarshalXML/UnmarshalXML(42 lines plus anencoding/xmlimport) and was closed by its author without review. FixingMarshalTextresolves that round trip at the root, in three lines, with no new import — and fixes every other text-based encoder at the same time. Tests here cover the XML case so that behaviour stays pinned.Compatibility
This is an observable behaviour change for anyone relying on
Dateemitting RFC 3339 through a text encoder. I'd argue that output was never usable, sinceUnmarshalTextrejects it, but flagging it explicitly so it's a deliberate call rather than a surprise. JSON output is unaffected.Testing
TestDate_MarshalText,TestDate_TextRoundTrip,TestDate_XMLRoundTripadded.go test ./...); no existing test depended on the RFC 3339 text output.golangci-lintv2.12.2 (the version CI pins) reports 0 issues ontypes/.