From 783883570e64d9091366e7baab9b9599d2726f49 Mon Sep 17 00:00:00 2001 From: aliiohs Date: Thu, 18 Jul 2019 14:12:09 +0800 Subject: [PATCH 01/15] change package version --- date.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/date.go b/date.go index 8ae4cc6b..5368da37 100644 --- a/date.go +++ b/date.go @@ -46,7 +46,7 @@ func encDateInMimute(b []byte, v time.Time) []byte { // # time in UTC encoded as 64-bit long milliseconds since epoch // ::= x4a b7 b6 b5 b4 b3 b2 b1 b0 // ::= x4b b3 b2 b1 b0 # minutes since epoch -func (d *Decoder) decDate(flag int32) (time.Time, error) { +func (d *Decoder) decDate(flag int32) (interface{}, error) { var ( err error l int @@ -64,6 +64,8 @@ func (d *Decoder) decDate(flag int32) (time.Time, error) { } switch { + case tag == BC_NULL: + return nil, nil case tag == BC_DATE: //'d': //date s = buf[:8] l, err = d.next(s) From 47029d60255a4697a06e95b42db1e767fbe05576 Mon Sep 17 00:00:00 2001 From: aliiohs Date: Thu, 18 Jul 2019 15:08:35 +0800 Subject: [PATCH 02/15] change package version --- date.go | 6 ++- date_test.go | 54 +++++++++++++++++++ encode.go | 9 ++-- .../src/main/java/test/TestCustomDecode.java | 6 +++ .../src/main/java/test/TestCustomReply.java | 7 +++ .../src/main/java/test/model/DateDemo.java | 32 +++++++++++ 6 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 test_hessian/src/main/java/test/model/DateDemo.java diff --git a/date.go b/date.go index 5368da37..ad54034a 100644 --- a/date.go +++ b/date.go @@ -26,6 +26,8 @@ import ( // Date ///////////////////////////////////////// +var ZeroDate = time.Time{} + // # time in UTC encoded as 64-bit long milliseconds since epoch // ::= x4a b7 b6 b5 b4 b3 b2 b1 b0 // ::= x4b b3 b2 b1 b0 # minutes since epoch @@ -46,7 +48,7 @@ func encDateInMimute(b []byte, v time.Time) []byte { // # time in UTC encoded as 64-bit long milliseconds since epoch // ::= x4a b7 b6 b5 b4 b3 b2 b1 b0 // ::= x4b b3 b2 b1 b0 # minutes since epoch -func (d *Decoder) decDate(flag int32) (interface{}, error) { +func (d *Decoder) decDate(flag int32) (time.Time, error) { var ( err error l int @@ -65,7 +67,7 @@ func (d *Decoder) decDate(flag int32) (interface{}, error) { switch { case tag == BC_NULL: - return nil, nil + return ZeroDate, nil case tag == BC_DATE: //'d': //date s = buf[:8] l, err = d.next(s) diff --git a/date_test.go b/date_test.go index 02cc93b2..0d6ba97f 100644 --- a/date_test.go +++ b/date_test.go @@ -15,10 +15,24 @@ package hessian import ( + "github.com/stretchr/testify/assert" "testing" "time" ) +func init() { + RegisterPOJO(&DateDemo{}) +} + +type DateDemo struct { + Name string + Date time.Time +} + +func (DateDemo) JavaClassName() string { + return "test.model.DateDemo" +} + func TestEncDate(t *testing.T) { var ( v string @@ -71,3 +85,43 @@ func TestDateEncode(t *testing.T) { testJavaDecode(t, "argDate_1", time.Date(1998, 5, 8, 9, 51, 31, 0, time.UTC)) testJavaDecode(t, "argDate_2", time.Date(1998, 5, 8, 9, 51, 0, 0, time.UTC)) } + +func TestEncDateNull(t *testing.T) { + var ( + e *Encoder + d *Decoder + res interface{} + ) + date := DateDemo{ + Name: "s", + Date: ZeroDate, + } + e = NewEncoder() + e.Encode(date) + if len(e.Buffer()) == 0 { + t.Fail() + } + d = NewDecoder(e.Buffer()) + res, _ = d.Decode() + assert.Equal(t, ZeroDate, res.(*DateDemo).Date) +} + +func TestDateNulJavaDecode(t *testing.T) { + date := DateDemo{ + Name: "s", + Date: ZeroDate, + } + testJavaDecode(t, "customArgTypedFixedList_DateNull", date) +} + +func TestDateNullDecode(t *testing.T) { + + doTestDateNull(t, "customReplyTypedFixedDateNull") +} + +func doTestDateNull(t *testing.T, method string) { + testDecodeFrameworkFunc(t, method, func(r interface{}) { + t.Logf("%#v", r) + assert.Equal(t, ZeroDate, r.(*DateDemo).Date) + }) +} diff --git a/encode.go b/encode.go index f5f333b8..2117bb20 100644 --- a/encode.go +++ b/encode.go @@ -101,9 +101,12 @@ func (e *Encoder) Encode(v interface{}) error { e.buffer = encInt64(e.buffer, int64(val)) case time.Time: - e.buffer = encDateInMs(e.buffer, val) - // e.buffer = encDateInMimute(v.(time.Time), e.buffer) - + if ZeroDate == val { + e.buffer = encNull(e.buffer) + } else { + e.buffer = encDateInMs(e.buffer, val) + // e.buffer = encDateInMimute(v.(time.Time), e.buffer) + } case float32: e.buffer = encFloat(e.buffer, float64(val)) diff --git a/test_hessian/src/main/java/test/TestCustomDecode.java b/test_hessian/src/main/java/test/TestCustomDecode.java index 27ac4ce0..dd9245d6 100644 --- a/test_hessian/src/main/java/test/TestCustomDecode.java +++ b/test_hessian/src/main/java/test/TestCustomDecode.java @@ -26,6 +26,7 @@ import java.util.Date; import java.util.List; import java.math.BigDecimal; +import test.model.DateDemo; public class TestCustomDecode { @@ -168,4 +169,9 @@ public Object customArgTypedFixedList_Decimal() throws Exception { BigDecimal o = (BigDecimal) input.readObject(); return o.toString().equals( "100.256"); } + + public Object customArgTypedFixedList_DateNull() throws Exception { + DateDemo o = (DateDemo) input.readObject(); + return o.getDate() == null; + } } \ No newline at end of file diff --git a/test_hessian/src/main/java/test/TestCustomReply.java b/test_hessian/src/main/java/test/TestCustomReply.java index 21ada2a1..83938da9 100644 --- a/test_hessian/src/main/java/test/TestCustomReply.java +++ b/test_hessian/src/main/java/test/TestCustomReply.java @@ -23,6 +23,7 @@ import java.util.Date; import java.util.HashMap; import java.math.BigDecimal; +import test.model.DateDemo; public class TestCustomReply { @@ -346,6 +347,12 @@ public void customReplyTypedFixedDecimal() throws Exception { output.flush(); } + public void customReplyTypedFixedDateNull() throws Exception { + DateDemo demo = new DateDemo("zhangshan",null); + output.writeObject(demo); + output.flush(); + } + } class TypedListTest implements Serializable { diff --git a/test_hessian/src/main/java/test/model/DateDemo.java b/test_hessian/src/main/java/test/model/DateDemo.java new file mode 100644 index 00000000..56fcb523 --- /dev/null +++ b/test_hessian/src/main/java/test/model/DateDemo.java @@ -0,0 +1,32 @@ +package test.model; + +import java.io.Serializable; +import java.util.Date; + +public class DateDemo implements Serializable { + private String name; + private Date date; + + public String getName() { + return name; + } + + public DateDemo() {} + + public DateDemo(String name,Date date) { + this.name = name; + this.date = date; + } + + public void setName(String name) { + this.name = name; + } + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + +} \ No newline at end of file From e9a885581809f1ef3d23f4deb245b5518b9c82b8 Mon Sep 17 00:00:00 2001 From: aliiohs Date: Thu, 18 Jul 2019 18:12:50 +0800 Subject: [PATCH 03/15] deal *time.Time --- date.go | 5 ++++- date_test.go | 14 +++++++++----- encode.go | 6 +++++- .../src/main/java/test/TestCustomDecode.java | 2 +- .../src/main/java/test/TestCustomReply.java | 2 +- .../src/main/java/test/model/DateDemo.java | 11 ++++++++++- 6 files changed, 30 insertions(+), 10 deletions(-) diff --git a/date.go b/date.go index ad54034a..f62cbbce 100644 --- a/date.go +++ b/date.go @@ -31,7 +31,10 @@ var ZeroDate = time.Time{} // # time in UTC encoded as 64-bit long milliseconds since epoch // ::= x4a b7 b6 b5 b4 b3 b2 b1 b0 // ::= x4b b3 b2 b1 b0 # minutes since epoch -func encDateInMs(b []byte, v time.Time) []byte { +func encDateInMs(b []byte, v *time.Time) []byte { + if v == nil { + return append(b, BC_NULL) + } b = append(b, BC_DATE) return append(b, PackInt64(v.UnixNano()/1e6)...) } diff --git a/date_test.go b/date_test.go index 0d6ba97f..81ba501b 100644 --- a/date_test.go +++ b/date_test.go @@ -25,8 +25,9 @@ func init() { } type DateDemo struct { - Name string - Date time.Time + Name string + Date time.Time + Date1 *time.Time } func (DateDemo) JavaClassName() string { @@ -93,8 +94,9 @@ func TestEncDateNull(t *testing.T) { res interface{} ) date := DateDemo{ - Name: "s", - Date: ZeroDate, + Name: "s", + Date: ZeroDate, + Date1: nil, } e = NewEncoder() e.Encode(date) @@ -104,6 +106,7 @@ func TestEncDateNull(t *testing.T) { d = NewDecoder(e.Buffer()) res, _ = d.Decode() assert.Equal(t, ZeroDate, res.(*DateDemo).Date) + assert.Equal(t, true, res.(*DateDemo).Date1 == nil) } func TestDateNulJavaDecode(t *testing.T) { @@ -114,7 +117,7 @@ func TestDateNulJavaDecode(t *testing.T) { testJavaDecode(t, "customArgTypedFixedList_DateNull", date) } -func TestDateNullDecode(t *testing.T) { +func TestDateNilDecode(t *testing.T) { doTestDateNull(t, "customReplyTypedFixedDateNull") } @@ -123,5 +126,6 @@ func doTestDateNull(t *testing.T, method string) { testDecodeFrameworkFunc(t, method, func(r interface{}) { t.Logf("%#v", r) assert.Equal(t, ZeroDate, r.(*DateDemo).Date) + assert.Equal(t, true, r.(*DateDemo).Date1 == nil) }) } diff --git a/encode.go b/encode.go index 2117bb20..a5157725 100644 --- a/encode.go +++ b/encode.go @@ -104,9 +104,13 @@ func (e *Encoder) Encode(v interface{}) error { if ZeroDate == val { e.buffer = encNull(e.buffer) } else { - e.buffer = encDateInMs(e.buffer, val) + e.buffer = encDateInMs(e.buffer, &val) // e.buffer = encDateInMimute(v.(time.Time), e.buffer) } + + case *time.Time: + e.buffer = encDateInMs(e.buffer, val) + case float32: e.buffer = encFloat(e.buffer, float64(val)) diff --git a/test_hessian/src/main/java/test/TestCustomDecode.java b/test_hessian/src/main/java/test/TestCustomDecode.java index dd9245d6..5eae85fb 100644 --- a/test_hessian/src/main/java/test/TestCustomDecode.java +++ b/test_hessian/src/main/java/test/TestCustomDecode.java @@ -172,6 +172,6 @@ public Object customArgTypedFixedList_Decimal() throws Exception { public Object customArgTypedFixedList_DateNull() throws Exception { DateDemo o = (DateDemo) input.readObject(); - return o.getDate() == null; + return o.getDate() == null && o.getDate1() == null; } } \ No newline at end of file diff --git a/test_hessian/src/main/java/test/TestCustomReply.java b/test_hessian/src/main/java/test/TestCustomReply.java index 83938da9..bbe64038 100644 --- a/test_hessian/src/main/java/test/TestCustomReply.java +++ b/test_hessian/src/main/java/test/TestCustomReply.java @@ -348,7 +348,7 @@ public void customReplyTypedFixedDecimal() throws Exception { } public void customReplyTypedFixedDateNull() throws Exception { - DateDemo demo = new DateDemo("zhangshan",null); + DateDemo demo = new DateDemo("zhangshan",null,null); output.writeObject(demo); output.flush(); } diff --git a/test_hessian/src/main/java/test/model/DateDemo.java b/test_hessian/src/main/java/test/model/DateDemo.java index 56fcb523..3dad162c 100644 --- a/test_hessian/src/main/java/test/model/DateDemo.java +++ b/test_hessian/src/main/java/test/model/DateDemo.java @@ -6,6 +6,7 @@ public class DateDemo implements Serializable { private String name; private Date date; + private Date date1; public String getName() { return name; @@ -13,9 +14,10 @@ public String getName() { public DateDemo() {} - public DateDemo(String name,Date date) { + public DateDemo(String name,Date date,Date date1) { this.name = name; this.date = date; + this.date1 = date1; } public void setName(String name) { @@ -28,5 +30,12 @@ public Date getDate() { public void setDate(Date date) { this.date = date; } + public Date getDate1() { + return date1; + } + + public void setDate1(Date date1) { + this.date1 = date1; + } } \ No newline at end of file From 777075af02760b84f319e249dc8c90d57f8c8be5 Mon Sep 17 00:00:00 2001 From: aliiohs Date: Thu, 18 Jul 2019 18:27:50 +0800 Subject: [PATCH 04/15] add apache standard license header --- .../src/main/java/test/model/DateDemo.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test_hessian/src/main/java/test/model/DateDemo.java b/test_hessian/src/main/java/test/model/DateDemo.java index 3dad162c..69a78fb4 100644 --- a/test_hessian/src/main/java/test/model/DateDemo.java +++ b/test_hessian/src/main/java/test/model/DateDemo.java @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package test.model; import java.io.Serializable; From 80259c0bfe9c417160d03fcf4e07a4114f0be42a Mon Sep 17 00:00:00 2001 From: aliiohs Date: Thu, 18 Jul 2019 23:08:00 +0800 Subject: [PATCH 05/15] sort import --- date_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/date_test.go b/date_test.go index 81ba501b..05da167f 100644 --- a/date_test.go +++ b/date_test.go @@ -15,10 +15,12 @@ package hessian import ( - "github.com/stretchr/testify/assert" "testing" "time" ) +import ( + "github.com/stretchr/testify/assert" +) func init() { RegisterPOJO(&DateDemo{}) From b7ff81d91b6d3a6da3e79ccbea7a5d2922ce1af0 Mon Sep 17 00:00:00 2001 From: aliiohs Date: Sat, 20 Jul 2019 20:44:20 +0800 Subject: [PATCH 06/15] fix some bug when item is ptr --- date.go | 11 ++++++----- date_test.go | 38 +++++++++++++++++++++++++++++--------- encode.go | 26 ++++++++++++++++---------- object.go | 9 +++++++-- 4 files changed, 58 insertions(+), 26 deletions(-) diff --git a/date.go b/date.go index f62cbbce..bcd0130b 100644 --- a/date.go +++ b/date.go @@ -15,6 +15,7 @@ package hessian import ( + "reflect" "time" ) @@ -31,12 +32,12 @@ var ZeroDate = time.Time{} // # time in UTC encoded as 64-bit long milliseconds since epoch // ::= x4a b7 b6 b5 b4 b3 b2 b1 b0 // ::= x4b b3 b2 b1 b0 # minutes since epoch -func encDateInMs(b []byte, v *time.Time) []byte { - if v == nil { - return append(b, BC_NULL) - } +func encDateInMs(b []byte, i interface{}) []byte { + + value := UnpackPtrValue(reflect.ValueOf(i)) + vi := value.Interface().(time.Time) b = append(b, BC_DATE) - return append(b, PackInt64(v.UnixNano()/1e6)...) + return append(b, PackInt64(vi.UnixNano()/1e6)...) } func encDateInMimute(b []byte, v time.Time) []byte { diff --git a/date_test.go b/date_test.go index 05da167f..6add31cd 100644 --- a/date_test.go +++ b/date_test.go @@ -27,9 +27,12 @@ func init() { } type DateDemo struct { - Name string - Date time.Time - Date1 *time.Time + Name string + Date time.Time + NilDate *time.Time + Date1 *time.Time + Date2 **time.Time + Date3 ***time.Time } func (DateDemo) JavaClassName() string { @@ -91,14 +94,26 @@ func TestDateEncode(t *testing.T) { func TestEncDateNull(t *testing.T) { var ( + v string + tz time.Time e *Encoder d *Decoder res interface{} ) + v = "2014-02-09 06:15:23 +0800 CST" + tz, _ = time.Parse("2006-01-02 15:04:05 +0800 CST", v) + d1 := &tz + d2 := &d1 + d3 := &d2 + date := DateDemo{ - Name: "s", - Date: ZeroDate, - Date1: nil, + Name: "zs", + Date: ZeroDate, + + NilDate: nil, + Date1: nil, + Date2: d2, + Date3: d3, } e = NewEncoder() e.Encode(date) @@ -108,12 +123,17 @@ func TestEncDateNull(t *testing.T) { d = NewDecoder(e.Buffer()) res, _ = d.Decode() assert.Equal(t, ZeroDate, res.(*DateDemo).Date) - assert.Equal(t, true, res.(*DateDemo).Date1 == nil) + assert.Equal(t, true, res.(*DateDemo).Date == ZeroDate) + assert.Equal(t, &ZeroDate, res.(*DateDemo).NilDate) + assert.Equal(t, ZeroDate, *res.(*DateDemo).Date1) + assert.Equal(t, tz.Local().String(), (*res.(*DateDemo).Date2).String()) + assert.Equal(t, tz.Local().String(), (*(*res.(*DateDemo).Date3)).String()) + } func TestDateNulJavaDecode(t *testing.T) { date := DateDemo{ - Name: "s", + Name: "zs", Date: ZeroDate, } testJavaDecode(t, "customArgTypedFixedList_DateNull", date) @@ -128,6 +148,6 @@ func doTestDateNull(t *testing.T, method string) { testDecodeFrameworkFunc(t, method, func(r interface{}) { t.Logf("%#v", r) assert.Equal(t, ZeroDate, r.(*DateDemo).Date) - assert.Equal(t, true, r.(*DateDemo).Date1 == nil) + assert.Equal(t, &ZeroDate, r.(*DateDemo).Date1) }) } diff --git a/encode.go b/encode.go index a5157725..73e97e18 100644 --- a/encode.go +++ b/encode.go @@ -108,9 +108,9 @@ func (e *Encoder) Encode(v interface{}) error { // e.buffer = encDateInMimute(v.(time.Time), e.buffer) } - case *time.Time: - e.buffer = encDateInMs(e.buffer, val) - + /* case *time.Time: + e.buffer = encDateInMs(e.buffer, val) + */ case float32: e.buffer = encFloat(e.buffer, float64(val)) @@ -130,15 +130,21 @@ func (e *Encoder) Encode(v interface{}) error { t := UnpackPtrType(reflect.TypeOf(v)) switch t.Kind() { case reflect.Struct: + vv := reflect.ValueOf(v) + vv = UnpackPtr(vv) + if !vv.IsValid() { + e.buffer = encNull(e.buffer) + return nil + } + if vv.Type().String() == "time.Time" { + e.buffer = encDateInMs(e.buffer, v) + return nil + } if p, ok := v.(POJO); ok { var clazz string - vv := reflect.ValueOf(v) - vv = UnpackPtr(vv) - if vv.IsValid() { - clazz = p.JavaClassName() - if c, ok := GetSerializer(clazz); ok { - return c.EncObject(e, p) - } + clazz = p.JavaClassName() + if c, ok := GetSerializer(clazz); ok { + return c.EncObject(e, p) } return e.encObject(p) } diff --git a/object.go b/object.go index d9f3cc05..f0176889 100644 --- a/object.go +++ b/object.go @@ -424,12 +424,17 @@ func (d *Decoder) decInstance(typ reflect.Type, cls classInfo) (interface{}, err err error s interface{} ) - if fldRawValue.Type().String() == "time.Time" { + typeName := fldRawValue.Type().String() + if strings.Contains(typeName, "time.Time") { s, err = d.decDate(TAG_READ) if err != nil { return nil, perrors.WithStack(err) } - fldRawValue.Set(reflect.ValueOf(s)) + tValue := reflect.ValueOf(s) + for fldRawValue.Type() != tValue.Type() { + tValue = PackPtr(tValue) + } + fldRawValue.Set(tValue) } else { s, err = d.decObject(TAG_READ) if err != nil { From 1edf4a3505636de700cc71cfd5d5fad71d1aeb54 Mon Sep 17 00:00:00 2001 From: aliiohs Date: Sat, 20 Jul 2019 22:38:45 +0800 Subject: [PATCH 07/15] fix ZeroTime bug --- date.go | 4 ++++ date_test.go | 1 - encode.go | 3 --- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/date.go b/date.go index bcd0130b..50723e09 100644 --- a/date.go +++ b/date.go @@ -36,6 +36,10 @@ func encDateInMs(b []byte, i interface{}) []byte { value := UnpackPtrValue(reflect.ValueOf(i)) vi := value.Interface().(time.Time) + if vi == ZeroDate { + b = append(b, BC_NULL) + return nil + } b = append(b, BC_DATE) return append(b, PackInt64(vi.UnixNano()/1e6)...) } diff --git a/date_test.go b/date_test.go index 6add31cd..c8705405 100644 --- a/date_test.go +++ b/date_test.go @@ -123,7 +123,6 @@ func TestEncDateNull(t *testing.T) { d = NewDecoder(e.Buffer()) res, _ = d.Decode() assert.Equal(t, ZeroDate, res.(*DateDemo).Date) - assert.Equal(t, true, res.(*DateDemo).Date == ZeroDate) assert.Equal(t, &ZeroDate, res.(*DateDemo).NilDate) assert.Equal(t, ZeroDate, *res.(*DateDemo).Date1) assert.Equal(t, tz.Local().String(), (*res.(*DateDemo).Date2).String()) diff --git a/encode.go b/encode.go index 73e97e18..7ae4d057 100644 --- a/encode.go +++ b/encode.go @@ -108,9 +108,6 @@ func (e *Encoder) Encode(v interface{}) error { // e.buffer = encDateInMimute(v.(time.Time), e.buffer) } - /* case *time.Time: - e.buffer = encDateInMs(e.buffer, val) - */ case float32: e.buffer = encFloat(e.buffer, float64(val)) From 97f581dfec959312808419b72a604f90974696d9 Mon Sep 17 00:00:00 2001 From: aliiohs Date: Tue, 23 Jul 2019 11:46:48 +0800 Subject: [PATCH 08/15] fix time array bug --- date_test.go | 9 ++++++--- object.go | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/date_test.go b/date_test.go index c8705405..7ea8e1a3 100644 --- a/date_test.go +++ b/date_test.go @@ -29,6 +29,7 @@ func init() { type DateDemo struct { Name string Date time.Time + Dates []time.Time NilDate *time.Time Date1 *time.Time Date2 **time.Time @@ -107,9 +108,9 @@ func TestEncDateNull(t *testing.T) { d3 := &d2 date := DateDemo{ - Name: "zs", - Date: ZeroDate, - + Name: "zs", + Date: ZeroDate, + Dates: []time.Time{tz, tz}, NilDate: nil, Date1: nil, Date2: d2, @@ -123,6 +124,8 @@ func TestEncDateNull(t *testing.T) { d = NewDecoder(e.Buffer()) res, _ = d.Decode() assert.Equal(t, ZeroDate, res.(*DateDemo).Date) + assert.Equal(t, 2, len(res.(*DateDemo).Dates)) + assert.Equal(t, tz.Local().String(), res.(*DateDemo).Dates[0].String()) assert.Equal(t, &ZeroDate, res.(*DateDemo).NilDate) assert.Equal(t, ZeroDate, *res.(*DateDemo).Date1) assert.Equal(t, tz.Local().String(), (*res.(*DateDemo).Date2).String()) diff --git a/object.go b/object.go index f0176889..e3eb4afe 100644 --- a/object.go +++ b/object.go @@ -424,8 +424,8 @@ func (d *Decoder) decInstance(typ reflect.Type, cls classInfo) (interface{}, err err error s interface{} ) - typeName := fldRawValue.Type().String() - if strings.Contains(typeName, "time.Time") { + typ := UnpackPtrType(fldRawValue.Type()) + if typ.String() == "time.Time" { s, err = d.decDate(TAG_READ) if err != nil { return nil, perrors.WithStack(err) From 07f16fe3437ba28d177b3fd0211130371cf259ee Mon Sep 17 00:00:00 2001 From: aliiohs Date: Tue, 23 Jul 2019 15:38:54 +0800 Subject: [PATCH 09/15] fix --- codec.go | 10 ++++++++-- date_test.go | 6 +++--- object.go | 6 +----- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/codec.go b/codec.go index 6c91ce53..94cc57d6 100644 --- a/codec.go +++ b/codec.go @@ -292,7 +292,14 @@ func SetValue(dest, v reflect.Value) { return } } - + //temporary process for multi-layer ptr,base type must be the same + if UnpackPtrType(dest.Type()) == UnpackPtrType(v.Type()) { + for dest.Type() != v.Type() { + v = PackPtr(v) + } + dest.Set(v) + return + } // if the kind of dest is Ptr, the original value will be zero value // set value on zero value is not allowed // unpack to one-level pointer @@ -307,7 +314,6 @@ func SetValue(dest, v reflect.Value) { for v.IsValid() && v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Ptr { v = v.Elem() } - // zero value not need to set if !v.IsValid() { return diff --git a/date_test.go b/date_test.go index 7ea8e1a3..90440021 100644 --- a/date_test.go +++ b/date_test.go @@ -29,7 +29,7 @@ func init() { type DateDemo struct { Name string Date time.Time - Dates []time.Time + Dates []**time.Time NilDate *time.Time Date1 *time.Time Date2 **time.Time @@ -110,7 +110,7 @@ func TestEncDateNull(t *testing.T) { date := DateDemo{ Name: "zs", Date: ZeroDate, - Dates: []time.Time{tz, tz}, + Dates: []**time.Time{d2, d2}, NilDate: nil, Date1: nil, Date2: d2, @@ -125,7 +125,7 @@ func TestEncDateNull(t *testing.T) { res, _ = d.Decode() assert.Equal(t, ZeroDate, res.(*DateDemo).Date) assert.Equal(t, 2, len(res.(*DateDemo).Dates)) - assert.Equal(t, tz.Local().String(), res.(*DateDemo).Dates[0].String()) + assert.Equal(t, tz.Local().String(), (*res.(*DateDemo).Dates[0]).String()) assert.Equal(t, &ZeroDate, res.(*DateDemo).NilDate) assert.Equal(t, ZeroDate, *res.(*DateDemo).Date1) assert.Equal(t, tz.Local().String(), (*res.(*DateDemo).Date2).String()) diff --git a/object.go b/object.go index e3eb4afe..39b73857 100644 --- a/object.go +++ b/object.go @@ -430,11 +430,7 @@ func (d *Decoder) decInstance(typ reflect.Type, cls classInfo) (interface{}, err if err != nil { return nil, perrors.WithStack(err) } - tValue := reflect.ValueOf(s) - for fldRawValue.Type() != tValue.Type() { - tValue = PackPtr(tValue) - } - fldRawValue.Set(tValue) + SetValue(fldRawValue, EnsurePackValue(s)) } else { s, err = d.decObject(TAG_READ) if err != nil { From 5ebf137f9edda619f7f020dd8182e7f885752982 Mon Sep 17 00:00:00 2001 From: aliiohs Date: Tue, 23 Jul 2019 16:05:41 +0800 Subject: [PATCH 10/15] Act only on time.Time --- codec.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/codec.go b/codec.go index 94cc57d6..55a5916f 100644 --- a/codec.go +++ b/codec.go @@ -292,8 +292,13 @@ func SetValue(dest, v reflect.Value) { return } } - //temporary process for multi-layer ptr,base type must be the same - if UnpackPtrType(dest.Type()) == UnpackPtrType(v.Type()) { + // zero value not need to set + if !v.IsValid() { + return + } + //temporary process for time.Time + typ := UnpackPtrType(dest.Type()) + if typ.String() == "time.Time" { for dest.Type() != v.Type() { v = PackPtr(v) } @@ -327,11 +332,6 @@ func SetValue(dest, v reflect.Value) { v = UnpackPtrValue(v) } - // zero value not need to set - if !v.IsValid() { - return - } - // set value as required type if dest.Type() == v.Type() && dest.CanSet() { dest.Set(v) From d70d15d5f7858e35511bf79ac866c4f6ffa457ef Mon Sep 17 00:00:00 2001 From: aliiohs Date: Tue, 23 Jul 2019 19:58:40 +0800 Subject: [PATCH 11/15] change packPtr range --- codec.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/codec.go b/codec.go index 55a5916f..2aa74e75 100644 --- a/codec.go +++ b/codec.go @@ -297,8 +297,7 @@ func SetValue(dest, v reflect.Value) { return } //temporary process for time.Time - typ := UnpackPtrType(dest.Type()) - if typ.String() == "time.Time" { + if UnpackPtrType(dest.Type()) == UnpackPtrType(v.Type()) && dest.Kind() == reflect.Ptr && dest.CanSet() { for dest.Type() != v.Type() { v = PackPtr(v) } From 9f427adbfe747a4e3ba13e3911ad2699186075f3 Mon Sep 17 00:00:00 2001 From: aliiohs Date: Tue, 23 Jul 2019 21:59:39 +0800 Subject: [PATCH 12/15] fix --- codec.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codec.go b/codec.go index 2aa74e75..387b66e4 100644 --- a/codec.go +++ b/codec.go @@ -296,7 +296,7 @@ func SetValue(dest, v reflect.Value) { if !v.IsValid() { return } - //temporary process for time.Time + //temporary process, only handle the same type of situation if UnpackPtrType(dest.Type()) == UnpackPtrType(v.Type()) && dest.Kind() == reflect.Ptr && dest.CanSet() { for dest.Type() != v.Type() { v = PackPtr(v) From 6d7d00fdf6bf9970e4525e86ca7d20216e73cae0 Mon Sep 17 00:00:00 2001 From: aliiohs Date: Wed, 24 Jul 2019 14:22:47 +0800 Subject: [PATCH 13/15] fix --- codec.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/codec.go b/codec.go index 387b66e4..a989303d 100644 --- a/codec.go +++ b/codec.go @@ -292,18 +292,20 @@ func SetValue(dest, v reflect.Value) { return } } - // zero value not need to set - if !v.IsValid() { - return - } + //temporary process, only handle the same type of situation if UnpackPtrType(dest.Type()) == UnpackPtrType(v.Type()) && dest.Kind() == reflect.Ptr && dest.CanSet() { + // zero value not need to set + if !v.IsValid() { + return + } for dest.Type() != v.Type() { v = PackPtr(v) } dest.Set(v) return } + // if the kind of dest is Ptr, the original value will be zero value // set value on zero value is not allowed // unpack to one-level pointer @@ -330,6 +332,10 @@ func SetValue(dest, v reflect.Value) { } else { v = UnpackPtrValue(v) } + // zero value not need to set + if !v.IsValid() { + return + } // set value as required type if dest.Type() == v.Type() && dest.CanSet() { From 1d886a291bd6e455d5c613f89e794564db474ecc Mon Sep 17 00:00:00 2001 From: aliiohs Date: Wed, 24 Jul 2019 14:25:54 +0800 Subject: [PATCH 14/15] fix --- codec.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/codec.go b/codec.go index a989303d..8bb02faf 100644 --- a/codec.go +++ b/codec.go @@ -292,13 +292,12 @@ func SetValue(dest, v reflect.Value) { return } } - + // zero value not need to set + if !v.IsValid() { + return + } //temporary process, only handle the same type of situation if UnpackPtrType(dest.Type()) == UnpackPtrType(v.Type()) && dest.Kind() == reflect.Ptr && dest.CanSet() { - // zero value not need to set - if !v.IsValid() { - return - } for dest.Type() != v.Type() { v = PackPtr(v) } From 0d65025d7a58357754889c193f5cd9f220766acf Mon Sep 17 00:00:00 2001 From: aliiohs Date: Wed, 24 Jul 2019 14:30:15 +0800 Subject: [PATCH 15/15] fix --- codec.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/codec.go b/codec.go index 8bb02faf..184938cf 100644 --- a/codec.go +++ b/codec.go @@ -292,12 +292,8 @@ func SetValue(dest, v reflect.Value) { return } } - // zero value not need to set - if !v.IsValid() { - return - } //temporary process, only handle the same type of situation - if UnpackPtrType(dest.Type()) == UnpackPtrType(v.Type()) && dest.Kind() == reflect.Ptr && dest.CanSet() { + if v.IsValid() && UnpackPtrType(dest.Type()) == UnpackPtrType(v.Type()) && dest.Kind() == reflect.Ptr && dest.CanSet() { for dest.Type() != v.Type() { v = PackPtr(v) }