-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from tdakkota/feature/client-enhancement
Add codegeneration for update handlers
- Loading branch information
Showing
12 changed files
with
1,714 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
{{ define "handlers" }} | ||
{{ $pkg := $.Package }} | ||
{{ template "header" $ }} | ||
|
||
type handler = func(UpdateContext, UpdateClass) error | ||
|
||
type UpdateDispatcher struct { | ||
handlers map[int]handler | ||
} | ||
|
||
func NewUpdateDispatcher() UpdateDispatcher { | ||
return UpdateDispatcher{ | ||
handlers: map[int]handler{}, | ||
} | ||
} | ||
|
||
type UpdateContext struct { | ||
context.Context | ||
|
||
Users map[int]*User | ||
Chats map[int]*Chat | ||
init bool | ||
} | ||
|
||
func (u *UpdateContext) lazyInitFromUpdates(updates *Updates) { | ||
if u.init { | ||
return | ||
} | ||
|
||
u.init = true | ||
u.Users = make(map[int]*User, len(updates.Users)) | ||
for _, class := range updates.Users { | ||
user, ok := class.(*User) | ||
if !ok { | ||
continue | ||
} | ||
u.Users[user.ID] = user | ||
} | ||
|
||
u.Chats = make(map[int]*Chat, len(updates.Chats)) | ||
for _, class := range updates.Chats { | ||
chat, ok := class.(*Chat) | ||
if !ok { | ||
continue | ||
} | ||
u.Chats[chat.ID] = chat | ||
} | ||
} | ||
|
||
func (u UpdateDispatcher) Handle(ctx context.Context, updates *Updates) error { | ||
uctx := UpdateContext{ | ||
Context: ctx, | ||
} | ||
|
||
for _, update := range updates.Updates { | ||
uctx.lazyInitFromUpdates(updates) | ||
switch update.(type) { | ||
{{- range $s:= $.Structs }}{{ if eq $s.Interface "UpdateClass" }} | ||
case *{{ $s.Name }}: | ||
if handler, ok := u.handlers[{{ $s.Name }}TypeID]; ok { | ||
if err := handler(uctx, update); err != nil { | ||
return err | ||
} | ||
} | ||
{{- end }}{{ end }} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
{{- range $s:= $.Structs }}{{ if eq $s.Interface "UpdateClass" }} | ||
{{ $eventName := trimPrefix $s.Name "Update"}} | ||
// {{ $eventName }}Handler is a {{ $eventName }} event handler. | ||
type {{ $eventName }}Handler func(ctx UpdateContext, update *{{ $s.Name }}) error | ||
|
||
// On{{ $eventName }} sets {{ $eventName }} handler. | ||
func (u UpdateDispatcher) On{{ $eventName }}(handler {{ $eventName }}Handler) { | ||
u.handlers[{{ $s.Name }}TypeID] = func(ctx UpdateContext, update UpdateClass) error { | ||
return handler(ctx, update.(*{{ $s.Name }})) | ||
} | ||
} | ||
{{- end }}{{ end }} | ||
|
||
{{ end }} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.