-
-
Notifications
You must be signed in to change notification settings - Fork 142
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 #363 from gotd/feat/client-middlewares
feat(telegram): reimplement middlewares
- Loading branch information
Showing
5 changed files
with
84 additions
and
2 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,37 @@ | ||
package telegram | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gotd/td/bin" | ||
"github.com/gotd/td/tg" | ||
) | ||
|
||
// InvokeFunc implements tg.Invoker as function. | ||
type InvokeFunc func(ctx context.Context, input bin.Encoder, output bin.Decoder) error | ||
|
||
// Invoke implements tg.Invoker. | ||
func (i InvokeFunc) Invoke(ctx context.Context, input bin.Encoder, output bin.Decoder) error { | ||
return i(ctx, input, output) | ||
} | ||
|
||
// Middleware returns new InvokeFunc for next invoker. | ||
type Middleware interface { | ||
Handle(next tg.Invoker) InvokeFunc | ||
} | ||
|
||
// MiddlewareFunc implements Middleware as function. | ||
type MiddlewareFunc func(next tg.Invoker) InvokeFunc | ||
|
||
// Handle implements Middleware. | ||
func (m MiddlewareFunc) Handle(next tg.Invoker) InvokeFunc { return m(next) } | ||
|
||
func chainMiddlewares(invoker tg.Invoker, chain ...Middleware) tg.Invoker { | ||
if len(chain) == 0 { | ||
return invoker | ||
} | ||
for i := len(chain) - 1; i >= 0; i-- { | ||
invoker = chain[i].Handle(invoker) | ||
} | ||
return invoker | ||
} |
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,37 @@ | ||
package telegram | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/gotd/td/bin" | ||
"github.com/gotd/td/tg" | ||
) | ||
|
||
type testPrintInvoker struct { | ||
t *testing.T | ||
} | ||
|
||
func (t testPrintInvoker) Invoke(ctx context.Context, input bin.Encoder, output bin.Decoder) error { | ||
t.t.Log("invoke") | ||
return nil | ||
} | ||
|
||
func TestExampleMiddleware(t *testing.T) { | ||
_ = chainMiddlewares(testPrintInvoker{t: t}, | ||
MiddlewareFunc(func(next tg.Invoker) InvokeFunc { | ||
return func(ctx context.Context, input bin.Encoder, output bin.Decoder) error { | ||
t.Log("First") | ||
defer t.Log("After first") | ||
return next.Invoke(ctx, input, output) | ||
} | ||
}), | ||
MiddlewareFunc(func(next tg.Invoker) InvokeFunc { | ||
return func(ctx context.Context, input bin.Encoder, output bin.Decoder) error { | ||
t.Log("Second") | ||
defer t.Log("After second") | ||
return next.Invoke(ctx, input, output) | ||
} | ||
}), | ||
).Invoke(context.Background(), nil, nil) | ||
} |
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