-
Notifications
You must be signed in to change notification settings - Fork 929
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from pantianying/master
generic#invoke (go to java)
- Loading branch information
Showing
8 changed files
with
291 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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 config | ||
|
||
type GenericService struct { | ||
Invoke func(req []interface{}) (interface{}, error) `dubbo:"$invoke"` | ||
referenceStr string | ||
} | ||
|
||
func NewGenericService(referenceStr string) *GenericService { | ||
return &GenericService{referenceStr: referenceStr} | ||
} | ||
|
||
func (u *GenericService) Reference() string { | ||
return u.referenceStr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* 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 impl | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
) | ||
import ( | ||
hessian "github.com/apache/dubbo-go-hessian2" | ||
) | ||
import ( | ||
"github.com/apache/dubbo-go/common/constant" | ||
"github.com/apache/dubbo-go/common/extension" | ||
"github.com/apache/dubbo-go/filter" | ||
"github.com/apache/dubbo-go/protocol" | ||
invocation2 "github.com/apache/dubbo-go/protocol/invocation" | ||
) | ||
|
||
const ( | ||
GENERIC = "generic" | ||
) | ||
|
||
func init() { | ||
extension.SetFilter(GENERIC, GetGenericFilter) | ||
} | ||
|
||
// when do a generic invoke, struct need to be map | ||
|
||
type GenericFilter struct{} | ||
|
||
func (ef *GenericFilter) Invoke(invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { | ||
if invocation.MethodName() == constant.GENERIC && len(invocation.Arguments()) == 3 { | ||
oldArguments := invocation.Arguments() | ||
var newParams []hessian.Object | ||
if oldParams, ok := oldArguments[2].([]interface{}); ok { | ||
for i := range oldParams { | ||
newParams = append(newParams, hessian.Object(struct2MapAll(oldParams[i]))) | ||
} | ||
} else { | ||
return invoker.Invoke(invocation) | ||
} | ||
newArguments := []interface{}{ | ||
oldArguments[0], | ||
oldArguments[1], | ||
newParams, | ||
} | ||
newInvocation := invocation2.NewRPCInvocation(invocation.MethodName(), newArguments, invocation.Attachments()) | ||
newInvocation.SetReply(invocation.Reply()) | ||
return invoker.Invoke(newInvocation) | ||
} | ||
return invoker.Invoke(invocation) | ||
} | ||
|
||
func (ef *GenericFilter) OnResponse(result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result { | ||
return result | ||
} | ||
|
||
func GetGenericFilter() filter.Filter { | ||
return &GenericFilter{} | ||
} | ||
func struct2MapAll(obj interface{}) interface{} { | ||
if obj == nil { | ||
return obj | ||
} | ||
t := reflect.TypeOf(obj) | ||
v := reflect.ValueOf(obj) | ||
if t.Kind() == reflect.Struct { | ||
result := make(map[string]interface{}, t.NumField()) | ||
for i := 0; i < t.NumField(); i++ { | ||
if v.Field(i).Kind() == reflect.Struct { | ||
if v.Field(i).CanInterface() { | ||
setInMap(result, t.Field(i), struct2MapAll(v.Field(i).Interface())) | ||
} | ||
} else if v.Field(i).Kind() == reflect.Slice { | ||
if v.Field(i).CanInterface() { | ||
setInMap(result, t.Field(i), struct2MapAll(v.Field(i).Interface())) | ||
} | ||
} else { | ||
if v.Field(i).CanInterface() { | ||
setInMap(result, t.Field(i), v.Field(i).Interface()) | ||
} | ||
} | ||
} | ||
return result | ||
} else if t.Kind() == reflect.Slice { | ||
value := reflect.ValueOf(obj) | ||
var newTemps = make([]interface{}, 0, value.Len()) | ||
for i := 0; i < value.Len(); i++ { | ||
newTemp := struct2MapAll(value.Index(i).Interface()) | ||
newTemps = append(newTemps, newTemp) | ||
} | ||
return newTemps | ||
} else { | ||
return obj | ||
} | ||
} | ||
func setInMap(m map[string]interface{}, structField reflect.StructField, value interface{}) (result map[string]interface{}) { | ||
result = m | ||
if tagName := structField.Tag.Get("m"); tagName == "" { | ||
result[headerAtoa(structField.Name)] = value | ||
} else { | ||
result[tagName] = value | ||
} | ||
return | ||
} | ||
func headerAtoa(a string) (b string) { | ||
b = strings.ToLower(a[:1]) + a[1:] | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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 impl | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
import ( | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_struct2MapAll(t *testing.T) { | ||
var testData struct { | ||
AaAa string `m:"aaAa"` | ||
BaBa string | ||
CaCa struct { | ||
AaAa string | ||
BaBa string `m:"baBa"` | ||
XxYy struct { | ||
xxXx string `m:"xxXx"` | ||
Xx string `m:"xx"` | ||
} `m:"xxYy"` | ||
} `m:"caCa"` | ||
} | ||
testData.AaAa = "1" | ||
testData.BaBa = "1" | ||
testData.CaCa.BaBa = "2" | ||
testData.CaCa.AaAa = "2" | ||
testData.CaCa.XxYy.xxXx = "3" | ||
testData.CaCa.XxYy.Xx = "3" | ||
m := struct2MapAll(testData).(map[string]interface{}) | ||
assert.Equal(t, "1", m["aaAa"].(string)) | ||
assert.Equal(t, "1", m["baBa"].(string)) | ||
assert.Equal(t, "2", m["caCa"].(map[string]interface{})["aaAa"].(string)) | ||
assert.Equal(t, "3", m["caCa"].(map[string]interface{})["xxYy"].(map[string]interface{})["xx"].(string)) | ||
|
||
assert.Equal(t, reflect.Map, reflect.TypeOf(m["caCa"]).Kind()) | ||
assert.Equal(t, reflect.Map, reflect.TypeOf(m["caCa"].(map[string]interface{})["xxYy"]).Kind()) | ||
} | ||
|
||
type testStruct struct { | ||
AaAa string | ||
BaBa string `m:"baBa"` | ||
XxYy struct { | ||
xxXx string `m:"xxXx"` | ||
Xx string `m:"xx"` | ||
} `m:"xxYy"` | ||
} | ||
|
||
func Test_struct2MapAll_Slice(t *testing.T) { | ||
var testData struct { | ||
AaAa string `m:"aaAa"` | ||
BaBa string | ||
CaCa []testStruct `m:"caCa"` | ||
} | ||
testData.AaAa = "1" | ||
testData.BaBa = "1" | ||
var tmp testStruct | ||
tmp.BaBa = "2" | ||
tmp.AaAa = "2" | ||
tmp.XxYy.xxXx = "3" | ||
tmp.XxYy.Xx = "3" | ||
testData.CaCa = append(testData.CaCa, tmp) | ||
m := struct2MapAll(testData).(map[string]interface{}) | ||
|
||
assert.Equal(t, "1", m["aaAa"].(string)) | ||
assert.Equal(t, "1", m["baBa"].(string)) | ||
assert.Equal(t, "2", m["caCa"].([]interface{})[0].(map[string]interface{})["aaAa"].(string)) | ||
assert.Equal(t, "3", m["caCa"].([]interface{})[0].(map[string]interface{})["xxYy"].(map[string]interface{})["xx"].(string)) | ||
|
||
assert.Equal(t, reflect.Slice, reflect.TypeOf(m["caCa"]).Kind()) | ||
assert.Equal(t, reflect.Map, reflect.TypeOf(m["caCa"].([]interface{})[0].(map[string]interface{})["xxYy"]).Kind()) | ||
} |