rpc: remove 'exported or builtin' restriction for parameters (#20332)
* rpc: remove 'exported or builtin' restriction for parameters There is no technial reason for this restriction because package reflect can create values of any type. Requiring parameters and return values to be exported causes a lot of noise in package exports. * rpc: fix staticcheck warnings
This commit is contained in:
committed by
Péter Szilágyi
parent
9c6cf960b4
commit
8008c5b1fa
@ -25,7 +25,6 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
)
|
||||
@ -139,16 +138,14 @@ func newCallback(receiver, fn reflect.Value) *callback {
|
||||
c := &callback{fn: fn, rcvr: receiver, errPos: -1, isSubscribe: isPubSub(fntype)}
|
||||
// Determine parameter types. They must all be exported or builtin types.
|
||||
c.makeArgTypes()
|
||||
if !allExportedOrBuiltin(c.argTypes) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Verify return types. The function must return at most one error
|
||||
// and/or one other non-error value.
|
||||
outs := make([]reflect.Type, fntype.NumOut())
|
||||
for i := 0; i < fntype.NumOut(); i++ {
|
||||
outs[i] = fntype.Out(i)
|
||||
}
|
||||
if len(outs) > 2 || !allExportedOrBuiltin(outs) {
|
||||
if len(outs) > 2 {
|
||||
return nil
|
||||
}
|
||||
// If an error is returned, it must be the last returned value.
|
||||
@ -218,27 +215,6 @@ func (c *callback) call(ctx context.Context, method string, args []reflect.Value
|
||||
return results[0].Interface(), nil
|
||||
}
|
||||
|
||||
// Is this an exported - upper case - name?
|
||||
func isExported(name string) bool {
|
||||
rune, _ := utf8.DecodeRuneInString(name)
|
||||
return unicode.IsUpper(rune)
|
||||
}
|
||||
|
||||
// Are all those types exported or built-in?
|
||||
func allExportedOrBuiltin(types []reflect.Type) bool {
|
||||
for _, typ := range types {
|
||||
for typ.Kind() == reflect.Ptr {
|
||||
typ = typ.Elem()
|
||||
}
|
||||
// PkgPath will be non-empty even for an exported type,
|
||||
// so we need to check the type name as well.
|
||||
if !isExported(typ.Name()) && typ.PkgPath() != "" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Is t context.Context or *context.Context?
|
||||
func isContextType(t reflect.Type) bool {
|
||||
for t.Kind() == reflect.Ptr {
|
||||
|
Reference in New Issue
Block a user