diff --git a/HISTORY.md b/HISTORY.md index 95b1836e..51e5d9fa 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -28,6 +28,8 @@ The codebase for Dependency Injection, Internationalization and localization and ## 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: ```go diff --git a/core/router/api_builder.go b/core/router/api_builder.go index 790052b3..e13ae6d3 100644 --- a/core/router/api_builder.go +++ b/core/router/api_builder.go @@ -852,6 +852,28 @@ func (api *APIBuilder) PartyFunc(relativePath string, partyBuilderFunc func(p Pa 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 // this specific "subdomain". // diff --git a/core/router/party.go b/core/router/party.go index 495e8f19..df485c03 100644 --- a/core/router/party.go +++ b/core/router/party.go @@ -87,6 +87,19 @@ type Party interface { // // Look `Party` for more. 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 // this specific "subdomain". //