You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by wo...@apache.org on 2020/11/27 00:39:07 UTC

[dubbo-go-hessian2] branch master updated: Ftr/support register with no pojo object (#243)

This is an automated email from the ASF dual-hosted git repository.

wongoo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-go-hessian2.git


The following commit(s) were added to refs/heads/master by this push:
     new 07e9397  Ftr/support register with no pojo object (#243)
07e9397 is described below

commit 07e93979195dcefa9e5c590409cc6d9886cec7cb
Author: Laurence <45...@users.noreply.github.com>
AuthorDate: Fri Nov 27 08:38:56 2020 +0800

    Ftr/support register with no pojo object (#243)
    
    * fix: add not pojo Object register support
    
    * fix: change gomod
    
    * fix: delete unused comment
    
    * chore: rv unused import
    
    * fix: change gomod name
    
    * Fix: replace go versionon
    
    * fix: change the way to encode no pojo interface
    
    * fix: change the way to encode no pojo interface
    
    * chore: cicd bugs
    
    * chore: call cicd
    
    * fix: go fmt file
    
    * chore: add none pojo check classInfoList
    
    * chore: delete pre check of none pojo obj
    
    * fix: change perror
    
    * fix: use loadPojoRegistry func
    
    * chore: error msg fix
---
 encode.go |  3 +--
 object.go | 21 +++++++++++++++++----
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/encode.go b/encode.go
index f20a476..224d35d 100644
--- a/encode.go
+++ b/encode.go
@@ -161,8 +161,7 @@ func (e *Encoder) Encode(v interface{}) error {
 				}
 				return e.encObject(p)
 			}
-
-			return perrors.Errorf("struct type not Support! %s[%v] is not a instance of POJO!", t.String(), v)
+			return e.encObject(vv.Interface())
 		case reflect.Slice, reflect.Array:
 			return e.encList(v)
 		case reflect.Map: // the type must be map[string]int
diff --git a/object.go b/object.go
index b398f25..7880d8b 100644
--- a/object.go
+++ b/object.go
@@ -97,7 +97,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
@@ -106,8 +106,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 := loadPOJORegistry(vv.Type().String())
+		if !ok {
+			return perrors.Errorf("non-pojo obj %s has not being registered before!", typeof(v))
+		}
+		nonePojoJavaName = s.javaName
+	}
+
 	// check ref
 	if n, ok := e.checkRefMap(vv); ok {
 		e.buffer = encRef(e.buffer, n)
@@ -124,7 +135,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
 		}
@@ -135,8 +146,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.Errorf("non-pojo obj %s has not being registered before!", typeof(v))
 			}
 		}
 		_, clsDef, err = getStructDefByIndex(idx)