Skip to content

Commit 8041905

Browse files
committed
Fixed time.Time values ignoring the omitempty tag. Fixes #176.
1 parent b1a2248 commit 8041905

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## Unreleased
6+
### Fixed
7+
- Fixed bug causing empty times to be inserted into the DB even when the omitempty tag was set.
8+
59
## v0.7.0 - 2015-03-30
610

711
This release includes support for RethinkDB 2.0 and connecting to clusters. To connect to a cluster you should use the new `Addresses` field in `ConnectOpts`, for example:

encoding/encoder_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"image"
55
"reflect"
66
"testing"
7+
"time"
78
)
89

910
var encodeExpected = map[string]interface{}{
@@ -74,6 +75,9 @@ type Optionals struct {
7475
Ir int `gorethink:"omitempty"` // actually named omitempty, not an option
7576
Io int `gorethink:"io,omitempty"`
7677

78+
Tr time.Time `gorethink:"tr"`
79+
To time.Time `gorethink:"to,omitempty"`
80+
7781
Slr []string `gorethink:"slr"`
7882
Slo []string `gorethink:"slo,omitempty"`
7983

@@ -84,22 +88,24 @@ type Optionals struct {
8488
var optionalsExpected = map[string]interface{}{
8589
"sr": "",
8690
"omitempty": int64(0),
91+
"tr": map[string]interface{}{"$reql_type$": "TIME", "epoch_time": 0, "timezone": "+00:00"},
8792
"slr": []interface{}{},
8893
"mr": map[string]interface{}{},
8994
}
9095

9196
func TestOmitEmpty(t *testing.T) {
9297
var o Optionals
9398
o.Sw = "something"
99+
o.Tr = time.Unix(0, 0)
94100
o.Mr = map[string]interface{}{}
95101
o.Mo = map[string]interface{}{}
96102

97103
got, err := Encode(&o)
98104
if err != nil {
99105
t.Fatal(err)
100106
}
101-
if !reflect.DeepEqual(got, optionalsExpected) {
102-
t.Errorf(" got: %v\nwant: %v\n", got, optionalsExpected)
107+
if !jsonEqual(got, optionalsExpected) {
108+
t.Errorf("\ngot: %#v\nwant: %#v\n", got, optionalsExpected)
103109
}
104110
}
105111

encoding/encoder_types.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (se *structEncoder) encode(v reflect.Value) interface{} {
133133

134134
for i, f := range se.fields {
135135
fv := fieldByIndex(v, f.index)
136-
if !fv.IsValid() || f.omitEmpty && isEmptyValue(fv) {
136+
if !fv.IsValid() || f.omitEmpty && se.isEmptyValue(fv) {
137137
continue
138138
}
139139

@@ -143,6 +143,14 @@ func (se *structEncoder) encode(v reflect.Value) interface{} {
143143
return m
144144
}
145145

146+
func (se *structEncoder) isEmptyValue(v reflect.Value) bool {
147+
if v.Type() == timeType {
148+
return v.Interface().(time.Time) == time.Time{}
149+
}
150+
151+
return isEmptyValue(v)
152+
}
153+
146154
func newStructEncoder(t reflect.Type) encoderFunc {
147155
fields := cachedTypeFields(t)
148156
se := &structEncoder{

0 commit comments

Comments
 (0)