Skip to content
Permalink
Browse files
Fixes #232 Add go struct refer to java.util.UUID (#256)
* add license checker

* dd javaProject java8time test code and add go hessian/java8_time strcut Year and testFunc

* alter author and error word

* add java.util.UUID encode

* Revert "reset go.mod after go fmt"

This reverts commit ab7254f

* add UUID test

* alter .travis.yml

* alter code

* add license

* shift count type int, must be unsigned integer

* alter file name and delete test file

* alter file name

* delete file

* alter file name

* alter file name

* add uuid struct info and split import block.

* add java UUID encode and uuid.toString() equals go uuid.ToString() test func

* alter ToString() -> String()

* fix code review

Co-authored-by: zouyixian <zouyixian@shein.com>
Co-authored-by: wilson chen <willson.chenwx@gmail.com>
Co-authored-by: wangoo <wongoo@apache.org>
  • Loading branch information
4 people committed Mar 14, 2021
1 parent f61ce42 commit 5527e919611f7ab0f9b0b25489d38b10842d6e47
Showing 8 changed files with 183 additions and 2 deletions.
@@ -29,4 +29,4 @@ after_success:
notifications:
webhooks:
- https://oapi.dingtalk.com/robot/send?access_token=27a5eb4510c8cf913b67a72832549b123a8c44655483d20443515604669de0ae
- https://oapi.dingtalk.com/robot/send?access_token=8250008579ed1defda3a44fb8608a38d81a55700fdfb15466315a90a7dd2045f
- https://oapi.dingtalk.com/robot/send?access_token=8250008579ed1defda3a44fb8608a38d81a55700fdfb15466315a90a7dd2045f
@@ -0,0 +1,29 @@
/*
* 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 (
"github.com/apache/dubbo-go-hessian2/java_util"
)

func init() {
RegisterPOJO(&java_util.UUID{
MostSigBits: int64(200),
LeastSigBits: int64(200),
})
}
@@ -0,0 +1,7 @@
# UUID-info

1. explain dubbo-go-hessian2 strut UUID
- JavaServer -> create UUID -> GO Client -> UUID struct (PASS)
- dubbo-go-hessian2 cannot create UUID strut
- see jdk source code of class:[java.util.UUID] learning how to create UUID struct
- see https://github.com/satori/go.uuid
@@ -0,0 +1,7 @@
# 说明

1. dubbo-go-hessian2 中 UUID 目前解析情况
- uuid.go 中提供了解析 Java 中生成好的 UUID 对象,测试通过,但是不提供生成 UUID 的功能
- java-server 提供的 uuid 可以解析,可以通过 UUID 的 ToString()函数解析成字符串,但是 go 目前未提供生成 uuid 的功能
- java uuid 生成可以参考 jdk 下 java.util.UUID 类的相关源码
- uuid 结构体创建参考 https://github.com/satori/go.uuid
@@ -0,0 +1,70 @@
/*
* 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_util

import (
"fmt"
)

// java.util.UUID
type UUID struct {
MostSigBits int64 `hessian:"mostSigBits"`
LeastSigBits int64 `hessian:"leastSigBits"`
}

func (UUID) JavaClassName() string {
return "java.util.UUID"
}

//String returns a string object representing this UUID.
//The UUID string representation is as described by this BNF:
//
// UUID = <time_low> "-" <time_mid> "-"
// <time_high_and_version> "-"
// <variant_and_sequence> "-"
// <node>
// time_low = 4*<hexOctet>
// time_mid = 2*<hexOctet>
// time_high_and_version = 2*<hexOctet>
// variant_and_sequence = 2*<hexOctet>
// node = 6*<hexOctet>
// hexOctet = <hexDigit><hexDigit>
// hexDigit =
// "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
// | "a" | "b" | "c" | "d" | "e" | "f"
// | "A" | "B" | "C" | "D" | "E" | "F"
//
//Returns:
//A string representation of this UUID
func (uuid UUID) String() string {
uuidStr := fmt.Sprintf("%v-%v-%v-%v-%v",
uuidDigits(uuid.MostSigBits>>32, 8),
uuidDigits(uuid.MostSigBits>>16, 4),
uuidDigits(uuid.MostSigBits, 4),
uuidDigits(uuid.LeastSigBits>>48, 4),
uuidDigits(uuid.LeastSigBits, 12),
)
return uuidStr
}

// uuidDigits returns arg represented by the specified number of hex digits.
func uuidDigits(arg int64, digits uint) string {
hi := int64(1) << (digits * 4)
i := hi | (arg & (hi - 1))
return fmt.Sprintf("%x", i)[1:]
}
@@ -0,0 +1,55 @@
/*
* 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"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/apache/dubbo-go-hessian2/java_util"
)

func TestJavaUtil(t *testing.T) {
res, err := decodeJavaResponse(`customReplyUUID`, ``, false)
if err != nil {
t.Error(err)
return
}
m := res.(map[interface{}]interface{})

uuid1 := &java_util.UUID{LeastSigBits: int64(-7160773830801198154), MostSigBits: int64(459021424248441700)}

resUuid1 := m["uuid1"]
resUuid1String := m["uuid1_string"]
resUuid2 := m["uuid2"]
resUuid2String := m["uuid2_string"]

assert.NotNil(t, resUuid1)
assert.NotNil(t, resUuid1String)
assert.NotNil(t, resUuid2)
assert.NotNil(t, resUuid2String)

assert.Equal(t, uuid1, resUuid1)
assert.Equal(t, uuid1.String(), resUuid1String)
assert.Equal(t, (resUuid2.(*java_util.UUID)).String(), resUuid2String)
}
@@ -102,4 +102,4 @@ private static void testCustomClassMethod(String methodName, String className) t
output.writeObject(result);
output.flush();
}
}
}
@@ -35,6 +35,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;


public class TestCustomReply {
@@ -553,6 +554,18 @@ public void customReplyGenericResponseBusinessData() throws Exception {
output.writeObject(response);
output.flush();
}

public void customReplyUUID() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
UUID uuid1 = new UUID(459021424248441700L, -7160773830801198154L);
UUID uuid2 = UUID.randomUUID();
map.put("uuid1", uuid1);
map.put("uuid1_string", uuid1.toString());
map.put("uuid2", uuid2);
map.put("uuid2_string", uuid2.toString());
output.writeObject(map);
output.flush();
}
}

interface Leg {

0 comments on commit 5527e91

Please sign in to comment.