gotd: move update dispatcher out of generator
This commit is contained in:
@@ -2,86 +2,6 @@
|
|||||||
{{ $pkg := $.Package }}
|
{{ $pkg := $.Package }}
|
||||||
{{ template "header" $ }}
|
{{ template "header" $ }}
|
||||||
|
|
||||||
type Handler = func(context.Context, Entities, UpdateClass) error
|
|
||||||
|
|
||||||
type UpdateDispatcher struct {
|
|
||||||
handlers map[uint32]Handler
|
|
||||||
fallback Handler
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewUpdateDispatcher() UpdateDispatcher {
|
|
||||||
return UpdateDispatcher{
|
|
||||||
handlers: map[uint32]Handler{},
|
|
||||||
fallback: func(ctx context.Context, entities Entities, class UpdateClass) error { return nil },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Entities struct {
|
|
||||||
Short bool
|
|
||||||
Users map[int64]*User
|
|
||||||
Chats map[int64]*Chat
|
|
||||||
Channels map[int64]*Channel
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *Entities) short() {
|
|
||||||
u.Short = true
|
|
||||||
u.Users = make(map[int64]*User, 0)
|
|
||||||
u.Chats = make(map[int64]*Chat, 0)
|
|
||||||
u.Channels = make(map[int64]*Channel, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle implements UpdateDispatcher.
|
|
||||||
func (u UpdateDispatcher) Handle(ctx context.Context, updates UpdatesClass) error {
|
|
||||||
var (
|
|
||||||
e Entities
|
|
||||||
upds []UpdateClass
|
|
||||||
)
|
|
||||||
switch u := updates.(type) {
|
|
||||||
case *Updates:
|
|
||||||
upds = u.Updates
|
|
||||||
e.Users = u.MapUsers().NotEmptyToMap()
|
|
||||||
chats := u.MapChats()
|
|
||||||
e.Chats = chats.ChatToMap()
|
|
||||||
e.Channels = chats.ChannelToMap()
|
|
||||||
case *UpdatesCombined:
|
|
||||||
upds = u.Updates
|
|
||||||
e.Users = u.MapUsers().NotEmptyToMap()
|
|
||||||
chats := u.MapChats()
|
|
||||||
e.Chats = chats.ChatToMap()
|
|
||||||
e.Channels = chats.ChannelToMap()
|
|
||||||
case *UpdateShort:
|
|
||||||
upds = []UpdateClass{u.Update}
|
|
||||||
e.short()
|
|
||||||
default:
|
|
||||||
// *UpdateShortMessage
|
|
||||||
// *UpdateShortChatMessage
|
|
||||||
// *UpdateShortSentMessage
|
|
||||||
// *UpdatesTooLong
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
for _, update := range upds {
|
|
||||||
multierr.AppendInto(&err, u.dispatch(ctx, e, update))
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u UpdateDispatcher) dispatch(ctx context.Context, e Entities, update UpdateClass) error {
|
|
||||||
if update == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err := u.fallback(ctx, e, update); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
typeID := update.TypeID()
|
|
||||||
handler, ok := u.handlers[typeID]
|
|
||||||
if ok {
|
|
||||||
return handler(ctx, e, update)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
{{- range $s := $.Structs }}{{ if eq $s.Interface "UpdateClass" }}
|
{{- range $s := $.Structs }}{{ if eq $s.Interface "UpdateClass" }}
|
||||||
{{ $eventName := trimPrefix $s.Name "Update"}}
|
{{ $eventName := trimPrefix $s.Name "Update"}}
|
||||||
// {{ $eventName }}Handler is a {{ $eventName }} event handler.
|
// {{ $eventName }}Handler is a {{ $eventName }} event handler.
|
||||||
|
|||||||
Generated
+87
@@ -0,0 +1,87 @@
|
|||||||
|
package tg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"go.uber.org/multierr"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Handler = func(context.Context, Entities, UpdateClass) error
|
||||||
|
|
||||||
|
type UpdateDispatcher struct {
|
||||||
|
handlers map[uint32]Handler
|
||||||
|
fallback Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUpdateDispatcher() UpdateDispatcher {
|
||||||
|
return UpdateDispatcher{
|
||||||
|
handlers: map[uint32]Handler{},
|
||||||
|
fallback: func(ctx context.Context, entities Entities, class UpdateClass) error { return nil },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Entities struct {
|
||||||
|
Short bool
|
||||||
|
Users map[int64]*User
|
||||||
|
Chats map[int64]*Chat
|
||||||
|
Channels map[int64]*Channel
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *Entities) short() {
|
||||||
|
u.Short = true
|
||||||
|
u.Users = make(map[int64]*User, 0)
|
||||||
|
u.Chats = make(map[int64]*Chat, 0)
|
||||||
|
u.Channels = make(map[int64]*Channel, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle implements UpdateDispatcher.
|
||||||
|
func (u UpdateDispatcher) Handle(ctx context.Context, updates UpdatesClass) error {
|
||||||
|
var (
|
||||||
|
e Entities
|
||||||
|
upds []UpdateClass
|
||||||
|
)
|
||||||
|
switch u := updates.(type) {
|
||||||
|
case *Updates:
|
||||||
|
upds = u.Updates
|
||||||
|
e.Users = u.MapUsers().NotEmptyToMap()
|
||||||
|
chats := u.MapChats()
|
||||||
|
e.Chats = chats.ChatToMap()
|
||||||
|
e.Channels = chats.ChannelToMap()
|
||||||
|
case *UpdatesCombined:
|
||||||
|
upds = u.Updates
|
||||||
|
e.Users = u.MapUsers().NotEmptyToMap()
|
||||||
|
chats := u.MapChats()
|
||||||
|
e.Chats = chats.ChatToMap()
|
||||||
|
e.Channels = chats.ChannelToMap()
|
||||||
|
case *UpdateShort:
|
||||||
|
upds = []UpdateClass{u.Update}
|
||||||
|
e.short()
|
||||||
|
default:
|
||||||
|
// *UpdateShortMessage
|
||||||
|
// *UpdateShortChatMessage
|
||||||
|
// *UpdateShortSentMessage
|
||||||
|
// *UpdatesTooLong
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
for _, update := range upds {
|
||||||
|
multierr.AppendInto(&err, u.dispatch(ctx, e, update))
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u UpdateDispatcher) dispatch(ctx context.Context, e Entities, update UpdateClass) error {
|
||||||
|
if update == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := u.fallback(ctx, e, update); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
typeID := update.TypeID()
|
||||||
|
handler, ok := u.handlers[typeID]
|
||||||
|
if ok {
|
||||||
|
return handler(ctx, e, update)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Generated
-80
@@ -31,86 +31,6 @@ var (
|
|||||||
_ = tdjson.Encoder{}
|
_ = tdjson.Encoder{}
|
||||||
)
|
)
|
||||||
|
|
||||||
type Handler = func(context.Context, Entities, UpdateClass) error
|
|
||||||
|
|
||||||
type UpdateDispatcher struct {
|
|
||||||
handlers map[uint32]Handler
|
|
||||||
fallback Handler
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewUpdateDispatcher() UpdateDispatcher {
|
|
||||||
return UpdateDispatcher{
|
|
||||||
handlers: map[uint32]Handler{},
|
|
||||||
fallback: func(ctx context.Context, entities Entities, class UpdateClass) error { return nil },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Entities struct {
|
|
||||||
Short bool
|
|
||||||
Users map[int64]*User
|
|
||||||
Chats map[int64]*Chat
|
|
||||||
Channels map[int64]*Channel
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *Entities) short() {
|
|
||||||
u.Short = true
|
|
||||||
u.Users = make(map[int64]*User, 0)
|
|
||||||
u.Chats = make(map[int64]*Chat, 0)
|
|
||||||
u.Channels = make(map[int64]*Channel, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle implements UpdateDispatcher.
|
|
||||||
func (u UpdateDispatcher) Handle(ctx context.Context, updates UpdatesClass) error {
|
|
||||||
var (
|
|
||||||
e Entities
|
|
||||||
upds []UpdateClass
|
|
||||||
)
|
|
||||||
switch u := updates.(type) {
|
|
||||||
case *Updates:
|
|
||||||
upds = u.Updates
|
|
||||||
e.Users = u.MapUsers().NotEmptyToMap()
|
|
||||||
chats := u.MapChats()
|
|
||||||
e.Chats = chats.ChatToMap()
|
|
||||||
e.Channels = chats.ChannelToMap()
|
|
||||||
case *UpdatesCombined:
|
|
||||||
upds = u.Updates
|
|
||||||
e.Users = u.MapUsers().NotEmptyToMap()
|
|
||||||
chats := u.MapChats()
|
|
||||||
e.Chats = chats.ChatToMap()
|
|
||||||
e.Channels = chats.ChannelToMap()
|
|
||||||
case *UpdateShort:
|
|
||||||
upds = []UpdateClass{u.Update}
|
|
||||||
e.short()
|
|
||||||
default:
|
|
||||||
// *UpdateShortMessage
|
|
||||||
// *UpdateShortChatMessage
|
|
||||||
// *UpdateShortSentMessage
|
|
||||||
// *UpdatesTooLong
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
for _, update := range upds {
|
|
||||||
multierr.AppendInto(&err, u.dispatch(ctx, e, update))
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u UpdateDispatcher) dispatch(ctx context.Context, e Entities, update UpdateClass) error {
|
|
||||||
if update == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err := u.fallback(ctx, e, update); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
typeID := update.TypeID()
|
|
||||||
handler, ok := u.handlers[typeID]
|
|
||||||
if ok {
|
|
||||||
return handler(ctx, e, update)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMessageHandler is a NewMessage event handler.
|
// NewMessageHandler is a NewMessage event handler.
|
||||||
type NewMessageHandler func(ctx context.Context, e Entities, update *UpdateNewMessage) error
|
type NewMessageHandler func(ctx context.Context, e Entities, update *UpdateNewMessage) error
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user