Skip to content

feat(router)!: add UseOuter middleware layer and update Predicate signature#458

Open
fluffur wants to merge 6 commits into
gotd:mainfrom
fluffur:allow-predicates-access-context-and-add-pre-middlewares
Open

feat(router)!: add UseOuter middleware layer and update Predicate signature#458
fluffur wants to merge 6 commits into
gotd:mainfrom
fluffur:allow-predicates-access-context-and-add-pre-middlewares

Conversation

@fluffur

@fluffur fluffur commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

This pull request introduces a new global middleware layer called UseOuter and updates the Predicate signature to be context-aware.


Key Changes

  1. Added UseOuter Middleware: Introduced bot.UseOuter(), which allows registering global middlewares that run before route matching. This is useful for firewalls, blacklists, or pre-loading database configurations.
  2. Context in Predicates: Changed the Predicate signature so it now accepts *botapi.Context instead of *botapi.Update. This allows route guards to read data injected into the context by the new UseOuter middlewares (for example, custom command prefixes).

Execution Order

An incoming update now passes through the bot in this sequence:
bot.UseOuter() -> bot.Predicate() matching -> bot.Use() -> bot.Handler


BREAKING CHANGES and Migration Guide

The Predicate type signature has changed from func(u *Update) bool to func(c *Context) bool. Existing custom predicates will cause compilation errors and must be updated.

How to migrate:

Change the function argument from *botapi.Update to *botapi.Context. If your code requires raw update fields, access them through c.Update or use context helper methods.

// Old syntax
func MyPredicate(u *botapi.Update) bool {
    return u.Message != nil
}

// New syntax
func MyPredicate(c *botapi.Context) bool {
    return c.Update.Message != nil
}

Documentation and Examples

  • Added an "Outer Middleware" section to README.md explaining the new pipeline lifecycle.
  • Created a standalone example in examples/middleware/main.go to demonstrate how data moves from UseOuter to predicates and handlers.

fluffur added 3 commits June 17, 2026 21:14
Predicates can now read database configurations, flags, and other data
pre-loaded by the new UseOuter middleware layer.

BREAKING CHANGE: The `Predicate` type signature changed from
`func(u *Update) bool` to `func(c *Context) bool`. To fix this,
update your custom predicate functions to accept `*Context` and
read the update via `c.Update` if needed.
…ature

Added a dedicated section for "Outer Middleware" in the README to explain
the middleware lifecycle and pipeline execution order.

BREAKING CHANGE: Updated examples and documentation to reflect that
`Predicate` now accepts `*Context` instead of `*Update`.
@fluffur fluffur force-pushed the allow-predicates-access-context-and-add-pre-middlewares branch from 948d681 to c7f4425 Compare June 17, 2026 16:16
@codecov

codecov Bot commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.18182% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 78.81%. Comparing base (3466ee3) to head (2e6a290).

Files with missing lines Patch % Lines
on.go 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #458      +/-   ##
==========================================
+ Coverage   78.78%   78.81%   +0.02%     
==========================================
  Files         118      118              
  Lines        8316     8325       +9     
==========================================
+ Hits         6552     6561       +9     
  Misses       1237     1237              
  Partials      527      527              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fluffur fluffur force-pushed the allow-predicates-access-context-and-add-pre-middlewares branch from bfb6600 to 76e2023 Compare June 17, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant