Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ua: decode types that convert to time.Time #634

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ua/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func isBinaryDecoder(val reflect.Value) bool {
}

func isTime(val reflect.Value) bool {
return val.Type() == timeType
return val.CanConvert(timeType)
}

type BinaryDecoder interface {
Expand All @@ -49,7 +49,7 @@ func decode(b []byte, val reflect.Value, name string) (n int, err error) {
v := val.Interface().(BinaryDecoder)
return v.Decode(b)
case isTime(val):
val.Set(reflect.ValueOf(buf.ReadTime()))
val.Set(reflect.ValueOf(buf.ReadTime()).Convert(val.Type()))
default:
// fmt.Printf("decode: %s is a %s\n", name, val.Kind())
switch val.Kind() {
Expand Down
13 changes: 13 additions & 0 deletions ua/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ type C struct {
B [2]byte
}

type Timestamp time.Time

func TestCodec(t *testing.T) {

tests := []struct {
name string
v interface{}
Expand Down Expand Up @@ -205,6 +208,16 @@ func TestCodec(t *testing.T) {
v: &struct{ V time.Time }{time.Time{}},
b: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
{
name: "DateTime as Timestamp",
v: &struct{ V Timestamp }{Timestamp(time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC))},
b: []byte{0x00, 0x98, 0x67, 0xdd, 0xfd, 0x30, 0xd4, 0x01},
},
{
name: "DateTimeZero as Timestamp",
v: &struct{ V Timestamp }{Timestamp{}},
b: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
{
name: "[]uint32==nil",
v: &struct{ V []uint32 }{},
Expand Down
2 changes: 1 addition & 1 deletion ua/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func encode(val reflect.Value, name string) ([]byte, error) {
return v.Encode()

case isTime(val):
buf.WriteTime(val.Interface().(time.Time))
buf.WriteTime(val.Convert(timeType).Interface().(time.Time))

default:
switch val.Kind() {
Expand Down