Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ftr/support register with no pojo object #243

Merged
merged 17 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ func (e *Encoder) Encode(v interface{}) error {
return e.encObject(p)
}

if _, ok := checkPOJORegistry(vv.Type().String()); ok {
wongoo marked this conversation as resolved.
Show resolved Hide resolved
return e.encObject(vv.Interface())
}
return perrors.Errorf("struct type not Support! %s[%v] is not a instance of POJO!", t.String(), v)
case reflect.Slice, reflect.Array:
return e.encList(v)
Expand Down
22 changes: 18 additions & 4 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package hessian

import (
"fmt"
wongoo marked this conversation as resolved.
Show resolved Hide resolved
"io"
"reflect"
"strings"
Expand Down Expand Up @@ -97,7 +98,7 @@ func typeof(v interface{}) string {
// x04 BLUE # BLUE value
//
//x51 x91 # object ref #1, i.e. Color.GREEN
func (e *Encoder) encObject(v POJO) error {
func (e *Encoder) encObject(v interface{}) error {
var (
ok bool
i int
Expand All @@ -106,8 +107,19 @@ func (e *Encoder) encObject(v POJO) error {
err error
clsDef classInfo
)

pojo, isPojo := v.(POJO)
vv := reflect.ValueOf(v)

// get none pojo JavaClassName
var nonePojoJavaName string
if !isPojo {
s, ok := pojoRegistry.registry[vv.Type().String()]
wongoo marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
return perrors.New("Not pojo obj:" + typeof(v) + " didn't registered before!")
wongoo marked this conversation as resolved.
Show resolved Hide resolved
}
nonePojoJavaName = s.javaName
}

// check ref
if n, ok := e.checkRefMap(vv); ok {
e.buffer = encRef(e.buffer, n)
Expand All @@ -124,7 +136,7 @@ func (e *Encoder) encObject(v POJO) error {
// write object definition
idx = -1
for i = range e.classInfoList {
if v.JavaClassName() == e.classInfoList[i].javaName {
if isPojo && pojo.JavaClassName() == e.classInfoList[i].javaName || !isPojo && nonePojoJavaName == e.classInfoList[i].javaName {
idx = i
break
}
Expand All @@ -135,8 +147,10 @@ func (e *Encoder) encObject(v POJO) error {
if !ok {
if reflect.TypeOf(v).Implements(javaEnumType) {
idx = RegisterJavaEnum(v.(POJOEnum))
} else if isPojo {
idx = RegisterPOJO(pojo)
} else {
idx = RegisterPOJO(v)
return perrors.New("Not pojo obj:" + typeof(v) + " didn't registered before!")
}
}
_, clsDef, err = getStructDefByIndex(idx)
Expand Down