Skip to content

Commit fd63704

Browse files
committed
Map to time.Time pointers
1 parent 295c630 commit fd63704

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed

record.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,14 +654,28 @@ func (r *Record) Map(obj interface{}, timeLoc *time.Location) {
654654
field.Set(reflect.ValueOf(r.Time(tag, timeLoc)))
655655
}
656656

657-
//Nested structs
658657
if field.Kind() == reflect.Struct {
659658
//Map nested struct
660659
r.Map(field.Addr().Interface(), timeLoc)
661660
continue
662661
} else if field.Kind() == reflect.Pointer && field.Elem().Kind() == reflect.Struct {
663662
//Map nested pointer to struct
664663
r.Map(field.Interface(), timeLoc)
664+
continue
665+
} else if field.Kind() == reflect.Pointer &&
666+
field.Type().Elem() == reflect.TypeOf(time.Time{}) {
667+
//Field is a time.Time pointer
668+
t := r.Time(tag, timeLoc)
669+
670+
//Only set field if time is not zero
671+
if field.IsNil() && !t.IsZero() {
672+
//Nil pointer
673+
field.Set(reflect.ValueOf(&t))
674+
} else if !t.IsZero() {
675+
//Value pointer
676+
field.Elem().Set(reflect.ValueOf(t))
677+
}
678+
665679
continue
666680
}
667681
}

record_test.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,28 @@ type nestedStructPointer struct {
4949
}
5050

5151
type testRecordStruct struct {
52-
String string `fm:"string"`
53-
Int int `fm:"int"`
54-
Int8 int8 `fm:"int8"`
55-
Int16 int16 `fm:"int16"`
56-
Int32 int32 `fm:"int32"`
57-
Int64 int64 `fm:"int64"`
58-
Float32 float32 `fm:"float32"`
59-
Float64 float64 `fm:"float64"`
60-
BoolTrueTxtTest bool `fm:"bool_true_txt_test"`
61-
BoolTrueTxtFalse bool `fm:"bool_true_txt_false"`
62-
BoolFalseTxt bool `fm:"bool_false_txt"`
63-
BoolTrueNum1 bool `fm:"bool_true_num_1"`
64-
BoolTrueNum123 bool `fm:"bool_true_num_123"`
65-
BoolFalseNum0 bool `fm:"bool_false_num_0"`
66-
Date1 time.Time `fm:"date_1"`
67-
Date2 time.Time `fm:"date_2"`
68-
Timestamp1 time.Time `fm:"timestamp_1"`
69-
Timestamp2 time.Time `fm:"timestamp_2"`
70-
TimeInvalid time.Time `fm:"time_invalid"`
71-
Nested struct {
52+
String string `fm:"string"`
53+
Int int `fm:"int"`
54+
Int8 int8 `fm:"int8"`
55+
Int16 int16 `fm:"int16"`
56+
Int32 int32 `fm:"int32"`
57+
Int64 int64 `fm:"int64"`
58+
Float32 float32 `fm:"float32"`
59+
Float64 float64 `fm:"float64"`
60+
BoolTrueTxtTest bool `fm:"bool_true_txt_test"`
61+
BoolTrueTxtFalse bool `fm:"bool_true_txt_false"`
62+
BoolFalseTxt bool `fm:"bool_false_txt"`
63+
BoolTrueNum1 bool `fm:"bool_true_num_1"`
64+
BoolTrueNum123 bool `fm:"bool_true_num_123"`
65+
BoolFalseNum0 bool `fm:"bool_false_num_0"`
66+
Date1 time.Time `fm:"date_1"`
67+
Date2 time.Time `fm:"date_2"`
68+
Timestamp1 time.Time `fm:"timestamp_1"`
69+
Timestamp2 time.Time `fm:"timestamp_2"`
70+
TimeInvalid time.Time `fm:"time_invalid"`
71+
TimePointer *time.Time `fm:"timestamp_1"`
72+
TimePointerInvalid *time.Time `fm:"time_invalid"`
73+
Nested struct {
7274
String string `fm:"related::string"`
7375
Nested struct {
7476
String string `fm:"string_2"`
@@ -242,6 +244,21 @@ func TestRecordMap(t *testing.T) {
242244
}
243245
})
244246

247+
t.Run("time_pointer", func(t *testing.T) {
248+
got := value.TimePointer
249+
expect := time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)
250+
if got == nil || *got != expect {
251+
t.Errorf("got: %v, expected: %v", got, expect)
252+
}
253+
})
254+
255+
t.Run("time_pointer_invalid", func(t *testing.T) {
256+
got := value.TimePointerInvalid
257+
if got != nil {
258+
t.Errorf("got: %v, expected: %v", got, nil)
259+
}
260+
})
261+
245262
t.Run("nested_related_string", func(t *testing.T) {
246263
got := value.Nested.String
247264
expect := "related"

0 commit comments

Comments
 (0)