gRPC interface reflection. (#6722)
* WIP on gRPC interface reflection. * Update docs in proto * Add tests * Add test * Add route inside router * Address nits * ListInterfaces -> ListAllInterfaces * Fix proto lint * Remove stray println * Update proto/cosmos/base/reflection/v1beta1/reflection.proto Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * Update codec/types/interface_registry.go Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * Update codec/types/interface_registry.go Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> * Add godoc Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
co-authored by
Alexander Bezobchuk
Amaury Martiny
parent
c25b3b9c44
commit
3f81c0a18d
@@ -42,6 +42,13 @@ type InterfaceRegistry interface {
|
||||
// Ex:
|
||||
// registry.RegisterImplementations((*sdk.Msg)(nil), &MsgSend{}, &MsgMultiSend{})
|
||||
RegisterImplementations(iface interface{}, impls ...proto.Message)
|
||||
|
||||
// ListAllInterfaces list the type URLs of all registered interfaces.
|
||||
ListAllInterfaces() []string
|
||||
|
||||
// ListImplementations lists the valid type URLs for the given interface name that can be used
|
||||
// for the provided interface type URL.
|
||||
ListImplementations(ifaceTypeURL string) []string
|
||||
}
|
||||
|
||||
// UnpackInterfacesMessage is meant to extend protobuf types (which implement
|
||||
@@ -111,6 +118,33 @@ func (registry *interfaceRegistry) RegisterImplementations(iface interface{}, im
|
||||
registry.interfaceImpls[ityp] = imap
|
||||
}
|
||||
|
||||
func (registry *interfaceRegistry) ListAllInterfaces() []string {
|
||||
interfaceNames := registry.interfaceNames
|
||||
keys := make([]string, 0, len(interfaceNames))
|
||||
for key := range interfaceNames {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
func (registry *interfaceRegistry) ListImplementations(ifaceName string) []string {
|
||||
typ, ok := registry.interfaceNames[ifaceName]
|
||||
if !ok {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
impls, ok := registry.interfaceImpls[typ.Elem()]
|
||||
if !ok {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
keys := make([]string, 0, len(impls))
|
||||
for key := range impls {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
func (registry *interfaceRegistry) UnpackAny(any *Any, iface interface{}) error {
|
||||
if any.TypeUrl == "" {
|
||||
// if TypeUrl is empty return nil because without it we can't actually unpack anything
|
||||
|
||||
Reference in New Issue
Block a user