🔥 feat: Add support for configuring the Regex engine on the router#4254
🔥 feat: Add support for configuring the Regex engine on the router#4254Claude wants to merge 19 commits into
Conversation
- Created RegexEngine and RegexCompiler interfaces - Updated path.go to use configured regex engine - Added RegexEngine field to Config with DefaultRegexEngine - Updated router.go to pass RegexEngine to parseRoute - Added comprehensive tests for regex abstraction - Created example showing how to integrate coregex Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/3e2b7990-e9c5-41f6-afbf-ad29c8bbcfd0 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4254 +/- ##
==========================================
- Coverage 91.35% 91.35% -0.01%
==========================================
Files 132 132
Lines 12977 13066 +89
==========================================
+ Hits 11855 11936 +81
- Misses 708 713 +5
- Partials 414 417 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/3e2b7990-e9c5-41f6-afbf-ad29c8bbcfd0 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
- Added regex-engines.md guide with coregex integration examples - Documented RegexEngine and RegexCompiler interfaces - Included performance benchmarks and use cases - Added custom implementation examples Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/3e2b7990-e9c5-41f6-afbf-ad29c8bbcfd0 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
|
- Removed examples/regex-engine folder as requested - Updated docs/guide/regex-engines.md to remove reference to examples folder - All markdown linting issues resolved Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/9e71d891-0cc3-4786-8921-d994e500db6f Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Fixed in d7629bf:
|
|
@claude[agent] I'm not sure about the addition of I believe the approach should be as straightforward as our JSONEncoder and Decoder implementations within the app configuration. |
Moved regex types from separate regex.go file into path.go where they are used. This follows the simpler pattern similar to JSONEncoder/Decoder and reduces over-engineering by keeping related code together. Changes: - Removed regex.go and moved RegexEngine/RegexCompiler types to path.go - Moved tests from regex_test.go to path_test.go - All tests pass, no linting issues Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/175129f3-3f03-4a29-9673-b0521797d587 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Simplified in ffc10c3. Removed the separate |
|
@claude[agent] Looking at the guide docs/guide/regex-engines.md and code. Using a different regex library is too complicated. We can simplify this. |
Following feedback to simplify the regex engine abstraction, this commit
refactors the implementation to use a simple function type instead of
interface-based approach.
Key changes:
- Replace RegexEngine interface with RegexHandler function type
- RegexHandler is now: func(pattern string) RegexCompiler
- Add defaultRegexHandler function that wraps regexp.MustCompile
- Update all references from RegexEngine to RegexHandler
- Simplify documentation to show direct usage without adapter types
This follows the same pattern as JSONEncoder/JSONDecoder in the codebase,
making it much simpler for users to adopt alternative regex engines:
Before (required adapter types):
type CoregexEngine struct{}
func (CoregexEngine) MustCompile(pattern string) fiber.RegexCompiler {...}
app := fiber.New(fiber.Config{RegexEngine: CoregexEngine{}})
After (just a function):
app := fiber.New(fiber.Config{
RegexHandler: func(pattern string) fiber.RegexCompiler {
return coregex.MustCompile(pattern)
},
})
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
|
@claude[agent] Why are you adding a handler, instead of just using // stdlib // coregex — same API The app config option should just take |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
@copilot apply changes based on the comments in this thread |
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/f9b8dbc7-cc72-4f60-b345-5b668e0ceed6 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/f9b8dbc7-cc72-4f60-b345-5b668e0ceed6 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/f9b8dbc7-cc72-4f60-b345-5b668e0ceed6 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Applied the review-thread follow-ups in 4d138de. |
|
@copilot apply changes based on the comments in this thread |
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/bb345d14-e46f-4eb3-80d3-76fb16369d8d Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/bb345d14-e46f-4eb3-80d3-76fb16369d8d Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/bb345d14-e46f-4eb3-80d3-76fb16369d8d Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Applied the follow-up changes in 67f902d. |
There was a problem hiding this comment.
⚠️ Performance Alert ⚠️
Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.
| Benchmark suite | Current: 67f902d | Previous: d5c49b2 | Ratio |
|---|---|---|---|
Benchmark_Compress/Zstd (github.com/gofiber/fiber/v3/middleware/compress) - B/op |
1 B/op |
0 B/op |
+∞ |
This comment was automatically generated by workflow using github-action-benchmark.
|
@copilot resolve the merge conflicts in this pull request |
…dd-alternative-regex-support # Conflicts: # prefork.go Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Resolved the merge conflict in f9d2dcc by merging |
| case regexConstraint: | ||
| if c.RegexCompiler == nil { | ||
| matcher := c.regexMatcher | ||
| if matcher == nil { | ||
| matcher = c.RegexCompiler | ||
| } |
There was a problem hiding this comment.
Fixed in b131e19. Constraint.CheckConstraint now treats typed-nil regex matchers the same as nil and returns false instead of calling MatchString on a nil receiver.
| // RegexCompiler defines the interface for regex pattern matching. | ||
| // Both *regexp.Regexp and alternative engines like *coregex.Regex implement this method. | ||
| type RegexCompiler interface { | ||
| // MatchString reports whether the string s contains any match of the regex pattern. | ||
| MatchString(s string) bool |
There was a problem hiding this comment.
Fixed in b131e19. I renamed the new interface to RegexMatcher and updated the related validation errors/docs so it no longer clashes with Constraint.RegexCompiler.
| parsedRaw := parseRoute(pathRaw, app.config.RegexHandler, app.customConstraints...) | ||
| parsedPretty := parseRoute(pathPretty, app.config.RegexHandler, app.customConstraints...) |
There was a problem hiding this comment.
Addressed in b131e19 by documenting the current registration behavior. The public config/docs now note that RegexHandler may be invoked more than once per route while Fiber parses raw and normalized patterns.
| // Test_RegexHandler_Default verifies the default regex handler works correctly. | ||
| func Test_RegexHandler_Default(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| require.True(t, regexp.MustCompile(`\d+`).MatchString("123")) | ||
| require.False(t, regexp.MustCompile(`\d+`).MatchString("abc")) |
There was a problem hiding this comment.
Fixed in b131e19. I rewrote that test to assert Fiber’s default RegexHandler wiring instead of just re-testing regexp.MustCompile directly.
|
|
||
| ## Notes | ||
|
|
||
| - Regex patterns are compiled once during route registration, so the performance improvement is in the matching phase |
There was a problem hiding this comment.
Fixed in b131e19. The guide now says regex patterns are compiled during route registration and may be compiled more than once while Fiber parses raw and normalized route patterns.
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/080c86b4-4942-4e6b-a12f-802358f0c71f Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Thanks for asking me to work on this. I will get started on it and keep this PR's description up to date as I form a plan and make progress.