Alanxtl commented on code in PR #3175:
URL: https://github.com/apache/dubbo-go/pull/3175#discussion_r2724940789
##########
filter/generic/util_test.go:
##########
@@ -81,7 +81,7 @@ func TestIsGeneric(t *testing.T) {
assert.True(t, isGeneric("Protobuf-Json"))
assert.False(t, isGeneric("false"))
assert.False(t, isGeneric(""))
- assert.False(t, isGeneric("bean"))
+ assert.True(t, isGeneric("bean")) // bean serialization is supported
Review Comment:
just delete this line
```suggestion
assert.True(t, isGeneric("bean"))
```
##########
filter/generic/generalizer/bean.go:
##########
@@ -0,0 +1,238 @@
+/*
+ * 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 generalizer
+
+import (
+ "reflect"
+ "sync"
+)
+
+import (
+ hessian "github.com/apache/dubbo-go-hessian2"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo/hessian2"
+)
+
+// JavaBeanDescriptor type constants
+const (
+ TypeClass = 1
+ TypeEnum = 2
+ TypeCollection = 3
+ TypeMap = 4
+ TypeArray = 5
+ TypePrimitive = 6
+ TypeBean = 7
+)
+
+// JavaBeanDescriptor corresponds to
org.apache.dubbo.common.beanutil.JavaBeanDescriptor
+type JavaBeanDescriptor struct {
+ ClassName string `json:"className"`
+ Type int `json:"type"`
+ Properties map[any]any `json:"properties"`
+}
+
+// JavaClassName implements hessian.POJO
+func (d *JavaBeanDescriptor) JavaClassName() string {
+ return "org.apache.dubbo.common.beanutil.JavaBeanDescriptor"
+}
+
+func init() {
+ hessian.RegisterPOJO(&JavaBeanDescriptor{})
+}
+
+var (
+ beanGeneralizer Generalizer
+ beanGeneralizerOnce sync.Once
+)
+
+func GetBeanGeneralizer() Generalizer {
+ beanGeneralizerOnce.Do(func() {
+ beanGeneralizer = &BeanGeneralizer{}
+ })
+ return beanGeneralizer
+}
+
+// BeanGeneralizer converts objects to/from JavaBeanDescriptor format
+type BeanGeneralizer struct{}
+
+func (g *BeanGeneralizer) Generalize(obj any) (any, error) {
+ if obj == nil {
+ return nil, nil
+ }
+ return g.toDescriptor(obj), nil
+}
+
+func (g *BeanGeneralizer) Realize(obj any, typ reflect.Type) (any, error) {
+ if obj == nil {
+ return nil, nil
+ }
+ return GetMapGeneralizer().Realize(g.fromDescriptor(obj), typ)
+}
+
+func (g *BeanGeneralizer) GetType(obj any) (typ string, err error) {
+ typ, err = hessian2.GetJavaName(obj)
+ // no error or error is not NilError
+ if err == nil || err != hessian2.NilError {
+ return
+ }
+
+ // fallback to java.lang.Object for nil or unrecognized types
+ typ = "java.lang.Object"
+ err = nil
+ return
+}
+
+// toDescriptor converts Go object to JavaBeanDescriptor
+func (g *BeanGeneralizer) toDescriptor(obj any) *JavaBeanDescriptor {
Review Comment:
这个toDescriptor在遇到循环引用的时候似乎没有进行处理,比如
```
type Node struct{ Next *Node }
n := &Node{}; n.Next = n
g.toDescriptor(n) // here
```
这种情况是不是会死循环啊,检查一下然后在test里面加入对这种情况的test
##########
filter/generic/generalizer/bean.go:
##########
@@ -0,0 +1,238 @@
+/*
+ * 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 generalizer
+
+import (
+ "reflect"
+ "sync"
+)
+
+import (
+ hessian "github.com/apache/dubbo-go-hessian2"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo/hessian2"
+)
+
+// JavaBeanDescriptor type constants
+const (
+ TypeClass = 1
+ TypeEnum = 2
+ TypeCollection = 3
+ TypeMap = 4
+ TypeArray = 5
+ TypePrimitive = 6
+ TypeBean = 7
+)
Review Comment:
现在定义了这几种type,但是toDescriptor() 实际好像只会产出
TypePrimitive、TypeArray、TypeMap、TypeBean
并不会产出剩下的几种type,这是符合预期的吗,如果是的话在注释里面写一下
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]