mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-11-30 18:53:30 +00:00
bot: Clarify unexported *Context field
This commit is contained in:
parent
917459611d
commit
17dbccccc3
|
@ -16,6 +16,25 @@ import (
|
|||
"github.com/diamondburned/arikawa/v3/utils/handler"
|
||||
)
|
||||
|
||||
type testUnexportedCtx struct {
|
||||
ctx *Context
|
||||
}
|
||||
|
||||
func TestUnexportedCtx(t *testing.T) {
|
||||
s := &state.State{
|
||||
Cabinet: store.NoopCabinet,
|
||||
}
|
||||
|
||||
_, err := New(s, &testUnexportedCtx{})
|
||||
if err == nil {
|
||||
t.Fatal("New returned unexpected nil error")
|
||||
}
|
||||
|
||||
if !strings.Contains(err.Error(), "no exported field with *bot.Context found") {
|
||||
t.Fatal("unexpected New error:", err)
|
||||
}
|
||||
}
|
||||
|
||||
type testc struct {
|
||||
Ctx *Context
|
||||
Return chan interface{}
|
||||
|
|
|
@ -14,12 +14,13 @@ var (
|
|||
typeMessageCreate = reflect.TypeOf((*gateway.MessageCreateEvent)(nil))
|
||||
typeMessageUpdate = reflect.TypeOf((*gateway.MessageUpdateEvent)(nil))
|
||||
|
||||
typeIError = reflect.TypeOf((*error)(nil)).Elem()
|
||||
typeIManP = reflect.TypeOf((*ManualParser)(nil)).Elem()
|
||||
typeICusP = reflect.TypeOf((*CustomParser)(nil)).Elem()
|
||||
typeIParser = reflect.TypeOf((*Parser)(nil)).Elem()
|
||||
typeIUsager = reflect.TypeOf((*Usager)(nil)).Elem()
|
||||
typeSetupFn = methodType((*CanSetup)(nil), "Setup")
|
||||
typeContextPtr = reflect.TypeOf((*Context)(nil))
|
||||
typeIError = reflect.TypeOf((*error)(nil)).Elem()
|
||||
typeIManP = reflect.TypeOf((*ManualParser)(nil)).Elem()
|
||||
typeICusP = reflect.TypeOf((*CustomParser)(nil)).Elem()
|
||||
typeIParser = reflect.TypeOf((*Parser)(nil)).Elem()
|
||||
typeIUsager = reflect.TypeOf((*Usager)(nil)).Elem()
|
||||
typeSetupFn = methodType((*CanSetup)(nil), "Setup")
|
||||
)
|
||||
|
||||
func methodType(iface interface{}, name string) reflect.Type {
|
||||
|
@ -351,14 +352,10 @@ func (sub *Subcommand) InitCommands(ctx *Context) error {
|
|||
}
|
||||
|
||||
func (sub *Subcommand) fillStruct(ctx *Context) error {
|
||||
for i := 0; i < sub.cmdValue.NumField(); i++ {
|
||||
for i := 0; i < sub.cmdType.NumField(); i++ {
|
||||
field := sub.cmdValue.Field(i)
|
||||
|
||||
if !field.CanSet() || !field.CanInterface() {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := field.Interface().(*Context); !ok {
|
||||
if !field.CanSet() || field.Type() != typeContextPtr {
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -366,7 +363,7 @@ func (sub *Subcommand) fillStruct(ctx *Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
return errors.New("no fields with *bot.Context found")
|
||||
return errors.New("no exported field with *bot.Context found")
|
||||
}
|
||||
|
||||
func (sub *Subcommand) parseCommands() error {
|
||||
|
|
Loading…
Reference in a new issue