feat(accounts): Add methods to check if an account accepts certain messages or queries (interface assertion) (#19361)
This commit is contained in:
parent
c91660e55b
commit
d26fe653c7
@ -126,6 +126,23 @@ type Implementation struct {
|
||||
ExecuteHandlersSchema map[string]HandlerSchema
|
||||
}
|
||||
|
||||
// HasExec returns true if the account can execute the given msg.
|
||||
func (i Implementation) HasExec(m ProtoMsg) bool {
|
||||
_, ok := i.ExecuteHandlersSchema[MessageName(m)]
|
||||
return ok
|
||||
}
|
||||
|
||||
// HasQuery returns true if the account can execute the given request.
|
||||
func (i Implementation) HasQuery(m ProtoMsg) bool {
|
||||
_, ok := i.QueryHandlersSchema[MessageName(m)]
|
||||
return ok
|
||||
}
|
||||
|
||||
// HasInit returns true if the account uses the provided init message.
|
||||
func (i Implementation) HasInit(m ProtoMsg) bool {
|
||||
return i.InitHandlerSchema.RequestSchema.Name == MessageName(m)
|
||||
}
|
||||
|
||||
// MessageSchema defines the schema of a message.
|
||||
// A message can also define a state schema.
|
||||
type MessageSchema struct {
|
||||
|
||||
@ -56,4 +56,24 @@ func TestImplementation(t *testing.T) {
|
||||
_, err := impl.Query(ctx, &types.Int32Value{Value: 1})
|
||||
require.ErrorIs(t, err, errInvalidMessage)
|
||||
})
|
||||
|
||||
t.Run("Has* methods", func(t *testing.T) {
|
||||
ok := impl.HasExec(&types.StringValue{})
|
||||
require.True(t, ok)
|
||||
|
||||
ok = impl.HasExec(&types.Duration{})
|
||||
require.False(t, ok)
|
||||
|
||||
ok = impl.HasQuery(&types.StringValue{})
|
||||
require.True(t, ok)
|
||||
|
||||
ok = impl.HasQuery(&types.Duration{})
|
||||
require.False(t, ok)
|
||||
|
||||
ok = impl.HasInit(&types.StringValue{})
|
||||
require.True(t, ok)
|
||||
|
||||
ok = impl.HasInit(&types.Duration{})
|
||||
require.False(t, ok)
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user