fix(read): Duration() parses Go duration strings (e.g. "300s", "1h30m")#218
Merged
Merged
Conversation
The Config.Duration getter read the value as an int64 and returned time.Duration(n) — i.e. nanoseconds — so a duration string like "300s" could not be read at all (it failed integer parsing and yielded 0), and a bare number was interpreted as nanoseconds. Duration parsing existed only on the struct-binding path (ParseTime decode hook), not the getter. Parse the stored value as a Go duration string first (time.ParseDuration), falling back to the previous int64-nanoseconds behaviour for bare integers, then to the default. Backwards compatible; adds a getter test.
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.
The
Config.Durationgetter reads the value as anint64and returnstime.Duration(n)— i.e. nanoseconds — so:"300s"cannot be read at all: it fails integer parsing and yields0(not even the default);300is interpreted as300ns.Duration-string parsing existed only on the struct-binding path (the
ParseTimedecode hook viareflects.ToTimeOrDuration), never on theDuration()getter — soconfig.Duration("ttl")on attl: 300svalue silently returns the wrong result.This parses the stored value as a Go duration string first (
time.ParseDuration), falling back to the previous int64-nanoseconds behaviour for bare integers, then to the default:Backwards compatible: bare integers keep their nanosecond meaning; only previously-unreadable duration strings change (from
0/error to the parsed value). No existing test asserted the old getter behaviour (the existingTestDurationcovers theDecode/ParseTimepath). Adds a getter test covering"300s"/"1h30m"/"20m", a bare integer, and the malformed/missing default fallbacks. Full package test suite passes.