apache/dubbo-go-hessian2
Fork: 114 Star: 200 (更新于 1970-01-01 00:00:00)
license: Apache-2.0
Language: Go .
caucho hessian2 implementation in Go for [apache/dubbo-go](https://github.com/apache/dubbo-go) which is compatible with [dubbo-hessian-lite](https://github.com/apache/dubbo-hessian-lite)
最后发布版本: v1.12.3 ( 2023-11-02 13:42:52)
dubbo-go-hessian2
Notice: When decoding, the java version of hessian will default skip and ignore non-exist fields. From the version of v1.6.0 , dubbo-go-hessian2 will skip non-exist fields too, while that before v1.6.0 will return errors.
It's a golang hessian library used by Apache/dubbo-go.
There is a big performance improvement, and some bugs fix for v1.6.0, thanks to micln, pantianying, zonghaishang, willson-chen, champly.
Feature List
- All JDK Exceptions
- Field Alias By Alias
- Java wrapper type
- Java Bigdecimal
- Java Date & Time
- java8 time.Date
- java8 java.sql.Time & java.sql.Date
- java UUID
- Java Generic Invokation
- Java Extends
- Dubbo Attachements
- Skipping unregistered POJO
- Emoji
hessian type mapping between Java and Go
Cross languages message definition should be careful, the following situations should be avoided:
- define object that only exists in a special language
- using various java exceptions (using error code/message instead)
So we can maintain a cross language type mapping:
hessian type | java type | golang type |
---|---|---|
null | null | nil |
binary | byte[] | []byte |
boolean | boolean | bool |
date | java.util.Date | time.Time |
double | double | float64 |
int | int | int32 |
long | long | int64 |
string | java.lang.String | string |
list | java.util.List | slice |
map | java.util.Map | map |
object | custom define object | custom define struct |
big decimal | java.math.BigDecimal | github.com/dubbogo/gost/math/big/Decimal |
big integer | java.math.BigInteger | github.com/dubbogo/gost/math/big/Integer |
date | java.sql.Date | github.com/apache/dubbo-go-hessian2/java_sql_time/Date |
date | java.sql.Time | github.com/apache/dubbo-go-hessian2/java_sql_time/Time |
date | all java8 sdk time | github.com/apache/dubbo-go-hessian2/java8_time |
Integer | java.lang.Integer | *int32 |
Byte | java.lang.Byte | *byte |
Short | java.lang.Short | *int16 |
Boolean | java.lang.Boolean | *bool |
Long | java.lang.Long | *int64 |
Float | java.lang.Float | *float32 |
Double | java.lang.Double | *float64 |
Character | java.lang.Character | *hessian.Rune |
OTHER COMMON USING TYPE |
reference
Basic Usage Examples
Encode To Bytes
type Circular struct {
Value
Previous *Circular
Next *Circular
}
type Value struct {
Num int
}
func (Circular) JavaClassName() string {
return "com.company.Circular"
}
c := &Circular{}
c.Num = 12345
c.Previous = c
c.Next = c
e := NewEncoder()
err := e.Encode(c)
if err != nil {
panic(err)
}
bytes := e.Buffer()
Decode From Bytes
decodedObject, err := NewDecoder(bytes).Decode()
if err != nil {
panic(err)
}
circular, ok := obj.(*Circular)
// ...
Customize Usage Examples
Encoding filed name
Hessian encoder default converts filed names of struct to lower camelcase, but you can customize it using hessian
tag.
Example:
type MyUser struct {
UserFullName string `hessian:"user_full_name"`
FamilyPhoneNumber string // default convert to => familyPhoneNumber
}
func (MyUser) JavaClassName() string {
return "com.company.myuser"
}
user := &MyUser{
UserFullName: "username",
FamilyPhoneNumber: "010-12345678",
}
e := hessian.NewEncoder()
err := e.Encode(user)
if err != nil {
panic(err)
}
The encoded bytes of the struct MyUser
is as following:
00000000 43 12 63 6f 6d 2e 63 6f 6d 70 61 6e 79 2e 6d 79 |C.com.company.my|
00000010 75 73 65 72 92 0e 75 73 65 72 5f 66 75 6c 6c 5f |user..user_full_|
00000020 6e 61 6d 65 11 66 61 6d 69 6c 79 50 68 6f 6e 65 |name.familyPhone|
00000030 4e 75 6d 62 65 72 60 08 75 73 65 72 6e 61 6d 65 |Number`.username|
00000040 0c 30 31 30 2d 31 32 33 34 35 36 37 38 |.010-12345678|
Decoding filed name
Hessian decoder finds the correct target field though comparing all filed names of struct one by one until matching.
The following example shows the order of the matching rules:
type MyUser struct {
MobilePhone string `hessian:"mobile-phone"`
}
// You must define the tag of struct for lookup filed form encoded binary bytes, in this case:
// 00000000 43 12 63 6f 6d 2e 63 6f 6d 70 61 6e 79 2e 6d 79 |C.com.company.my|
// 00000010 75 73 65 72 91 0c 6d 6f 62 69 6c 65 2d 70 68 6f |user..mobile-pho|
// 00000020 6e 65 60 0b 31 37 36 31 32 33 34 31 32 33 34 |ne`.17612341234|
//
// mobile-phone(tag lookup) => mobilePhone(lowerCameCase) => MobilePhone(SameCase) => mobilephone(lowercase)
// ^ will matched
type MyUser struct {
MobilePhone string
}
// The following encoded binary bytes will be hit automatically:
//
// 00000000 43 12 63 6f 6d 2e 63 6f 6d 70 61 6e 79 2e 6d 79 |C.com.company.my|
// 00000010 75 73 65 72 91 0b 6d 6f 62 69 6c 65 50 68 6f 6e |user..mobilePhon|
// 00000020 65 60 0b 31 37 36 31 32 33 34 31 32 33 34 |e`.17612341234|
//
// mobile-phone(tag lookup) => mobilePhone(lowerCameCase) => MobilePhone(SameCase) => mobilephone(lowercase)
// ^ will matched
//
// 00000000 43 12 63 6f 6d 2e 63 6f 6d 70 61 6e 79 2e 6d 79 |C.com.company.my|
// 00000010 75 73 65 72 91 0b 4d 6f 62 69 6c 65 50 68 6f 6e |user..MobilePhon|
// 00000020 65 60 0b 31 37 36 31 32 33 34 31 32 33 34 |e`.17612341234|
//
// mobile-phone(tag lookup) => mobilePhone(lowerCameCase) => MobilePhone(SameCase) => mobilephone(lowercase)
// ^ will matched
//
// 00000000 43 12 63 6f 6d 2e 63 6f 6d 70 61 6e 79 2e 6d 79 |C.com.company.my|
// 00000010 75 73 65 72 91 0b 6d 6f 62 69 6c 65 70 68 6f 6e |user..mobilephon|
// 00000020 65 60 0b 31 37 36 31 32 33 34 31 32 33 34 |e`.17612341234|
//
// mobile-phone(tag lookup) => mobilePhone(lowerCameCase) => MobilePhone(SameCase) => mobilephone(lowercase)
// ^ will matched
Encoding param name
When a Java method declares an argument as a parent class, it actually hope receives a subclass, You can specify the encoding type of the parameter separately.
java-server
public abstract class User {
}
public class MyUser extends User implements Serializable {
private String userFullName;
private String familyPhoneNumber;
}
public interface UserProvider {
String GetUser(User user);
}
public class UserProviderImpl implements UserProvider {
public UserProviderImpl() {
}
public String GetUser(User user) {
MyUser myUser=(MyUser)user;
return myUser.getUserFullName();
}
}
go-client
type MyUser struct {
UserFullName string `hessian:"userFullName"`
FamilyPhoneNumber string // default convert to => familyPhoneNumber
}
func (m *MyUser) JavaClassName() string {
return "com.company.MyUser"
}
func (m *MyUser) JavaParamName() string {
return "com.company.User"
}
type UserProvider struct {
GetUser func(ctx context.Context, user *MyUser) (string, error) `dubbo:"GetUser"`
}
Set method Alias
When the Go client calls the Java server, the first letter of the method is converted to lowercase by default,you can use the dubbo tag to set method alias.
type UserProvider struct {
GetUser func(ctx context.Context) (*User, error) `dubbo:"GetUser"`
}
hessian.SetTagIdentifier
You can use hessian.SetTagIdentifier
to customize tag-identifier of hessian, which takes effect to both encoder and decoder.
Example:
hessian.SetTagIdentifier("json")
type MyUser struct {
UserFullName string `json:"user_full_name"`
FamilyPhoneNumber string // default convert to => familyPhoneNumber
}
func (MyUser) JavaClassName() string {
return "com.company.myuser"
}
user := &MyUser{
UserFullName: "username",
FamilyPhoneNumber: "010-12345678",
}
e := hessian.NewEncoder()
err := e.Encode(user)
if err != nil {
panic(err)
}
The encoded bytes of the struct MyUser
is as following:
00000000 43 12 63 6f 6d 2e 63 6f 6d 70 61 6e 79 2e 6d 79 |C.com.company.my|
00000010 75 73 65 72 92 0e 75 73 65 72 5f 66 75 6c 6c 5f |user..user_full_|
00000020 6e 61 6d 65 11 66 61 6d 69 6c 79 50 68 6f 6e 65 |name.familyPhone|
00000030 4e 75 6d 62 65 72 60 08 75 73 65 72 6e 61 6d 65 |Number`.username|
00000040 0c 30 31 30 2d 31 32 33 34 35 36 37 38 |.010-12345678|
Using Java collections
By default, the output of Hessian Java impl of a Java collection like java.util.HashSet will be decoded as []interface{}
in go-hessian2
.
To apply the one-to-one mapping relationship between certain Java collection class and your Go struct, examples are as follows:
//use HashSet as example
//define your struct, which should implements hessian.JavaCollectionObject
type JavaHashSet struct {
value []interface{}
}
//get the inside slice value
func (j *JavaHashSet) Get() []interface{} {
return j.value
}
//set the inside slice value
func (j *JavaHashSet) Set(v []interface{}) {
j.value = v
}
//should be the same as the class name of the Java collection
func (j *JavaHashSet) JavaClassName() string {
return "java.util.HashSet"
}
func init() {
//register your struct so that hessian can recognized it when encoding and decoding
SetCollectionSerialize(&JavaHashSet{})
}
Notice for inheritance
go-hessian2
supports inheritance struct, but the following situations should be avoided.
- Avoid fields with the same name in multiple parent struct
The following struct C
have inherited field Name
(default from the first parent),
but it's confused in logic.
type A struct { Name string }
type B struct { Name string }
type C struct {
A
B
}
- Avoid inheritance for a pointer of struct
The following definition is valid for golang syntax,
but the parent will be nil when create a new Dog, like dog := Dog{}
,
which will not happen in java inheritance,
and is also not supported by go-hessian2
.
type Dog struct {
*Animal
}
Strict Mode
Default, hessian2 will decode an object to map if it's not being registered. If you don't want that, change the decoder to strict mode as following, and it will return error when meeting unregistered object.
e := hessian.NewDecoder(bytes)
e.Strict = true // set to strict mode, default is false
// or
e := hessian.NewStrictDecoder(bytes)
Tools
tools/gen-go-enum
A tool for generate hessian2 java enum define golang code. Read more details.
最近版本更新:(数据更新于 1970-01-01 00:00:00)
2023-11-02 13:42:52 v1.12.3
2023-05-06 14:05:39 v1.12.2
2023-04-10 00:16:08 v1.12.1
2023-02-26 11:41:01 v1.12.0
2023-02-24 21:28:00 v1.11.7
2023-02-14 09:50:41 v1.11.6
2022-12-06 17:10:42 v1.11.5
2022-11-13 18:05:31 v1.11.4
2022-10-31 17:07:30 v1.11.3
2022-10-24 11:26:20 v1.11.2
主题(topics):
apache-dubbo-go, caucho, dubbo, dubbo-go, dubbo-hessian, dubbo-hessian-lite, dubbogo, dubbox, hessian, hessian2
apache/dubbo-go-hessian2同语言 Go最近更新仓库
2024-12-22 07:52:58 navidrome/navidrome
2024-12-21 20:15:12 SagerNet/sing-box
2024-12-21 03:25:54 SpecterOps/BloodHound
2024-12-19 23:11:24 shadow1ng/fscan
2024-12-19 21:50:56 minio/minio
2024-12-19 10:04:39 istio/istio