Add new PartyConfigure helper

This commit is contained in:
Gerasimos (Makis) Maropoulos 2021-02-17 16:50:36 +02:00
parent 5ccd80a6cd
commit cbf70a7bcf
No known key found for this signature in database
GPG Key ID: A771A828097B36C7
3 changed files with 37 additions and 0 deletions

View File

@ -28,6 +28,8 @@ The codebase for Dependency Injection, Internationalization and localization and
## Fixes and Improvements ## Fixes and Improvements
- New `PartyConfigure(relativePath string, partyReg PartyConfigurator) Party` helper, registers a children Party like `Party` and `PartyFunc` but instead it accepts a structure value (useful when the api's dependencies amount are too much to pass on a function).
- **New feature:** add the ability to set custom error handlers on path type parameters errors (existing or custom ones). Example Code: - **New feature:** add the ability to set custom error handlers on path type parameters errors (existing or custom ones). Example Code:
```go ```go

View File

@ -852,6 +852,28 @@ func (api *APIBuilder) PartyFunc(relativePath string, partyBuilderFunc func(p Pa
return p return p
} }
// PartyConfigurator is an interface which all child parties that are registered
// through `PartyConfigure` should implement.
type PartyConfigurator interface {
Configure(parent Party)
}
// PartyConfigure like `Party` and `PartyFunc` registers a new children Party but instead it accepts a struct value.
// It initializes a new children Party and executes the PartyConfigurator's Configure.
// Useful when the api's dependencies amount are too much to pass on a function.
//
// Usage:
// app.PartyConfigure("/users", &api.UsersAPI{UserRepository: ..., ...})
// Where UsersAPI looks like:
// type UsersAPI struct { [...] }
// func(api *UsersAPI) Configure(router iris.Party) {
// router.Get("/{id:uuid}", api.getUser)
// [...]
// }
func (api *APIBuilder) PartyConfigure(relativePath string, partyReg PartyConfigurator) Party {
return api.PartyFunc(relativePath, partyReg.Configure)
}
// Subdomain returns a new party which is responsible to register routes to // Subdomain returns a new party which is responsible to register routes to
// this specific "subdomain". // this specific "subdomain".
// //

View File

@ -87,6 +87,19 @@ type Party interface {
// //
// Look `Party` for more. // Look `Party` for more.
PartyFunc(relativePath string, partyBuilderFunc func(p Party)) Party PartyFunc(relativePath string, partyBuilderFunc func(p Party)) Party
// PartyConfigure like `Party` and `PartyFunc` registers a new children Party but instead it accepts a struct value.
// It initializes a new children Party and executes the PartyConfigurator's Configure.
// Useful when the api's dependencies amount are too much to pass on a function.
//
// Usage:
// app.PartyConfigure("/users", &api.UsersAPI{UserRepository: ..., ...})
// Where UsersAPI looks like:
// type UsersAPI struct { [...] }
// func(api *UsersAPI) Configure(router iris.Party) {
// router.Get("/{id:uuid}", api.getUser)
// [...]
// }
PartyConfigure(relativePath string, partyReg PartyConfigurator) Party
// Subdomain returns a new party which is responsible to register routes to // Subdomain returns a new party which is responsible to register routes to
// this specific "subdomain". // this specific "subdomain".
// //