Skip to content

Commit 80c6fa3

Browse files
committed
handle date pointers
1 parent 32b6f1f commit 80c6fa3

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

liquid/standardfilters_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package liquid
33
import (
44
"strings"
55
"testing"
6+
"time"
67
)
78

89
func TestStandardFiltersSize(t *testing.T) {
@@ -1535,3 +1536,14 @@ func TestIsTruthyTypedCollections(t *testing.T) {
15351536
})
15361537
}
15371538
}
1539+
1540+
func TestStandardFilters_Date_WithPointerToTime(t *testing.T) {
1541+
now := time.Date(2024, 3, 7, 12, 0, 0, 0, time.UTC)
1542+
ptr := &now
1543+
sf := &StandardFilters{}
1544+
result := sf.Date(ptr, "%b %d, %Y")
1545+
expected := "Mar 07, 2024"
1546+
if result != expected {
1547+
t.Errorf("Date(*time.Time) = %v, expected %v", result, expected)
1548+
}
1549+
}

liquid/utils.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,19 @@ func ToNumber(obj interface{}) interface{} {
171171

172172
// ToDate converts a value to a time.Time.
173173
func ToDate(obj interface{}) *time.Time {
174-
// If it already has a Strftime method (time.Time), return it
174+
// Handle time.Time directly
175175
if t, ok := obj.(time.Time); ok {
176176
return &t
177177
}
178178

179+
// Handle *time.Time pointer (NEW - fixes the bug)
180+
if t, ok := obj.(*time.Time); ok {
181+
if t == nil {
182+
return nil
183+
}
184+
return t
185+
}
186+
179187
switch v := obj.(type) {
180188
case string:
181189
if v == "" {

liquid/utils_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func TestToDate(t *testing.T) {
8686
wantNil bool
8787
}{
8888
{"time.Time", now, false},
89+
{"*time.Time pointer", &now, false},
90+
{"nil *time.Time pointer", (*time.Time)(nil), true},
8991
{"now string", "now", false},
9092
{"today string", "today", false},
9193
{"unix timestamp string", "1609459200", false},
@@ -102,6 +104,27 @@ func TestToDate(t *testing.T) {
102104
}
103105
}
104106

107+
func TestToDate_PointerToTime(t *testing.T) {
108+
now := time.Now()
109+
ptr := &now
110+
got := ToDate(ptr)
111+
if got == nil {
112+
t.Errorf("ToDate(*time.Time) = nil, expected non-nil")
113+
return
114+
}
115+
if !got.Equal(now) {
116+
t.Errorf("ToDate(*time.Time) = %v, expected %v", got, now)
117+
}
118+
}
119+
120+
func TestToDate_NilPointerToTime(t *testing.T) {
121+
var ptr *time.Time = nil
122+
got := ToDate(ptr)
123+
if got != nil {
124+
t.Errorf("ToDate(nil *time.Time) = %v, expected nil", got)
125+
}
126+
}
127+
105128
func TestToS(t *testing.T) {
106129
tests := []struct {
107130
name string

0 commit comments

Comments
 (0)