feat(accounts): Add methods to check if an account accepts certain messages or queries (interface assertion) (#19361)

This commit is contained in:
testinginprod 2024-02-06 17:53:45 +01:00 committed by GitHub
parent c91660e55b
commit d26fe653c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 0 deletions

View File

@ -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 {

View File

@ -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)
})
}