From 2d983192d731c9e5247dfc261965990d148b3c5a Mon Sep 17 00:00:00 2001 From: zhangshen023 <1292369127@qq.com> Date: Thu, 13 Aug 2020 07:54:59 +0800 Subject: [PATCH 01/15] java.sql.Time --- java_sql_time.go | 197 ++++++++++++++++++ java_sql_time_test.go | 35 ++++ test_hessian/src/main/java/test/Hessian.java | 23 +- .../src/main/java/test/TestJavaSqlTime.java | 69 ++++++ 4 files changed, 322 insertions(+), 2 deletions(-) create mode 100644 java_sql_time.go create mode 100644 java_sql_time_test.go create mode 100644 test_hessian/src/main/java/test/TestJavaSqlTime.java diff --git a/java_sql_time.go b/java_sql_time.go new file mode 100644 index 00000000..3b50653e --- /dev/null +++ b/java_sql_time.go @@ -0,0 +1,197 @@ +/* + * 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 hessian + +import ( + "reflect" + "time" +) + +import ( + perrors "github.com/pkg/errors" +) + +type JavaSqlTime interface { + SetTime(time int64) + ValueOf(time string) (JavaSqlTime, error) + JavaClassName() string + UnixNano() int64 + time() time.Time +} +type Date struct { + time.Time +} + +func (d Date) time() time.Time { + return d.Time +} + +func (Date) JavaClassName() string { + return "java.sql.Date" +} + +func (Date) Error() string { + return "encode java.sql.Date error" +} +func (Date) SetTime(time int64) { + panic("") +} +func (Date) ValueOf(time string) (JavaSqlTime, error) { + panic("") +} + +type Time struct { + time.Time +} + +func (Time) JavaClassName() string { + return "java.sql.Time" +} + +func (Time) Error() string { + return "encode java.sql.Time error" +} + +func (t Time) time() time.Time { + return t.Time +} + +func (Time) SetTime(time int64) { + panic("") +} +func (Time) ValueOf(time string) (JavaSqlTime, error) { + panic("") +} + +var javaSqlTimeTypeMap = make(map[string]reflect.Type, 16) + +func SetJavaSqlTimeSerialize(time JavaSqlTime) { + name := time.JavaClassName() + v := reflect.ValueOf(time) + var typ reflect.Type + switch v.Kind() { + case reflect.Struct: + typ = v.Type() + case reflect.Ptr: + typ = v.Elem().Type() + default: + typ = reflect.TypeOf(time) + } + SetSerializer(name, JavaSqlTimeSerializer{}) + //RegisterPOJO(time) + javaSqlTimeTypeMap[name] = typ +} + +func getJavaSqlTimeSerialize(name string) reflect.Type { + return javaSqlTimeTypeMap[name] +} + +type JavaSqlTimeSerializer struct { +} + +func (JavaSqlTimeSerializer) EncObject(e *Encoder, vv POJO) error { + + var ( + idx int + idx1 int + i int + err error + clsDef classInfo + ) + v, ok := vv.(JavaSqlTime) + if !ok { + return perrors.New("can not be converted into java sql time object") + } + className := v.JavaClassName() + if className == "" { + return perrors.New("class name empty") + } + + tValue := reflect.ValueOf(vv) + // check ref + if n, ok := e.checkRefMap(tValue); ok { + e.buffer = encRef(e.buffer, n) + return nil + } + + // write object definition + idx = -1 + for i = range e.classInfoList { + if v.JavaClassName() == e.classInfoList[i].javaName { + idx = i + break + } + } + + if idx == -1 { + idx1, ok = checkPOJORegistry(typeof(v)) + if !ok { + if reflect.TypeOf(v).Implements(javaEnumType) { + idx1 = RegisterJavaEnum(v.(POJOEnum)) + } else { + idx1 = RegisterPOJO(v) + } + } + _, clsDef, err = getStructDefByIndex(idx1) + if err != nil { + return perrors.WithStack(err) + } + + i = len(e.classInfoList) + e.classInfoList = append(e.classInfoList, clsDef) + e.buffer = append(e.buffer, clsDef.buffer...) + e.buffer = e.buffer[0 : len(e.buffer)-1] + } + + //if idx < -1 { + // e.buffer = encString(e.buffer, "value") + // var bytes []byte + // bytes = append(bytes, PackInt64(v.UnixNano()/1e6)...) + // e.buffer = encDateInMs(bytes, v) + // e.buffer = append(e.buffer, BC_END) + //} else + //{ + if idx == -1 { + e.buffer = encInt32(e.buffer, 1) + e.buffer = encString(e.buffer, "value") + + // write object instance + if byte(i) <= OBJECT_DIRECT_MAX { + e.buffer = encByte(e.buffer, byte(i)+BC_OBJECT_DIRECT) + } else { + e.buffer = encByte(e.buffer, BC_OBJECT) + e.buffer = encInt32(e.buffer, int32(idx1)) + } + //if (ref == -1) { + // out.writeInt(1); + // out.writeString("value"); + // out.writeObjectBegin(cl.getName()); + //} + //} + //var bytes []byte + //bytes = append(bytes, PackInt64(v.UnixNano()/1e6)...) + e.buffer = encDateInMs(e.buffer, v.time()) + } + + return nil +} + +func (JavaSqlTimeSerializer) DecObject(d *Decoder, typ reflect.Type, cls classInfo) (interface{}, error) { + + return nil, perrors.New("unexpected collection decode call") +} diff --git a/java_sql_time_test.go b/java_sql_time_test.go new file mode 100644 index 00000000..de6a2a15 --- /dev/null +++ b/java_sql_time_test.go @@ -0,0 +1,35 @@ +/* + * 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 hessian + +import ( + "testing" + "time" +) + +func init() { + SetJavaSqlTimeSerialize(&Date{}) + SetJavaSqlTimeSerialize(&Time{}) +} +func TestJavaSqlTimeEncode(t *testing.T) { + Bdate := "1997-01-01 13:15:46" + location, _ := time.ParseInLocation("2006-01-02 15:04:05", Bdate, time.Local) + time1 := Time{Time: location} + t.Log(time1.UnixNano() / 1000000) + testJavaDecode(t, "javaSql_encode_time", time1) +} diff --git a/test_hessian/src/main/java/test/Hessian.java b/test_hessian/src/main/java/test/Hessian.java index f26cbedf..a9596a26 100644 --- a/test_hessian/src/main/java/test/Hessian.java +++ b/test_hessian/src/main/java/test/Hessian.java @@ -62,15 +62,34 @@ public static void main(String[] args) throws Exception { Hessian2Output output = new Hessian2Output(System.out); output.writeObject(object); output.flush(); - }else if(args[0].startsWith("java8_")){ + } else if (args[0].startsWith("java8_")) { //add java8 java.time Object test Method method = TestJava8Time.class.getMethod(args[0]); Object obj = new Object(); - Object object =method.invoke(obj); + Object object = method.invoke(obj); Hessian2Output output = new Hessian2Output(System.out); output.writeObject(object); output.flush(); + } else if (args[0].startsWith("javaSql_")) { + if (args[0].startsWith("javaSql_encode")) { + + Hessian2Input input = new Hessian2Input(System.in); + Object o = input.readObject(); + + Method method = TestJavaSqlTime.class.getMethod(args[0], Object.class); + TestJavaSqlTime testJavaSqlTime = new TestJavaSqlTime(); + System.out.print(method.invoke(testJavaSqlTime, o)); + } else { + Method method = TestJavaSqlTime.class.getMethod(args[0]); + TestJavaSqlTime testJavaSqlTime = new TestJavaSqlTime(System.out); + Object object = method.invoke(testJavaSqlTime); + + Hessian2Output output = new Hessian2Output(System.out); + output.writeObject(object); + output.flush(); + } + } } diff --git a/test_hessian/src/main/java/test/TestJavaSqlTime.java b/test_hessian/src/main/java/test/TestJavaSqlTime.java new file mode 100644 index 00000000..7632482f --- /dev/null +++ b/test_hessian/src/main/java/test/TestJavaSqlTime.java @@ -0,0 +1,69 @@ +/* + * 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; + +import com.alibaba.com.caucho.hessian.io.Hessian2Output; + +import java.io.IOException; +import java.io.OutputStream; +import java.sql.Date; +import java.sql.Time; +import java.text.SimpleDateFormat; + +public class TestJavaSqlTime { + + public void javaSql_decode_date() throws IOException { + Date date = Date.valueOf("2020-08-09"); + output.writeObject(date); + output.flush(); + } + + public void javaSql_decode_time() throws IOException { + Time time = Time.valueOf("07:35:13"); + output.writeObject(time); + output.flush(); + } + + private Hessian2Output output; + + public TestJavaSqlTime(OutputStream os) { + output = new Hessian2Output(os); + } + + + public TestJavaSqlTime() { + } + + public static void main(String[] args) { + long time = 894621091000L; + + time -= time % 60000L; + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + System.out.println(sdf.format(new java.util.Date(time))); + } + + public static Object javaSql_encode_time(Object v) { + return v.equals(new Time(852095746000L)); + } + + public boolean javaSql_encode_date(Object v) { + Date date = Date.valueOf("2020-08-09"); + return date.equals(v); + } + +} From e9a300f08358f7bec79ff963ef16493c4f7dcf09 Mon Sep 17 00:00:00 2001 From: zhangshen023 <1292369127@qq.com> Date: Fri, 14 Aug 2020 00:30:43 +0800 Subject: [PATCH 02/15] java.sql.Time simple test --- decode_test.go | 2 +- java_sql_time.go | 48 ++++++++++++++----- java_sql_time_test.go | 20 ++++++++ test_hessian/src/main/java/test/Hessian.java | 2 +- .../src/main/java/test/TestJavaSqlTime.java | 21 ++------ 5 files changed, 62 insertions(+), 31 deletions(-) diff --git a/decode_test.go b/decode_test.go index 87f46040..024a1ff4 100644 --- a/decode_test.go +++ b/decode_test.go @@ -76,7 +76,7 @@ func getJavaReply(method, className string) []byte { if className != "" { cmdArgs = append(cmdArgs, className) } - cmd := exec.Command("java", cmdArgs...) + cmd := exec.Command("/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/bin/java", cmdArgs...) out, err := cmd.Output() if err != nil { log.Fatal(err) diff --git a/java_sql_time.go b/java_sql_time.go index 3b50653e..f358c2cf 100644 --- a/java_sql_time.go +++ b/java_sql_time.go @@ -18,6 +18,7 @@ package hessian import ( + "io" "reflect" "time" ) @@ -26,8 +27,13 @@ import ( perrors "github.com/pkg/errors" ) +func init() { + RegisterPOJO(&Date{}) + RegisterPOJO(&Time{}) +} + type JavaSqlTime interface { - SetTime(time int64) + SetTime(time time.Time) ValueOf(time string) (JavaSqlTime, error) JavaClassName() string UnixNano() int64 @@ -45,11 +51,8 @@ func (Date) JavaClassName() string { return "java.sql.Date" } -func (Date) Error() string { - return "encode java.sql.Date error" -} -func (Date) SetTime(time int64) { - panic("") +func (d *Date) SetTime(time time.Time) { + d.Time = time } func (Date) ValueOf(time string) (JavaSqlTime, error) { panic("") @@ -63,16 +66,12 @@ func (Time) JavaClassName() string { return "java.sql.Time" } -func (Time) Error() string { - return "encode java.sql.Time error" -} - func (t Time) time() time.Time { return t.Time } -func (Time) SetTime(time int64) { - panic("") +func (t *Time) SetTime(time time.Time) { + t.Time = time } func (Time) ValueOf(time string) (JavaSqlTime, error) { panic("") @@ -193,5 +192,28 @@ func (JavaSqlTimeSerializer) EncObject(e *Encoder, vv POJO) error { func (JavaSqlTimeSerializer) DecObject(d *Decoder, typ reflect.Type, cls classInfo) (interface{}, error) { - return nil, perrors.New("unexpected collection decode call") + if typ.Kind() != reflect.Struct { + return nil, perrors.Errorf("wrong type expect Struct but get:%s", typ.String()) + } + + vRef := reflect.New(typ) + // add pointer ref so that ref the same object + d.appendRefs(vRef.Interface()) + + tag, err := d.readByte() + if err == io.EOF { + return nil, err + } + date, err := d.decDate(int32(tag)) + if err != nil { + date = date + } + sqlTime := vRef.Interface() + + result, ok := sqlTime.(JavaSqlTime) + result.SetTime(date) + if !ok { + panic("result type is not sql time, please check the whether the conversion is ok") + } + return result, nil } diff --git a/java_sql_time_test.go b/java_sql_time_test.go index de6a2a15..c7635973 100644 --- a/java_sql_time_test.go +++ b/java_sql_time_test.go @@ -18,6 +18,7 @@ package hessian import ( + "reflect" "testing" "time" ) @@ -33,3 +34,22 @@ func TestJavaSqlTimeEncode(t *testing.T) { t.Log(time1.UnixNano() / 1000000) testJavaDecode(t, "javaSql_encode_time", time1) } + +func TestJavaSqlTimeDecode(t *testing.T) { + Bdate := "1997-01-01 13:15:46" + location, _ := time.ParseInLocation("2006-01-02 15:04:05", Bdate, time.Local) + time1 := Time{Time: location} + //t.Log(time1.UnixNano() / 1000000) + //testJavaDecode(t, "javaSql_encode_time", time1) + testDecodeFramework(t, "javaSql_decode_time", &time1) +} + +func TestName(t *testing.T) { + Bdate := "1997-01-01 13:15:46" + location, _ := time.ParseInLocation("2006-01-02 15:04:05", Bdate, time.Local) + time1 := Time{Time: location} + time2 := Time{Time: location} + r := time1 + expected := time2 + t.Log(reflect.DeepEqual(r, expected)) +} diff --git a/test_hessian/src/main/java/test/Hessian.java b/test_hessian/src/main/java/test/Hessian.java index a9596a26..c012885b 100644 --- a/test_hessian/src/main/java/test/Hessian.java +++ b/test_hessian/src/main/java/test/Hessian.java @@ -82,7 +82,7 @@ public static void main(String[] args) throws Exception { System.out.print(method.invoke(testJavaSqlTime, o)); } else { Method method = TestJavaSqlTime.class.getMethod(args[0]); - TestJavaSqlTime testJavaSqlTime = new TestJavaSqlTime(System.out); + TestJavaSqlTime testJavaSqlTime = new TestJavaSqlTime(); Object object = method.invoke(testJavaSqlTime); Hessian2Output output = new Hessian2Output(System.out); diff --git a/test_hessian/src/main/java/test/TestJavaSqlTime.java b/test_hessian/src/main/java/test/TestJavaSqlTime.java index 7632482f..cc4ce92e 100644 --- a/test_hessian/src/main/java/test/TestJavaSqlTime.java +++ b/test_hessian/src/main/java/test/TestJavaSqlTime.java @@ -17,32 +17,21 @@ package test; -import com.alibaba.com.caucho.hessian.io.Hessian2Output; - import java.io.IOException; -import java.io.OutputStream; import java.sql.Date; import java.sql.Time; import java.text.SimpleDateFormat; public class TestJavaSqlTime { - public void javaSql_decode_date() throws IOException { + public Object javaSql_decode_date() throws IOException { Date date = Date.valueOf("2020-08-09"); - output.writeObject(date); - output.flush(); - } - - public void javaSql_decode_time() throws IOException { - Time time = Time.valueOf("07:35:13"); - output.writeObject(time); - output.flush(); + return date; } - private Hessian2Output output; - - public TestJavaSqlTime(OutputStream os) { - output = new Hessian2Output(os); + public Object javaSql_decode_time() throws IOException { + Time time = new Time(852095746000L); + return time; } From a0458bd0e6307fc4c6ce61b062a9d6183d43f67a Mon Sep 17 00:00:00 2001 From: zhangshen023 <1292369127@qq.com> Date: Sat, 15 Aug 2020 23:03:24 +0800 Subject: [PATCH 03/15] java.sql.Time java.sql.Date --- java_sql_time.go | 85 +++++++++++-------- java_sql_time_test.go | 67 ++++++++++----- .../src/main/java/test/TestJavaSqlTime.java | 14 +-- 3 files changed, 97 insertions(+), 69 deletions(-) diff --git a/java_sql_time.go b/java_sql_time.go index f358c2cf..6d80e69f 100644 --- a/java_sql_time.go +++ b/java_sql_time.go @@ -33,29 +33,47 @@ func init() { } type JavaSqlTime interface { - SetTime(time time.Time) - ValueOf(time string) (JavaSqlTime, error) + ValueOf(timeStr string) error + setTime(time time.Time) JavaClassName() string - UnixNano() int64 time() time.Time } + type Date struct { time.Time } -func (d Date) time() time.Time { +func (d *Date) time() time.Time { return d.Time } +func (d *Date) setTime(time time.Time) { + d.Time = time +} + func (Date) JavaClassName() string { return "java.sql.Date" } -func (d *Date) SetTime(time time.Time) { +func (d *Date) ValueOf(dateStr string) error { + time, err := time.Parse("2006-01-02", dateStr) + if err != nil { + return err + } d.Time = time + return nil +} + +func (d *Date) Year() int { + return d.Time.Year() } -func (Date) ValueOf(time string) (JavaSqlTime, error) { - panic("") + +func (d *Date) Month() time.Month { + return d.Time.Month() +} + +func (d *Date) Day() int { + return d.Time.Day() } type Time struct { @@ -70,27 +88,36 @@ func (t Time) time() time.Time { return t.Time } -func (t *Time) SetTime(time time.Time) { +func (t *Time) Hours() int { + return t.Time.Hour() +} + +func (t *Time) Minutes() int { + return t.Time.Minute() +} + +func (t *Time) Seconds() int { + return t.Time.Second() +} + +func (t *Time) setTime(time time.Time) { t.Time = time } -func (Time) ValueOf(time string) (JavaSqlTime, error) { - panic("") + +func (t *Time) ValueOf(timeStr string) error { + time, err := time.Parse("15:04:05", timeStr) + if err != nil { + return err + } + t.Time = time + return nil } var javaSqlTimeTypeMap = make(map[string]reflect.Type, 16) func SetJavaSqlTimeSerialize(time JavaSqlTime) { name := time.JavaClassName() - v := reflect.ValueOf(time) - var typ reflect.Type - switch v.Kind() { - case reflect.Struct: - typ = v.Type() - case reflect.Ptr: - typ = v.Elem().Type() - default: - typ = reflect.TypeOf(time) - } + var typ = reflect.TypeOf(time) SetSerializer(name, JavaSqlTimeSerializer{}) //RegisterPOJO(time) javaSqlTimeTypeMap[name] = typ @@ -157,14 +184,6 @@ func (JavaSqlTimeSerializer) EncObject(e *Encoder, vv POJO) error { e.buffer = e.buffer[0 : len(e.buffer)-1] } - //if idx < -1 { - // e.buffer = encString(e.buffer, "value") - // var bytes []byte - // bytes = append(bytes, PackInt64(v.UnixNano()/1e6)...) - // e.buffer = encDateInMs(bytes, v) - // e.buffer = append(e.buffer, BC_END) - //} else - //{ if idx == -1 { e.buffer = encInt32(e.buffer, 1) e.buffer = encString(e.buffer, "value") @@ -176,14 +195,6 @@ func (JavaSqlTimeSerializer) EncObject(e *Encoder, vv POJO) error { e.buffer = encByte(e.buffer, BC_OBJECT) e.buffer = encInt32(e.buffer, int32(idx1)) } - //if (ref == -1) { - // out.writeInt(1); - // out.writeString("value"); - // out.writeObjectBegin(cl.getName()); - //} - //} - //var bytes []byte - //bytes = append(bytes, PackInt64(v.UnixNano()/1e6)...) e.buffer = encDateInMs(e.buffer, v.time()) } @@ -211,7 +222,7 @@ func (JavaSqlTimeSerializer) DecObject(d *Decoder, typ reflect.Type, cls classIn sqlTime := vRef.Interface() result, ok := sqlTime.(JavaSqlTime) - result.SetTime(date) + result.setTime(date) if !ok { panic("result type is not sql time, please check the whether the conversion is ok") } diff --git a/java_sql_time_test.go b/java_sql_time_test.go index c7635973..6f13540c 100644 --- a/java_sql_time_test.go +++ b/java_sql_time_test.go @@ -18,7 +18,7 @@ package hessian import ( - "reflect" + "github.com/stretchr/testify/assert" "testing" "time" ) @@ -27,29 +27,56 @@ func init() { SetJavaSqlTimeSerialize(&Date{}) SetJavaSqlTimeSerialize(&Time{}) } + +// test local time between go and java +// go encode +// java decode func TestJavaSqlTimeEncode(t *testing.T) { - Bdate := "1997-01-01 13:15:46" - location, _ := time.ParseInLocation("2006-01-02 15:04:05", Bdate, time.Local) - time1 := Time{Time: location} - t.Log(time1.UnixNano() / 1000000) - testJavaDecode(t, "javaSql_encode_time", time1) + location, _ := time.ParseInLocation("2006-01-02 15:04:05", "1997-01-01 13:15:46", time.Local) + testSqlTime := Time{Time: location} + testJavaDecode(t, "javaSql_encode_time", &testSqlTime) + + location, _ = time.ParseInLocation("2006-01-02 15:04:05", "2020-08-09 00:00:00", time.Local) + testSqlDate := Time{Time: location} + testJavaDecode(t, "javaSql_encode_date", &testSqlDate) } +// test local time between go and java +// java encode +// go decode func TestJavaSqlTimeDecode(t *testing.T) { - Bdate := "1997-01-01 13:15:46" - location, _ := time.ParseInLocation("2006-01-02 15:04:05", Bdate, time.Local) - time1 := Time{Time: location} - //t.Log(time1.UnixNano() / 1000000) - //testJavaDecode(t, "javaSql_encode_time", time1) - testDecodeFramework(t, "javaSql_decode_time", &time1) + location, _ := time.ParseInLocation("2006-01-02 15:04:05", "1997-01-01 13:15:46", time.Local) + testSqlTime := Time{Time: location} + testDecodeFramework(t, "javaSql_decode_time", &testSqlTime) + + location, _ = time.ParseInLocation("2006-01-02 15:04:05", "2020-08-09 00:00:00", time.Local) + testDateTime := Date{Time: location} + testDecodeFramework(t, "javaSql_decode_date", &testDateTime) } -func TestName(t *testing.T) { - Bdate := "1997-01-01 13:15:46" - location, _ := time.ParseInLocation("2006-01-02 15:04:05", Bdate, time.Local) - time1 := Time{Time: location} - time2 := Time{Time: location} - r := time1 - expected := time2 - t.Log(reflect.DeepEqual(r, expected)) +// test local time between go and go +// go encode +// go decode +func TestJavaSqlTimeWithGo(t *testing.T) { + location, _ := time.ParseInLocation("2006-01-02 15:04:05", "1997-01-01 13:15:46", time.Local) + sqlTime := Time{Time: location} + e := NewEncoder() + e.Encode(&sqlTime) + if len(e.Buffer()) == 0 { + t.Fail() + } + d := NewDecoder(e.Buffer()) + resultSqlTime, _ := d.Decode() + assert.Equal(t, &sqlTime, resultSqlTime) + + location, _ = time.ParseInLocation("2006-01-02 15:04:05", "2020-08-09 00:00:00", time.Local) + sqlDate := Date{Time: location} + e = NewEncoder() + e.Encode(&sqlDate) + if len(e.Buffer()) == 0 { + t.Fail() + } + d = NewDecoder(e.Buffer()) + resultSqlDate, _ := d.Decode() + assert.Equal(t, &sqlDate, resultSqlDate) } diff --git a/test_hessian/src/main/java/test/TestJavaSqlTime.java b/test_hessian/src/main/java/test/TestJavaSqlTime.java index cc4ce92e..4dc38bd8 100644 --- a/test_hessian/src/main/java/test/TestJavaSqlTime.java +++ b/test_hessian/src/main/java/test/TestJavaSqlTime.java @@ -17,19 +17,17 @@ package test; -import java.io.IOException; import java.sql.Date; import java.sql.Time; -import java.text.SimpleDateFormat; public class TestJavaSqlTime { - public Object javaSql_decode_date() throws IOException { + public Object javaSql_decode_date() { Date date = Date.valueOf("2020-08-09"); return date; } - public Object javaSql_decode_time() throws IOException { + public Object javaSql_decode_time() { Time time = new Time(852095746000L); return time; } @@ -38,14 +36,6 @@ public Object javaSql_decode_time() throws IOException { public TestJavaSqlTime() { } - public static void main(String[] args) { - long time = 894621091000L; - - time -= time % 60000L; - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - System.out.println(sdf.format(new java.util.Date(time))); - } - public static Object javaSql_encode_time(Object v) { return v.equals(new Time(852095746000L)); } From a9869e3a11348d0d652027a4c2129bd0d04f6c1c Mon Sep 17 00:00:00 2001 From: zhangshen023 <1292369127@qq.com> Date: Sun, 16 Aug 2020 15:24:32 +0800 Subject: [PATCH 04/15] fix errors --- decode_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decode_test.go b/decode_test.go index 024a1ff4..87f46040 100644 --- a/decode_test.go +++ b/decode_test.go @@ -76,7 +76,7 @@ func getJavaReply(method, className string) []byte { if className != "" { cmdArgs = append(cmdArgs, className) } - cmd := exec.Command("/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home/bin/java", cmdArgs...) + cmd := exec.Command("java", cmdArgs...) out, err := cmd.Output() if err != nil { log.Fatal(err) From 8935be6c503b146dd49bea2d674197c6426f277e Mon Sep 17 00:00:00 2001 From: zhangshen023 <1292369127@qq.com> Date: Sun, 16 Aug 2020 17:19:19 +0800 Subject: [PATCH 05/15] compare time with unix nano --- java_sql_time_test.go | 35 +++++++++++++------ .../src/main/java/test/TestJavaSqlTime.java | 11 +++--- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/java_sql_time_test.go b/java_sql_time_test.go index 6f13540c..c718d790 100644 --- a/java_sql_time_test.go +++ b/java_sql_time_test.go @@ -32,12 +32,12 @@ func init() { // go encode // java decode func TestJavaSqlTimeEncode(t *testing.T) { - location, _ := time.ParseInLocation("2006-01-02 15:04:05", "1997-01-01 13:15:46", time.Local) - testSqlTime := Time{Time: location} + sqlTime := time.Date(1997, 1, 1, 13, 15, 46, 0, time.UTC) + testSqlTime := Time{Time: sqlTime} testJavaDecode(t, "javaSql_encode_time", &testSqlTime) - location, _ = time.ParseInLocation("2006-01-02 15:04:05", "2020-08-09 00:00:00", time.Local) - testSqlDate := Time{Time: location} + sqlDate := time.Date(2020, 8, 9, 0, 0, 0, 0, time.UTC) + testSqlDate := Date{Time: sqlDate} testJavaDecode(t, "javaSql_encode_date", &testSqlDate) } @@ -45,13 +45,28 @@ func TestJavaSqlTimeEncode(t *testing.T) { // java encode // go decode func TestJavaSqlTimeDecode(t *testing.T) { - location, _ := time.ParseInLocation("2006-01-02 15:04:05", "1997-01-01 13:15:46", time.Local) - testSqlTime := Time{Time: location} - testDecodeFramework(t, "javaSql_decode_time", &testSqlTime) + sqlTime := time.Date(1997, 1, 1, 13, 15, 46, 0, time.UTC) + testSqlTime := Time{Time: sqlTime} + testDecodeJavaSqlTime(t, "javaSql_decode_time", &testSqlTime) - location, _ = time.ParseInLocation("2006-01-02 15:04:05", "2020-08-09 00:00:00", time.Local) - testDateTime := Date{Time: location} - testDecodeFramework(t, "javaSql_decode_date", &testDateTime) + sqlDate := time.Date(2020, 8, 9, 0, 0, 0, 0, time.UTC) + testDateTime := Date{Time: sqlDate} + testDecodeJavaSqlTime(t, "javaSql_decode_date", &testDateTime) +} + +func testDecodeJavaSqlTime(t *testing.T, method string, expected JavaSqlTime) { + r, e := decodeJavaResponse(method, "", false) + if e != nil { + t.Errorf("%s: decode fail with error %v", method, e) + return + } + + resultSqlTime, ok := r.(JavaSqlTime) + + if !ok { + t.Errorf("got error type:%v", r) + } + assert.Equal(t, resultSqlTime.time().UnixNano(), expected.time().UnixNano()) } // test local time between go and go diff --git a/test_hessian/src/main/java/test/TestJavaSqlTime.java b/test_hessian/src/main/java/test/TestJavaSqlTime.java index 4dc38bd8..5316a881 100644 --- a/test_hessian/src/main/java/test/TestJavaSqlTime.java +++ b/test_hessian/src/main/java/test/TestJavaSqlTime.java @@ -23,13 +23,11 @@ public class TestJavaSqlTime { public Object javaSql_decode_date() { - Date date = Date.valueOf("2020-08-09"); - return date; + return new Date(1596931200000L); } public Object javaSql_decode_time() { - Time time = new Time(852095746000L); - return time; + return new Time(852124546000L); } @@ -37,12 +35,11 @@ public TestJavaSqlTime() { } public static Object javaSql_encode_time(Object v) { - return v.equals(new Time(852095746000L)); + return v.equals(new Time(852124546000L)); } public boolean javaSql_encode_date(Object v) { - Date date = Date.valueOf("2020-08-09"); - return date.equals(v); + return v.equals(new Date(1596931200000L)); } } From 9de5cb64e58de5e75e160ed164612903a8c0d931 Mon Sep 17 00:00:00 2001 From: zhangshen023 <1292369127@qq.com> Date: Mon, 17 Aug 2020 08:04:58 +0800 Subject: [PATCH 06/15] add comment and optimize --- java_sql_time.go | 85 ++++++++++++++++++++++++------------------- java_sql_time_test.go | 9 +++-- 2 files changed, 52 insertions(+), 42 deletions(-) diff --git a/java_sql_time.go b/java_sql_time.go index 6d80e69f..48209b3f 100644 --- a/java_sql_time.go +++ b/java_sql_time.go @@ -33,9 +33,12 @@ func init() { } type JavaSqlTime interface { + // ValueOf parse time string which format likes '2006-01-02 15:04:05' ValueOf(timeStr string) error + // setTime for decode time setTime(time time.Time) JavaClassName() string + // time used to time time() time.Time } @@ -64,14 +67,17 @@ func (d *Date) ValueOf(dateStr string) error { return nil } +// nolint func (d *Date) Year() int { return d.Time.Year() } +// nolint func (d *Date) Month() time.Month { return d.Time.Month() } +// nolint func (d *Date) Day() int { return d.Time.Day() } @@ -88,15 +94,18 @@ func (t Time) time() time.Time { return t.Time } -func (t *Time) Hours() int { +// nolint +func (t *Time) Hour() int { return t.Time.Hour() } -func (t *Time) Minutes() int { +// nolint +func (t *Time) Minute() int { return t.Time.Minute() } -func (t *Time) Seconds() int { +// nolint +func (t *Time) Second() int { return t.Time.Second() } @@ -115,39 +124,48 @@ func (t *Time) ValueOf(timeStr string) error { var javaSqlTimeTypeMap = make(map[string]reflect.Type, 16) +// SetJavaSqlTimeSerialize register serializer for java.sql.Time & java.sql.Date func SetJavaSqlTimeSerialize(time JavaSqlTime) { name := time.JavaClassName() var typ = reflect.TypeOf(time) SetSerializer(name, JavaSqlTimeSerializer{}) - //RegisterPOJO(time) javaSqlTimeTypeMap[name] = typ } +// nolint func getJavaSqlTimeSerialize(name string) reflect.Type { return javaSqlTimeTypeMap[name] } +// JavaSqlTimeSerializer used to encode & decode java.sql.Time & java.sql.Date type JavaSqlTimeSerializer struct { } +// nolint func (JavaSqlTimeSerializer) EncObject(e *Encoder, vv POJO) error { var ( - idx int - idx1 int - i int - err error - clsDef classInfo + i int + idx int + err error + clsDef classInfo + className string + ptrV reflect.Value ) - v, ok := vv.(JavaSqlTime) + + // ensure ptrV is pointer to know vv is type JavaSqlTime or not + ptrV = reflect.ValueOf(vv) + if reflect.TypeOf(vv).Kind() != reflect.Ptr { + ptrV = PackPtr(ptrV) + } + v, ok := ptrV.Interface().(JavaSqlTime) if !ok { return perrors.New("can not be converted into java sql time object") } - className := v.JavaClassName() + className = v.JavaClassName() if className == "" { return perrors.New("class name empty") } - tValue := reflect.ValueOf(vv) // check ref if n, ok := e.checkRefMap(tValue); ok { @@ -163,44 +181,35 @@ func (JavaSqlTimeSerializer) EncObject(e *Encoder, vv POJO) error { break } } - if idx == -1 { - idx1, ok = checkPOJORegistry(typeof(v)) + idx, ok = checkPOJORegistry(typeof(vv)) if !ok { - if reflect.TypeOf(v).Implements(javaEnumType) { - idx1 = RegisterJavaEnum(v.(POJOEnum)) - } else { - idx1 = RegisterPOJO(v) - } + idx = RegisterPOJO(v) } - _, clsDef, err = getStructDefByIndex(idx1) + _, clsDef, err = getStructDefByIndex(idx) if err != nil { return perrors.WithStack(err) } - - i = len(e.classInfoList) + idx = len(e.classInfoList) e.classInfoList = append(e.classInfoList, clsDef) e.buffer = append(e.buffer, clsDef.buffer...) - e.buffer = e.buffer[0 : len(e.buffer)-1] } - - if idx == -1 { - e.buffer = encInt32(e.buffer, 1) - e.buffer = encString(e.buffer, "value") - - // write object instance - if byte(i) <= OBJECT_DIRECT_MAX { - e.buffer = encByte(e.buffer, byte(i)+BC_OBJECT_DIRECT) - } else { - e.buffer = encByte(e.buffer, BC_OBJECT) - e.buffer = encInt32(e.buffer, int32(idx1)) - } - e.buffer = encDateInMs(e.buffer, v.time()) + e.buffer = e.buffer[0 : len(e.buffer)-1] + e.buffer = encInt32(e.buffer, 1) + e.buffer = encString(e.buffer, "value") + + // write object instance + if byte(idx) <= OBJECT_DIRECT_MAX { + e.buffer = encByte(e.buffer, byte(idx)+BC_OBJECT_DIRECT) + } else { + e.buffer = encByte(e.buffer, BC_OBJECT) + e.buffer = encInt32(e.buffer, int32(idx)) } - + e.buffer = encDateInMs(e.buffer, v.time()) return nil } +// nolint func (JavaSqlTimeSerializer) DecObject(d *Decoder, typ reflect.Type, cls classInfo) (interface{}, error) { if typ.Kind() != reflect.Struct { @@ -217,7 +226,7 @@ func (JavaSqlTimeSerializer) DecObject(d *Decoder, typ reflect.Type, cls classIn } date, err := d.decDate(int32(tag)) if err != nil { - date = date + return nil, perrors.WithStack(err) } sqlTime := vRef.Interface() diff --git a/java_sql_time_test.go b/java_sql_time_test.go index c718d790..8c1df5d9 100644 --- a/java_sql_time_test.go +++ b/java_sql_time_test.go @@ -18,11 +18,14 @@ package hessian import ( - "github.com/stretchr/testify/assert" "testing" "time" ) +import ( + "github.com/stretchr/testify/assert" +) + func init() { SetJavaSqlTimeSerialize(&Date{}) SetJavaSqlTimeSerialize(&Time{}) @@ -34,7 +37,7 @@ func init() { func TestJavaSqlTimeEncode(t *testing.T) { sqlTime := time.Date(1997, 1, 1, 13, 15, 46, 0, time.UTC) testSqlTime := Time{Time: sqlTime} - testJavaDecode(t, "javaSql_encode_time", &testSqlTime) + testJavaDecode(t, "javaSql_encode_time", testSqlTime) sqlDate := time.Date(2020, 8, 9, 0, 0, 0, 0, time.UTC) testSqlDate := Date{Time: sqlDate} @@ -60,9 +63,7 @@ func testDecodeJavaSqlTime(t *testing.T, method string, expected JavaSqlTime) { t.Errorf("%s: decode fail with error %v", method, e) return } - resultSqlTime, ok := r.(JavaSqlTime) - if !ok { t.Errorf("got error type:%v", r) } From ceba5b3a0821b685e7c99f985523f09d72aa4868 Mon Sep 17 00:00:00 2001 From: shen Date: Mon, 17 Aug 2020 18:09:58 +0800 Subject: [PATCH 07/15] refactor code --- java_sql_time.go | 109 ++++----------------------------- java_sql_time/date.go | 43 +++++++++++++ java_sql_time/java_sql_time.go | 13 ++++ java_sql_time/time.go | 43 +++++++++++++ java_sql_time_test.go | 26 ++++---- 5 files changed, 125 insertions(+), 109 deletions(-) create mode 100644 java_sql_time/date.go create mode 100644 java_sql_time/java_sql_time.go create mode 100644 java_sql_time/time.go diff --git a/java_sql_time.go b/java_sql_time.go index 48209b3f..71c61e4d 100644 --- a/java_sql_time.go +++ b/java_sql_time.go @@ -20,112 +20,25 @@ package hessian import ( "io" "reflect" - "time" ) import ( perrors "github.com/pkg/errors" ) -func init() { - RegisterPOJO(&Date{}) - RegisterPOJO(&Time{}) -} - -type JavaSqlTime interface { - // ValueOf parse time string which format likes '2006-01-02 15:04:05' - ValueOf(timeStr string) error - // setTime for decode time - setTime(time time.Time) - JavaClassName() string - // time used to time - time() time.Time -} - -type Date struct { - time.Time -} - -func (d *Date) time() time.Time { - return d.Time -} - -func (d *Date) setTime(time time.Time) { - d.Time = time -} - -func (Date) JavaClassName() string { - return "java.sql.Date" -} - -func (d *Date) ValueOf(dateStr string) error { - time, err := time.Parse("2006-01-02", dateStr) - if err != nil { - return err - } - d.Time = time - return nil -} - -// nolint -func (d *Date) Year() int { - return d.Time.Year() -} - -// nolint -func (d *Date) Month() time.Month { - return d.Time.Month() -} - -// nolint -func (d *Date) Day() int { - return d.Time.Day() -} - -type Time struct { - time.Time -} - -func (Time) JavaClassName() string { - return "java.sql.Time" -} - -func (t Time) time() time.Time { - return t.Time -} - -// nolint -func (t *Time) Hour() int { - return t.Time.Hour() -} - -// nolint -func (t *Time) Minute() int { - return t.Time.Minute() -} - -// nolint -func (t *Time) Second() int { - return t.Time.Second() -} - -func (t *Time) setTime(time time.Time) { - t.Time = time -} +import ( + "github.com/apache/dubbo-go-hessian2/java_sql_time" +) -func (t *Time) ValueOf(timeStr string) error { - time, err := time.Parse("15:04:05", timeStr) - if err != nil { - return err - } - t.Time = time - return nil +func init() { + RegisterPOJO(&java_sql_time.Date{}) + RegisterPOJO(&java_sql_time.Time{}) } var javaSqlTimeTypeMap = make(map[string]reflect.Type, 16) // SetJavaSqlTimeSerialize register serializer for java.sql.Time & java.sql.Date -func SetJavaSqlTimeSerialize(time JavaSqlTime) { +func SetJavaSqlTimeSerialize(time java_sql_time.JavaSqlTime) { name := time.JavaClassName() var typ = reflect.TypeOf(time) SetSerializer(name, JavaSqlTimeSerializer{}) @@ -158,7 +71,7 @@ func (JavaSqlTimeSerializer) EncObject(e *Encoder, vv POJO) error { if reflect.TypeOf(vv).Kind() != reflect.Ptr { ptrV = PackPtr(ptrV) } - v, ok := ptrV.Interface().(JavaSqlTime) + v, ok := ptrV.Interface().(java_sql_time.JavaSqlTime) if !ok { return perrors.New("can not be converted into java sql time object") } @@ -205,7 +118,7 @@ func (JavaSqlTimeSerializer) EncObject(e *Encoder, vv POJO) error { e.buffer = encByte(e.buffer, BC_OBJECT) e.buffer = encInt32(e.buffer, int32(idx)) } - e.buffer = encDateInMs(e.buffer, v.time()) + e.buffer = encDateInMs(e.buffer, v.GetTime()) return nil } @@ -230,8 +143,8 @@ func (JavaSqlTimeSerializer) DecObject(d *Decoder, typ reflect.Type, cls classIn } sqlTime := vRef.Interface() - result, ok := sqlTime.(JavaSqlTime) - result.setTime(date) + result, ok := sqlTime.(java_sql_time.JavaSqlTime) + result.SetTime(date) if !ok { panic("result type is not sql time, please check the whether the conversion is ok") } diff --git a/java_sql_time/date.go b/java_sql_time/date.go new file mode 100644 index 00000000..84aea269 --- /dev/null +++ b/java_sql_time/date.go @@ -0,0 +1,43 @@ +package java_sql_time + +import "time" + +type Date struct { + time.Time +} + +func (d *Date) GetTime() time.Time { + return d.Time +} + +func (d *Date) SetTime(time time.Time) { + d.Time = time +} + +func (Date) JavaClassName() string { + return "java.sql.Date" +} + +func (d *Date) ValueOf(dateStr string) error { + time, err := time.Parse("2006-01-02", dateStr) + if err != nil { + return err + } + d.Time = time + return nil +} + +// nolint +func (d *Date) Year() int { + return d.Time.Year() +} + +// nolint +func (d *Date) Month() time.Month { + return d.Time.Month() +} + +// nolint +func (d *Date) Day() int { + return d.Time.Day() +} diff --git a/java_sql_time/java_sql_time.go b/java_sql_time/java_sql_time.go new file mode 100644 index 00000000..e35f571a --- /dev/null +++ b/java_sql_time/java_sql_time.go @@ -0,0 +1,13 @@ +package java_sql_time + +import "time" + +type JavaSqlTime interface { + // ValueOf parse time string which format likes '2006-01-02 15:04:05' + ValueOf(timeStr string) error + // SetTime for decode time + SetTime(time time.Time) + JavaClassName() string + // GetTime used to time + GetTime() time.Time +} diff --git a/java_sql_time/time.go b/java_sql_time/time.go new file mode 100644 index 00000000..b4fab871 --- /dev/null +++ b/java_sql_time/time.go @@ -0,0 +1,43 @@ +package java_sql_time + +import "time" + +type Time struct { + time.Time +} + +func (Time) JavaClassName() string { + return "java.sql.Time" +} + +func (t *Time) GetTime() time.Time { + return t.Time +} + +// nolint +func (t *Time) Hour() int { + return t.Time.Hour() +} + +// nolint +func (t *Time) Minute() int { + return t.Time.Minute() +} + +// nolint +func (t *Time) Second() int { + return t.Time.Second() +} + +func (t *Time) SetTime(time time.Time) { + t.Time = time +} + +func (t *Time) ValueOf(timeStr string) error { + time, err := time.Parse("15:04:05", timeStr) + if err != nil { + return err + } + t.Time = time + return nil +} diff --git a/java_sql_time_test.go b/java_sql_time_test.go index 8c1df5d9..345cfab1 100644 --- a/java_sql_time_test.go +++ b/java_sql_time_test.go @@ -26,9 +26,13 @@ import ( "github.com/stretchr/testify/assert" ) +import ( + "github.com/apache/dubbo-go-hessian2/java_sql_time" +) + func init() { - SetJavaSqlTimeSerialize(&Date{}) - SetJavaSqlTimeSerialize(&Time{}) + SetJavaSqlTimeSerialize(&java_sql_time.Date{}) + SetJavaSqlTimeSerialize(&java_sql_time.Time{}) } // test local time between go and java @@ -36,11 +40,11 @@ func init() { // java decode func TestJavaSqlTimeEncode(t *testing.T) { sqlTime := time.Date(1997, 1, 1, 13, 15, 46, 0, time.UTC) - testSqlTime := Time{Time: sqlTime} + testSqlTime := java_sql_time.Time{Time: sqlTime} testJavaDecode(t, "javaSql_encode_time", testSqlTime) sqlDate := time.Date(2020, 8, 9, 0, 0, 0, 0, time.UTC) - testSqlDate := Date{Time: sqlDate} + testSqlDate := java_sql_time.Date{Time: sqlDate} testJavaDecode(t, "javaSql_encode_date", &testSqlDate) } @@ -49,25 +53,25 @@ func TestJavaSqlTimeEncode(t *testing.T) { // go decode func TestJavaSqlTimeDecode(t *testing.T) { sqlTime := time.Date(1997, 1, 1, 13, 15, 46, 0, time.UTC) - testSqlTime := Time{Time: sqlTime} + testSqlTime := java_sql_time.Time{Time: sqlTime} testDecodeJavaSqlTime(t, "javaSql_decode_time", &testSqlTime) sqlDate := time.Date(2020, 8, 9, 0, 0, 0, 0, time.UTC) - testDateTime := Date{Time: sqlDate} + testDateTime := java_sql_time.Date{Time: sqlDate} testDecodeJavaSqlTime(t, "javaSql_decode_date", &testDateTime) } -func testDecodeJavaSqlTime(t *testing.T, method string, expected JavaSqlTime) { +func testDecodeJavaSqlTime(t *testing.T, method string, expected java_sql_time.JavaSqlTime) { r, e := decodeJavaResponse(method, "", false) if e != nil { t.Errorf("%s: decode fail with error %v", method, e) return } - resultSqlTime, ok := r.(JavaSqlTime) + resultSqlTime, ok := r.(java_sql_time.JavaSqlTime) if !ok { t.Errorf("got error type:%v", r) } - assert.Equal(t, resultSqlTime.time().UnixNano(), expected.time().UnixNano()) + assert.Equal(t, resultSqlTime.GetTime().UnixNano(), expected.GetTime().UnixNano()) } // test local time between go and go @@ -75,7 +79,7 @@ func testDecodeJavaSqlTime(t *testing.T, method string, expected JavaSqlTime) { // go decode func TestJavaSqlTimeWithGo(t *testing.T) { location, _ := time.ParseInLocation("2006-01-02 15:04:05", "1997-01-01 13:15:46", time.Local) - sqlTime := Time{Time: location} + sqlTime := java_sql_time.Time{Time: location} e := NewEncoder() e.Encode(&sqlTime) if len(e.Buffer()) == 0 { @@ -86,7 +90,7 @@ func TestJavaSqlTimeWithGo(t *testing.T) { assert.Equal(t, &sqlTime, resultSqlTime) location, _ = time.ParseInLocation("2006-01-02 15:04:05", "2020-08-09 00:00:00", time.Local) - sqlDate := Date{Time: location} + sqlDate := java_sql_time.Date{Time: location} e = NewEncoder() e.Encode(&sqlDate) if len(e.Buffer()) == 0 { From 68ebf21a89a3444c16f871eb0d801a5ea49e05ae Mon Sep 17 00:00:00 2001 From: zhangshen023 <1292369127@qq.com> Date: Mon, 17 Aug 2020 21:53:48 +0800 Subject: [PATCH 08/15] add license --- java_sql_time/date.go | 17 +++++++++++++++++ java_sql_time/java_sql_time.go | 17 +++++++++++++++++ java_sql_time/time.go | 17 +++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/java_sql_time/date.go b/java_sql_time/date.go index 84aea269..3fdc4006 100644 --- a/java_sql_time/date.go +++ b/java_sql_time/date.go @@ -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 java_sql_time import "time" diff --git a/java_sql_time/java_sql_time.go b/java_sql_time/java_sql_time.go index e35f571a..2f738644 100644 --- a/java_sql_time/java_sql_time.go +++ b/java_sql_time/java_sql_time.go @@ -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 java_sql_time import "time" diff --git a/java_sql_time/time.go b/java_sql_time/time.go index b4fab871..709cec8e 100644 --- a/java_sql_time/time.go +++ b/java_sql_time/time.go @@ -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 java_sql_time import "time" From 9b32032784f6887c4e922b9a03a95dab67b71186 Mon Sep 17 00:00:00 2001 From: cvictory Date: Wed, 19 Aug 2020 14:03:03 +0800 Subject: [PATCH 09/15] [refer dubbo 2.7.6] attachment type from map[string]stiring to map[string]interface{} (#218) * support attachment from map[sting]string to map[string]interface{} in invocation and result (refer to dubbo java 2.7.6) --- hessian.go | 4 ++-- hessian_test.go | 15 +++++++++++++-- request.go | 17 +++++++---------- request_test.go | 8 ++++---- response.go | 23 ++++++++++++++--------- response_test.go | 5 ++++- 6 files changed, 44 insertions(+), 28 deletions(-) diff --git a/hessian.go b/hessian.go index d4fb135c..6527604d 100644 --- a/hessian.go +++ b/hessian.go @@ -218,7 +218,7 @@ func (h *HessianCodec) ReadBody(rspObj interface{}) error { } // ignore body, but only read attachments -func (h *HessianCodec) ReadAttachments() (map[string]string, error) { +func (h *HessianCodec) ReadAttachments() (map[string]interface{}, error) { if h.reader.Buffered() < h.bodyLen { return nil, ErrBodyNotEnough } @@ -237,7 +237,7 @@ func (h *HessianCodec) ReadAttachments() (map[string]string, error) { if err = unpackRequestBody(NewDecoderWithSkip(buf[:]), rspObj); err != nil { return nil, perrors.WithStack(err) } - return rspObj[6].(map[string]string), nil + return rspObj[6].(map[string]interface{}), nil case PackageResponse: rspObj := &Response{} if err = unpackResponseBody(NewDecoderWithSkip(buf[:]), rspObj); err != nil { diff --git a/hessian_test.go b/hessian_test.go index e466cf2d..6f89f0b9 100644 --- a/hessian_test.go +++ b/hessian_test.go @@ -192,14 +192,14 @@ func TestRequest(t *testing.T) { } func TestHessianCodec_ReadAttachments(t *testing.T) { + RegisterPOJO(&AttachObject{}) body := &Response{ RspObj: &CaseB{A: "A", B: CaseA{A: "a", B: 1, C: Case{A: "c", B: 2}}}, Exception: nil, - Attachments: map[string]string{DUBBO_VERSION_KEY: "2.6.4"}, + Attachments: map[string]interface{}{DUBBO_VERSION_KEY: "2.6.4", "att": AttachObject{Id: 23, Name: "haha"}}, } resp, err := doTestHessianEncodeHeader(t, PackageResponse, Response_OK, body) assert.NoError(t, err) - unRegisterPOJOs(&CaseB{}, &CaseA{}) codecR1 := NewHessianCodec(bufio.NewReader(bytes.NewReader(resp))) codecR2 := NewHessianCodec(bufio.NewReader(bytes.NewReader(resp))) @@ -214,6 +214,17 @@ func TestHessianCodec_ReadAttachments(t *testing.T) { attrs, err := codecR2.ReadAttachments() assert.NoError(t, err) assert.Equal(t, "2.6.4", attrs[DUBBO_VERSION_KEY]) + assert.Equal(t, AttachObject{Id: 23, Name: "haha"}, *(attrs["att"].(*AttachObject))) + assert.NotEqual(t, AttachObject{Id: 24, Name: "nohaha"}, *(attrs["att"].(*AttachObject))) t.Log(attrs) } + +type AttachObject struct { + Id int32 + Name string `dubbo:name` +} + +func (AttachObject) JavaClassName() string { + return "com.test.Test" +} diff --git a/request.go b/request.go index 82c1ef5e..c668fcbe 100644 --- a/request.go +++ b/request.go @@ -151,13 +151,13 @@ func getArgsTypeList(args []interface{}) (string, error) { type Request struct { Params interface{} - Attachments map[string]string + Attachments map[string]interface{} } // NewRequest create a new Request -func NewRequest(params interface{}, atta map[string]string) *Request { +func NewRequest(params interface{}, atta map[string]interface{}) *Request { if atta == nil { - atta = make(map[string]string) + atta = make(map[string]interface{}) } return &Request{ Params: params, @@ -327,25 +327,22 @@ func unpackRequestBody(decoder *Decoder, reqObj interface{}) error { } if v, ok := attachments.(map[interface{}]interface{}); ok { v[DUBBO_VERSION_KEY] = dubboVersion - req[6] = ToMapStringString(v) + req[6] = ToMapStringInterface(v) return nil } return perrors.Errorf("get wrong attachments: %+v", attachments) } -func ToMapStringString(origin map[interface{}]interface{}) map[string]string { - dest := make(map[string]string) +func ToMapStringInterface(origin map[interface{}]interface{}) map[string]interface{} { + dest := make(map[string]interface{}) for k, v := range origin { if kv, ok := k.(string); ok { if v == nil { dest[kv] = "" continue } - - if vv, ok := v.(string); ok { - dest[kv] = vv - } + dest[kv] = v } } return dest diff --git a/request_test.go b/request_test.go index 2b7f1f33..37ec48df 100644 --- a/request_test.go +++ b/request_test.go @@ -129,7 +129,7 @@ func TestIssue192(t *testing.T) { tests := []struct { name string args args - want map[string]string + want map[string]interface{} }{ { name: "not null", @@ -140,7 +140,7 @@ func TestIssue192(t *testing.T) { "": "", }, }, - want: map[string]string{ + want: map[string]interface{}{ "1": "", "2": "3", "": "", @@ -149,8 +149,8 @@ func TestIssue192(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := ToMapStringString(tt.args.origin); !reflect.DeepEqual(got, tt.want) { - t.Errorf("ToMapStringString() = %v, want %v", got, tt.want) + if got := ToMapStringInterface(tt.args.origin); !reflect.DeepEqual(got, tt.want) { + t.Errorf("ToMapStringInterface() = %v, want %v", got, tt.want) } }) } diff --git a/response.go b/response.go index 0b2a97f6..910323b8 100644 --- a/response.go +++ b/response.go @@ -37,13 +37,13 @@ import ( type Response struct { RspObj interface{} Exception error - Attachments map[string]string + Attachments map[string]interface{} } // NewResponse create a new Response -func NewResponse(rspObj interface{}, exception error, attachments map[string]string) *Response { +func NewResponse(rspObj interface{}, exception error, attachments map[string]interface{}) *Response { if attachments == nil { - attachments = make(map[string]string) + attachments = make(map[string]interface{}) } return &Response{ RspObj: rspObj, @@ -176,7 +176,7 @@ func unpackResponseBody(decoder *Decoder, resp interface{}) error { return perrors.WithStack(err) } if v, ok := attachments.(map[interface{}]interface{}); ok { - atta := ToMapStringString(v) + atta := ToMapStringInterface(v) response.Attachments = atta } else { return perrors.Errorf("get wrong attachments: %+v", attachments) @@ -201,7 +201,7 @@ func unpackResponseBody(decoder *Decoder, resp interface{}) error { return perrors.WithStack(err) } if v, ok := attachments.(map[interface{}]interface{}); ok { - response.Attachments = ToMapStringString(v) + response.Attachments = ToMapStringInterface(v) } else { return perrors.Errorf("get wrong attachments: %+v", attachments) } @@ -222,7 +222,7 @@ func unpackResponseBody(decoder *Decoder, resp interface{}) error { return perrors.WithStack(err) } if v, ok := attachments.(map[interface{}]interface{}); ok { - atta := ToMapStringString(v) + atta := ToMapStringInterface(v) response.Attachments = atta } else { return perrors.Errorf("get wrong attachments: %+v", attachments) @@ -338,8 +338,9 @@ var versionInt = make(map[string]int) // https://github.com/apache/dubbo/blob/dubbo-2.7.1/dubbo-common/src/main/java/org/apache/dubbo/common/Version.java#L96 // isSupportResponseAttachment is for compatibility among some dubbo version -func isSupportResponseAttachment(version string) bool { - if version == "" { +func isSupportResponseAttachment(ver interface{}) bool { + version, ok := ver.(string) + if !ok || len(version) == 0 { return false } @@ -357,7 +358,11 @@ func isSupportResponseAttachment(version string) bool { return v >= LOWEST_VERSION_FOR_RESPONSE_ATTACHMENT } -func version2Int(version string) int { +func version2Int(ver interface{}) int { + version, ok := ver.(string) + if !ok || len(version) == 0 { + return 0 + } var v = 0 varr := strings.Split(version, ".") length := len(varr) diff --git a/response_test.go b/response_test.go index 72993891..5f0e07ad 100644 --- a/response_test.go +++ b/response_test.go @@ -168,7 +168,10 @@ func TestCopySlice(t *testing.T) { } func TestIsSupportResponseAttachment(t *testing.T) { - is := isSupportResponseAttachment("2.0.10") + is := isSupportResponseAttachment("2.X") + assert.False(t, is) + + is = isSupportResponseAttachment("2.0.10") assert.False(t, is) is = isSupportResponseAttachment("2.5.3") From b76a7ffcbcf42028af36e21adc4fa3b0ba77548a Mon Sep 17 00:00:00 2001 From: shen Date: Tue, 25 Aug 2020 09:22:03 +0800 Subject: [PATCH 10/15] optimize code --- java_sql_time.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/java_sql_time.go b/java_sql_time.go index 71c61e4d..e6861bbd 100644 --- a/java_sql_time.go +++ b/java_sql_time.go @@ -33,21 +33,14 @@ import ( func init() { RegisterPOJO(&java_sql_time.Date{}) RegisterPOJO(&java_sql_time.Time{}) + SetJavaSqlTimeSerialize(&java_sql_time.Date{}) + SetJavaSqlTimeSerialize(&java_sql_time.Time{}) } -var javaSqlTimeTypeMap = make(map[string]reflect.Type, 16) - // SetJavaSqlTimeSerialize register serializer for java.sql.Time & java.sql.Date func SetJavaSqlTimeSerialize(time java_sql_time.JavaSqlTime) { name := time.JavaClassName() - var typ = reflect.TypeOf(time) SetSerializer(name, JavaSqlTimeSerializer{}) - javaSqlTimeTypeMap[name] = typ -} - -// nolint -func getJavaSqlTimeSerialize(name string) reflect.Type { - return javaSqlTimeTypeMap[name] } // JavaSqlTimeSerializer used to encode & decode java.sql.Time & java.sql.Date @@ -144,9 +137,9 @@ func (JavaSqlTimeSerializer) DecObject(d *Decoder, typ reflect.Type, cls classIn sqlTime := vRef.Interface() result, ok := sqlTime.(java_sql_time.JavaSqlTime) - result.SetTime(date) if !ok { panic("result type is not sql time, please check the whether the conversion is ok") } + result.SetTime(date) return result, nil } From 94c67187cd634801e2abadf73f336d76977c3041 Mon Sep 17 00:00:00 2001 From: chenyuebo Date: Sun, 30 Aug 2020 19:48:47 +0800 Subject: [PATCH 11/15] add java_time add offsetDateTime&offsetTime&zoneOffSet&zonedDateTime --- java8_time.go | 5 +++ java8_time/offset_date_time.go | 31 ++++++++++++++++++ java8_time/offset_time.go | 31 ++++++++++++++++++ java8_time/zone_off_set.go | 30 +++++++++++++++++ java8_time/zoned_date_time.go | 32 +++++++++++++++++++ java8_time_test.go | 4 +++ .../src/main/java/test/TestJava8Time.java | 9 +++++- 7 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 java8_time/offset_date_time.go create mode 100644 java8_time/offset_time.go create mode 100644 java8_time/zone_off_set.go create mode 100644 java8_time/zoned_date_time.go diff --git a/java8_time.go b/java8_time.go index d4a72b1f..216b461f 100644 --- a/java8_time.go +++ b/java8_time.go @@ -31,4 +31,9 @@ func init() { RegisterPOJO(&java8_time.MonthDay{Month: 6, Day: 6}) RegisterPOJO(&java8_time.Duration{Second: 0, Nano: 0}) RegisterPOJO(&java8_time.Instant{Seconds: 100, Nanos: 0}) + RegisterPOJO(&java8_time.ZoneOffSet{Seconds: 0}) + RegisterPOJO(&java8_time.OffsetDateTime{DateTime: java8_time.LocalDateTime{Date: java8_time.LocalDate{Year: 2020, Month: 6, Day: 6}, Time: java8_time.LocalTime{Hour: 6, Minute: 6, Second: 6, Nano: 6}}, Offset: java8_time.ZoneOffSet{Seconds: -64800}}) + RegisterPOJO(&java8_time.OffsetTime{LocalTime: java8_time.LocalTime{Hour: 6, Minute: 6, Second: 6, Nano: 6}, ZoneOffset: java8_time.ZoneOffSet{Seconds: -64800}}) + RegisterPOJO(&java8_time.ZonedDateTime{DateTime: java8_time.LocalDateTime{Date: java8_time.LocalDate{Year: 2020, Month: 6, Day: 6}, Time: java8_time.LocalTime{Hour: 6, Minute: 6, Second: 6, Nano: 6}}, Offset: java8_time.ZoneOffSet{Seconds: 0}, ZoneId: "Z"}) + } diff --git a/java8_time/offset_date_time.go b/java8_time/offset_date_time.go new file mode 100644 index 00000000..986a804d --- /dev/null +++ b/java8_time/offset_date_time.go @@ -0,0 +1,31 @@ +/* + * 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 java8_time + +type OffsetDateTime struct { + DateTime LocalDateTime `hessian:"dateTime"` + Offset ZoneOffSet `hessian:"offset"` +} + +func (OffsetDateTime) JavaClassName() string { + return "com.alibaba.com.caucho.hessian.io.java8.OffsetDateTimeHandle" +} + +func (OffsetDateTime) Error() string { + return "encode OffsetDateTime error" +} diff --git a/java8_time/offset_time.go b/java8_time/offset_time.go new file mode 100644 index 00000000..9adc215d --- /dev/null +++ b/java8_time/offset_time.go @@ -0,0 +1,31 @@ +/* + * 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 java8_time + +type OffsetTime struct { + LocalTime LocalTime `hessian:"localTime"` + ZoneOffset ZoneOffSet `hessian:"zoneOffset"` +} + +func (OffsetTime) JavaClassName() string { + return "com.alibaba.com.caucho.hessian.io.java8.OffsetTimeHandle" +} + +func (OffsetTime) Error() string { + return "encode OffsetDateTime error" +} diff --git a/java8_time/zone_off_set.go b/java8_time/zone_off_set.go new file mode 100644 index 00000000..9b2e8a92 --- /dev/null +++ b/java8_time/zone_off_set.go @@ -0,0 +1,30 @@ +/* + * 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 java8_time + +type ZoneOffSet struct { + Seconds int32 `hessian:"seconds"` +} + +func (ZoneOffSet) JavaClassName() string { + return "com.alibaba.com.caucho.hessian.io.java8.ZoneOffsetHandle" +} + +func (ZoneOffSet) Error() string { + return "encode ZoneOffSet error" +} diff --git a/java8_time/zoned_date_time.go b/java8_time/zoned_date_time.go new file mode 100644 index 00000000..2e1eeb2b --- /dev/null +++ b/java8_time/zoned_date_time.go @@ -0,0 +1,32 @@ +/* + * 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 java8_time + +type ZonedDateTime struct { + DateTime LocalDateTime `hessian:"dateTime"` + Offset ZoneOffSet `hessian:"offset"` + ZoneId string `hessian:"zoneId"` +} + +func (ZonedDateTime) JavaClassName() string { + return "com.alibaba.com.caucho.hessian.io.java8.ZonedDateTimeHandle" +} + +func (ZonedDateTime) Error() string { + return "encode ZonedDateTime error" +} diff --git a/java8_time_test.go b/java8_time_test.go index 7506291c..b7e93dfa 100644 --- a/java8_time_test.go +++ b/java8_time_test.go @@ -32,6 +32,10 @@ func TestJava8Time(t *testing.T) { doTestTime(t, "java8_Instant", &java8_time.Instant{Seconds: 100, Nanos: 0}) doTestTime(t, "java8_YearMonth", &java8_time.YearMonth{Year: 2020, Month: 6}) doTestTime(t, "java8_Period", &java8_time.Period{Years: 2020, Months: 6, Days: 6}) + doTestTime(t, "java8_ZoneOffset", &java8_time.ZoneOffSet{Seconds: 0}) + doTestTime(t, "java8_OffsetDateTime", &java8_time.OffsetDateTime{DateTime: java8_time.LocalDateTime{Date: java8_time.LocalDate{Year: 2020, Month: 6, Day: 6}, Time: java8_time.LocalTime{Hour: 6, Minute: 6, Second: 6, Nano: 6}}, Offset: java8_time.ZoneOffSet{Seconds: -64800}}) + doTestTime(t, "java8_OffsetTime", &java8_time.OffsetTime{LocalTime: java8_time.LocalTime{Hour: 6, Minute: 6, Second: 6, Nano: 6}, ZoneOffset: java8_time.ZoneOffSet{Seconds: -64800}}) + doTestTime(t, "java8_ZonedDateTime", &java8_time.ZonedDateTime{DateTime: java8_time.LocalDateTime{Date: java8_time.LocalDate{Year: 2020, Month: 6, Day: 6}, Time: java8_time.LocalTime{Hour: 6, Minute: 6, Second: 6, Nano: 6}}, Offset: java8_time.ZoneOffSet{Seconds: 0}, ZoneId: "Z"}) } func doTestTime(t *testing.T, method string, expected interface{}) { diff --git a/test_hessian/src/main/java/test/TestJava8Time.java b/test_hessian/src/main/java/test/TestJava8Time.java index a4e39f01..2c41729f 100644 --- a/test_hessian/src/main/java/test/TestJava8Time.java +++ b/test_hessian/src/main/java/test/TestJava8Time.java @@ -34,7 +34,7 @@ public static LocalDate java8_LocalDate() { } public static LocalDateTime java8_LocalDateTime() { - return LocalDateTime.of(2020, 6, 6, 6, 6); + return LocalDateTime.of(2020, 6, 6, 6, 6, 6, 6); } public static LocalTime java8_LocalTime() { @@ -71,6 +71,13 @@ public static ZonedDateTime java8_ZonedDateTime() { } public static ZoneId java8_ZoneId() { + return ZoneId.of("Z"); + } + + public static ZoneOffset java8_ZoneOffset() { + return ZoneOffset.of("Z"); + } + return ZoneId.of("1000"); } From 396b558cd999686227c89c21934f90c7df9d7b4d Mon Sep 17 00:00:00 2001 From: chenyuebo Date: Sun, 30 Aug 2020 19:50:55 +0800 Subject: [PATCH 12/15] delete err code --- test_hessian/src/main/java/test/TestJava8Time.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/test_hessian/src/main/java/test/TestJava8Time.java b/test_hessian/src/main/java/test/TestJava8Time.java index 2c41729f..5dff8da6 100644 --- a/test_hessian/src/main/java/test/TestJava8Time.java +++ b/test_hessian/src/main/java/test/TestJava8Time.java @@ -78,9 +78,6 @@ public static ZoneOffset java8_ZoneOffset() { return ZoneOffset.of("Z"); } - return ZoneId.of("1000"); - } - public static ZoneOffset java8_ZoneOffset() { return ZoneOffset.of("1000"); } From 539ecdc9164f7277a31a940f2291ab23b93e4336 Mon Sep 17 00:00:00 2001 From: chenyuebo Date: Sun, 30 Aug 2020 20:23:48 +0800 Subject: [PATCH 13/15] delete fun --- test_hessian/src/main/java/test/TestJava8Time.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test_hessian/src/main/java/test/TestJava8Time.java b/test_hessian/src/main/java/test/TestJava8Time.java index 5dff8da6..779fe5e0 100644 --- a/test_hessian/src/main/java/test/TestJava8Time.java +++ b/test_hessian/src/main/java/test/TestJava8Time.java @@ -78,8 +78,4 @@ public static ZoneOffset java8_ZoneOffset() { return ZoneOffset.of("Z"); } - public static ZoneOffset java8_ZoneOffset() { - return ZoneOffset.of("1000"); - } - } From 97e42f605bd968b189a23923f24251414aea8730 Mon Sep 17 00:00:00 2001 From: chenyuebo Date: Sun, 30 Aug 2020 20:27:35 +0800 Subject: [PATCH 14/15] add localdateatime add second, nano --- java8_time.go | 2 +- java8_time_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/java8_time.go b/java8_time.go index 216b461f..6d5283ff 100644 --- a/java8_time.go +++ b/java8_time.go @@ -27,7 +27,7 @@ func init() { RegisterPOJO(&java8_time.Period{Years: 2020, Months: 6, Days: 6}) RegisterPOJO(&java8_time.LocalDate{Year: 2020, Month: 6, Day: 6}) RegisterPOJO(&java8_time.LocalTime{Hour: 6, Minute: 6, Second: 0, Nano: 0}) - RegisterPOJO(&java8_time.LocalDateTime{Date: java8_time.LocalDate{Year: 2020, Month: 6, Day: 6}, Time: java8_time.LocalTime{Hour: 6, Minute: 6}}) + RegisterPOJO(&java8_time.LocalDateTime{Date: java8_time.LocalDate{Year: 2020, Month: 6, Day: 6}, Time: java8_time.LocalTime{Hour: 6, Minute: 6, Second: 6, Nano: 6}}) RegisterPOJO(&java8_time.MonthDay{Month: 6, Day: 6}) RegisterPOJO(&java8_time.Duration{Second: 0, Nano: 0}) RegisterPOJO(&java8_time.Instant{Seconds: 100, Nanos: 0}) diff --git a/java8_time_test.go b/java8_time_test.go index b7e93dfa..d9d59858 100644 --- a/java8_time_test.go +++ b/java8_time_test.go @@ -26,7 +26,7 @@ func TestJava8Time(t *testing.T) { doTestTime(t, "java8_Year", &java8_time.Year{Year: 2020}) doTestTime(t, "java8_LocalDate", &java8_time.LocalDate{Year: 2020, Month: 6, Day: 6}) doTestTime(t, "java8_LocalTime", &java8_time.LocalTime{Hour: 6, Minute: 6}) - doTestTime(t, "java8_LocalDateTime", &java8_time.LocalDateTime{Date: java8_time.LocalDate{Year: 2020, Month: 6, Day: 6}, Time: java8_time.LocalTime{Hour: 6, Minute: 6}}) + doTestTime(t, "java8_LocalDateTime", &java8_time.LocalDateTime{Date: java8_time.LocalDate{Year: 2020, Month: 6, Day: 6}, Time: java8_time.LocalTime{Hour: 6, Minute: 6, Second: 6, Nano: 6}}) doTestTime(t, "java8_MonthDay", &java8_time.MonthDay{Month: 6, Day: 6}) doTestTime(t, "java8_Duration", &java8_time.Duration{Second: 0, Nano: 0}) doTestTime(t, "java8_Instant", &java8_time.Instant{Seconds: 100, Nanos: 0}) From 067bdf66a680a2985e61eecf59fe6b0dee143392 Mon Sep 17 00:00:00 2001 From: yuyu Date: Mon, 31 Aug 2020 00:17:21 +0800 Subject: [PATCH 15/15] update readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 6bc9b78e..2887670b 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ thanks to [micln](https://github.com/micln), [pantianying](https://github.com/pa * [Field Alias By Alias](https://github.com/apache/dubbo-go-hessian2/issues/19) * [Java Bigdecimal](https://github.com/apache/dubbo-go-hessian2/issues/89) * [Java Date & Time](https://github.com/apache/dubbo-go-hessian2/issues/90) +* [java8 time.Date](https://github.com/apache/dubbo-go-hessian2/pull/212) +* [java8 java.sql.Time & java.sql.Date](https://github.com/apache/dubbo-go-hessian2/pull/219) * [Java Generic Invokation](https://github.com/apache/dubbo-go-hessian2/issues/84) * [Java Extends](https://github.com/apache/dubbo-go-hessian2/issues/157) * [Dubbo Attachements](https://github.com/apache/dubbo-go-hessian2/issues/49)