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: add json generalizer #1343

Merged
merged 4 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
101 changes: 101 additions & 0 deletions filter/generic/generalizer/json.go
Original file line number Diff line number Diff line change
@@ -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)
EnableAsync marked this conversation as resolved.
Show resolved Hide resolved
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 {
EnableAsync marked this conversation as resolved.
Show resolved Hide resolved
typ = typ.Elem()
}

// create the target object
ret, ok := reflect.New(typ).Interface().(hessian.Object)
EnableAsync marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
return nil, perrors.Errorf("the type of obj(=%s) should be hessian object", typ)
}

err := json.Unmarshal([]byte(jsonbytes), ret)
if err != nil {
return nil, err
}

return ret, nil
}

func (JsonGeneralizer) GetType(obj interface{}) (typ string, err error) {
typ, err = hessian2.GetJavaName(obj)
// no error or error is not NilError
if err == nil || err != hessian2.NilError {
return
}

typ = "java.lang.Object"
if err == hessian2.NilError {
logger.Debugf("the type of nil object couldn't be inferred, use the default value(\"%s\")", typ)
return
}

logger.Debugf("the type of object(=%T) couldn't be recognized as a POJO, use the default value(\"%s\")", obj, typ)
return
}
71 changes: 71 additions & 0 deletions filter/generic/generalizer/json_test.go
Original file line number Diff line number Diff line change
@@ -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{
EnableAsync marked this conversation as resolved.
Show resolved Hide resolved
Age: 20,
Gender: "male",
Email: "childName@example.com",
Name: "childName",
}
p := mockJsonParent{
Age: 30,
Gender: "male",
Email: "enableasync@example.com",
Name: "enableasync",
Child: c,
}

m, err := mockJsonGeneralizer.Generalize(p)
assert.Nil(t, err)
assert.Equal(t, "{\"Gender\":\"male\",\"Email\":\"enableasync@example.com\",\"Name\":\"enableasync\",\"Age\":30,\"Child\":{\"Gender\":\"male\",\"Email\":\"childName@example.com\",\"Name\":\"childName\",\"Age\":20}}", m)

r, err := mockJsonGeneralizer.Realize(m, reflect.TypeOf(p))
assert.Nil(t, err)
rMockParent, ok := r.(*mockJsonParent)
assert.True(t, ok)
// parent
assert.Equal(t, "enableasync", rMockParent.Name)
assert.Equal(t, 30, rMockParent.Age)
// child
assert.Equal(t, "childName", rMockParent.Child.Name)
assert.Equal(t, 20, rMockParent.Child.Age)
}