Skip to content
Permalink
Browse files
support serialize UUID to string (#285)
support serialize UUID to string

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 Oct 25, 2021
1 parent 23c41ad commit 3a4ef0be57cec0ab889e3a0b22e546df24e2e6a8
Showing 4 changed files with 5 additions and 44 deletions.
@@ -23,8 +23,7 @@ import (

func init() {
RegisterPOJO(&java_util.UUID{
MostSigBits: int64(200),
LeastSigBits: int64(200),
Value: "",
})
RegisterPOJO(&java_util.LocaleHandle{
Value: "",
@@ -17,54 +17,16 @@

package java_util

import (
"fmt"
)

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

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:]
return uuid.Value
}
@@ -37,7 +37,7 @@ func TestJavaUtil(t *testing.T) {
}
m := res.(map[interface{}]interface{})

uuid1 := &java_util.UUID{LeastSigBits: int64(-7160773830801198154), MostSigBits: int64(459021424248441700)}
uuid1 := &java_util.UUID{Value: "065ec58d-a89f-4b64-9c9f-d223ea2e73b6"}

resUuid1 := m["uuid1"]
resUuid1String := m["uuid1_string"]
@@ -27,7 +27,7 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>hessian-lite</artifactId>
<version>3.2.7</version>
<version>3.2.8</version>
<scope>compile</scope>
</dependency>
<dependency>

0 comments on commit 3a4ef0b

Please sign in to comment.