-
Notifications
You must be signed in to change notification settings - Fork 953
generic#invoke (go to java) #122
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
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
22498ac
Merge pull request #1 from apache/master
pantianying 47c4d20
Merge pull request #2 from apache/develop
pantianying 9751266
add go to java generic
pantianying 6cde06a
fix test
pantianying 25b1512
update
pantianying e216bcb
优化
pantianying 2cf9399
改成和java一样的使用方式
pantianying 4df2f94
fix
pantianying e545dd0
add license
pantianying 48073dd
change file name
pantianying 5e0ebf7
fix、and jsonrpc has no genericComsumer
pantianying 79775de
Merge pull request #3 from apache/develop
pantianying 0b111a5
Merge pull request #5 from apache/develop
pantianying 4dbeb2a
fix bug with Temporary disposal
pantianying 7f00a83
Merge branch 'master' of https://github.com/pantianying/dubbo-go
pantianying 09b9130
update
pantianying 81b8c35
change annotation
pantianying 96f5554
add generic filter
pantianying e9d7d86
Merge remote-tracking branch 'apache/develop'
pantianying 74e16bb
add ut
pantianying 428fad4
ut be compatible with go 1.11
pantianying 31c898f
add Generic tag
pantianying d77c8e3
Merge branch 'develop' into master
pantianying 6e5df6c
fix
pantianying a1e9a4d
fix fmt
pantianying 068983b
add generic config.Load support
pantianying c3d2c9c
fix bug
pantianying 754252b
add hessain.Object to genericfilter
pantianying d7be686
add support
pantianying 4271bcc
fix
pantianying 1faca0c
generic filter add slice support
pantianying cd1d707
change code style
pantianying 57cbc46
fix code
pantianying 0c6e36c
fix bug
pantianying File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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" | ||
pantianying marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
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()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.