Skip to content

Commit

Permalink
Adds support for decoding Timestamp to Time (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
justing-bq authored Jul 9, 2021
1 parent beaf154 commit 3f5d6c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
11 changes: 9 additions & 2 deletions ion/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,13 +506,20 @@ func (d *Decoder) decodeTimestampTo(v reflect.Value) error {

switch v.Kind() {
case reflect.Struct:
if v.Type() == timestampType {
switch v.Type() {
case timestampType:
if val != nil {
v.Set(reflect.ValueOf(*val))
}
return d.attachAnnotations(v)
case nativeTimeType:
if val != nil {
v.Set(reflect.ValueOf((*val).dateTime))
}
return d.attachAnnotations(v)
default:
return d.decodeToStructWithAnnotation(v, timestampType.Kind())
}
return d.decodeToStructWithAnnotation(v, timestampType.Kind())

case reflect.Interface:
if v.NumMethod() == 0 {
Expand Down
15 changes: 12 additions & 3 deletions ion/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,19 @@ func TestDecodeTimestampTo(t *testing.T) {
t.Run(str, func(t *testing.T) {
d := NewDecoder(NewReaderString(str))

var val Timestamp
require.NoError(t, d.DecodeTo(&val))
var timestampVal Timestamp
require.NoError(t, d.DecodeTo(&timestampVal))

assert.True(t, val.Equal(eval), "expected %v, got %v", eval, val)
assert.True(t, timestampVal.Equal(eval), "expected %v, got %v", eval, timestampVal)
})

t.Run(str, func(t *testing.T) {
d := NewDecoder(NewReaderString(str))

var timeVal time.Time
require.NoError(t, d.DecodeTo(&timeVal))

assert.True(t, timeVal.Equal(eval.dateTime), "expected %v, got %v", eval.dateTime, timeVal)
})
}
test("null", Timestamp{})
Expand Down

0 comments on commit 3f5d6c7

Please sign in to comment.