Skip to content

Commit dfdb24e

Browse files
authored
Handle nil timestamps in query parameters. (#389)
We represent timestamps as `*time.Time`. However, when a nullable timestamp is serialized to query parameters in paths.go, we panic on the nil pointer. This patch adds a helper to wrap time formatting to safely check and omit nil timestamps. Note: I think we shouldn't represent all timestamps, including non-nullable timestamps, as `*time.Time`, but I'll think about that in another patch.
1 parent c14d206 commit dfdb24e

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

internal/generate/paths.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,7 @@ func buildPathOrQueryParams(
426426
case "*int":
427427
pathParams = append(pathParams, fmt.Sprintf("%q: PointerIntToStr(%s),", name, n))
428428
case "*time.Time":
429-
pathParams = append(
430-
pathParams,
431-
fmt.Sprintf("%q: %s.Format(time.RFC3339),", name, n),
432-
)
429+
pathParams = append(pathParams, fmt.Sprintf("%q: PointerTimeToStr(%s),", name, n))
433430
default:
434431
if p.Schema.Value != nil && p.Schema.Value.Type.Is("integer") {
435432
pathParams = append(pathParams, fmt.Sprintf("%q: fmt.Sprint(%s),", name, n))

oxide/paths.go

Lines changed: 6 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

oxide/utils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strconv"
88
"strings"
99
"text/template"
10+
"time"
1011
)
1112

1213
// NewPointer returns a pointer to a given value.
@@ -24,6 +25,16 @@ func PointerIntToStr(i *int) string {
2425
return strconv.Itoa(*i)
2526
}
2627

28+
// PointerTimeToStr converts a *time.Time into an RFC3339 string.
29+
// If nil, an empty string is returned.
30+
func PointerTimeToStr(t *time.Time) string {
31+
if t == nil {
32+
return ""
33+
}
34+
35+
return t.Format(time.RFC3339)
36+
}
37+
2738
// resolveRelative combines a url base with a relative path.
2839
func resolveRelative(basestr, relstr string) string {
2940
u, _ := url.Parse(basestr)

0 commit comments

Comments
 (0)