Skip to content

Commit

Permalink
feat(accounts): Add methods to check if an account accepts certain me…
Browse files Browse the repository at this point in the history
…ssages or queries (interface assertion) (#19361)
  • Loading branch information
testinginprod authored Feb 6, 2024
1 parent c91660e commit d26fe65
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
17 changes: 17 additions & 0 deletions x/accounts/internal/implementation/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions x/accounts/internal/implementation/implementation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit d26fe65

Please sign in to comment.