You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/07/26 08:04:58 UTC

[GitHub] [dubbo-go] xavier-niu commented on a change in pull request #1343: Ftr: add json generalizer

xavier-niu commented on a change in pull request #1343:
URL: https://github.com/apache/dubbo-go/pull/1343#discussion_r676370958



##########
File path: filter/generic/generalizer/json.go
##########
@@ -0,0 +1,101 @@
+/*
+ * 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 (
+	"encoding/json"
+	"reflect"
+	"sync"
+)
+
+import (
+	"dubbo.apache.org/dubbo-go/v3/common/logger"
+	"dubbo.apache.org/dubbo-go/v3/protocol/dubbo/hessian2"
+	hessian "github.com/apache/dubbo-go-hessian2"
+	perrors "github.com/pkg/errors"
+)
+
+var (
+	jsonGeneralizer     Generalizer
+	jsonGeneralizerOnce sync.Once
+)
+
+func GetJsonrpcGeneralizer() Generalizer {
+	jsonGeneralizerOnce.Do(func() {
+		jsonGeneralizer = &JsonGeneralizer{}
+	})
+	return jsonGeneralizer
+}
+
+type JsonGeneralizer struct{}
+
+func (JsonGeneralizer) Generalize(obj interface{}) (interface{}, error) {
+	newObj, ok := obj.(hessian.Object)

Review comment:
       Checking `hessian.Object` type is not necessary here. Instead, `obj` must conform to `hessian.POJO`, in other words, it should have a method named `JavaClassName()`.

##########
File path: filter/generic/generalizer/json.go
##########
@@ -0,0 +1,101 @@
+/*
+ * 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 (
+	"encoding/json"
+	"reflect"
+	"sync"
+)
+
+import (
+	"dubbo.apache.org/dubbo-go/v3/common/logger"
+	"dubbo.apache.org/dubbo-go/v3/protocol/dubbo/hessian2"
+	hessian "github.com/apache/dubbo-go-hessian2"
+	perrors "github.com/pkg/errors"
+)
+
+var (
+	jsonGeneralizer     Generalizer
+	jsonGeneralizerOnce sync.Once
+)
+
+func GetJsonrpcGeneralizer() Generalizer {
+	jsonGeneralizerOnce.Do(func() {
+		jsonGeneralizer = &JsonGeneralizer{}
+	})
+	return jsonGeneralizer
+}
+
+type JsonGeneralizer struct{}
+
+func (JsonGeneralizer) Generalize(obj interface{}) (interface{}, error) {
+	newObj, ok := obj.(hessian.Object)
+	if !ok {
+		return nil, perrors.Errorf("unexpected type of obj(=%T), wanted is hessian object", obj)
+	}
+
+	jsonbytes, err := json.Marshal(newObj)
+	if err != nil {
+		return nil, err
+	}
+
+	return string(jsonbytes), nil
+}
+
+func (JsonGeneralizer) Realize(obj interface{}, typ reflect.Type) (interface{}, error) {
+	jsonbytes, ok := obj.(string)
+	if !ok {
+		return nil, perrors.Errorf("unexpected type of obj(=%T), wanted is string", obj)
+	}
+
+	// typ represents a struct instead of a pointer
+	for typ.Kind() == reflect.Ptr {

Review comment:
       `typ` could be a pointer, this for loop may cause some problems(I'm not sure). You should test this branch in unit tests.

##########
File path: filter/generic/generalizer/json_test.go
##########
@@ -0,0 +1,71 @@
+/*
+ * 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"
+	"testing"
+)
+
+import (
+	"github.com/stretchr/testify/assert"
+)
+
+var mockJsonGeneralizer = GetJsonrpcGeneralizer()
+
+type mockJsonParent struct {
+	Gender, Email, Name string
+	Age                 int
+	Child               *mockJsonChild
+}
+
+type mockJsonChild struct {
+	Gender, Email, Name string
+	Age                 int
+}
+
+func TestJsonGeneralizer(t *testing.T) {
+	c := &mockJsonChild{

Review comment:
       Please add test case for `c` to check if the for loop is necessary.

##########
File path: filter/generic/generalizer/json.go
##########
@@ -0,0 +1,101 @@
+/*
+ * 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 (
+	"encoding/json"
+	"reflect"
+	"sync"
+)
+
+import (
+	"dubbo.apache.org/dubbo-go/v3/common/logger"
+	"dubbo.apache.org/dubbo-go/v3/protocol/dubbo/hessian2"
+	hessian "github.com/apache/dubbo-go-hessian2"
+	perrors "github.com/pkg/errors"
+)
+
+var (
+	jsonGeneralizer     Generalizer
+	jsonGeneralizerOnce sync.Once
+)
+
+func GetJsonrpcGeneralizer() Generalizer {
+	jsonGeneralizerOnce.Do(func() {
+		jsonGeneralizer = &JsonGeneralizer{}
+	})
+	return jsonGeneralizer
+}
+
+type JsonGeneralizer struct{}
+
+func (JsonGeneralizer) Generalize(obj interface{}) (interface{}, error) {
+	newObj, ok := obj.(hessian.Object)
+	if !ok {
+		return nil, perrors.Errorf("unexpected type of obj(=%T), wanted is hessian object", obj)
+	}
+
+	jsonbytes, err := json.Marshal(newObj)
+	if err != nil {
+		return nil, err
+	}
+
+	return string(jsonbytes), nil
+}
+
+func (JsonGeneralizer) Realize(obj interface{}, typ reflect.Type) (interface{}, error) {
+	jsonbytes, ok := obj.(string)
+	if !ok {
+		return nil, perrors.Errorf("unexpected type of obj(=%T), wanted is string", obj)
+	}
+
+	// typ represents a struct instead of a pointer
+	for typ.Kind() == reflect.Ptr {
+		typ = typ.Elem()
+	}
+
+	// create the target object
+	ret, ok := reflect.New(typ).Interface().(hessian.Object)

Review comment:
       Same above(`hessian.POJO`).




-- 
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: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org