From 7aa2d1f9d50c98efdd8a8a96e4d86629114b69f7 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Tue, 5 Jan 2021 18:16:32 +0200 Subject: [PATCH] make versioning.Group a Party compatible by using a type alias on its embedded field was reported as missing of the a Party method, the type alias is a good hack to solve that --- _examples/routing/versioning/main.go | 11 +++++++++++ versioning/group.go | 11 ++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/_examples/routing/versioning/main.go b/_examples/routing/versioning/main.go index 3502173f..f234c9af 100644 --- a/_examples/routing/versioning/main.go +++ b/_examples/routing/versioning/main.go @@ -67,6 +67,10 @@ func examplePerParty(app *iris.Application) { usersAPIV2.Post("/", func(ctx iris.Context) { ctx.Writef("v2 resource: /api/users post handler") }) + + // version 3, pass it as a common iris.Party. + usersAPIV3 := versioning.NewGroup(usersAPI, ">= 3, < 4") + registerAPIV3(usersAPIV3) } func catsVersionExactly1Handler(ctx iris.Context) { @@ -76,3 +80,10 @@ func catsVersionExactly1Handler(ctx iris.Context) { func catsV2Handler(ctx iris.Context) { ctx.Writef("v2 resource: /api/cats handler") } + +func registerAPIV3(p iris.Party) { + p.Get("/", func(ctx iris.Context) { + ctx.Writef("v3 resource: /api/users handler") + }) + // [...] +} diff --git a/versioning/group.go b/versioning/group.go index 9e7f0e06..d74d96c1 100644 --- a/versioning/group.go +++ b/versioning/group.go @@ -5,10 +5,15 @@ import ( "github.com/kataras/iris/v12/core/router" ) +// API is a type alias of router.Party. +// This is required in order for a Group instance +// to implement the Party interface without field conflict. +type API = router.Party + // Group is a group of version-based routes. // One version per one or more routes. type Group struct { - router.Party + API // Information not currently in-use. version string @@ -32,7 +37,7 @@ func NewGroup(r router.Party, version string) *Group { r.UseOnce(Handler(version)) // this is required in order to not populate this middleware to the next group. return &Group{ - Party: r, + API: r, version: version, } } @@ -43,7 +48,7 @@ func (g *Group) Deprecated(options DeprecationOptions) *Group { // store it for future use, e.g. collect all deprecated APIs and notify the developer. g.deprecation = options - g.Party.UseOnce(func(ctx *context.Context) { + g.API.UseOnce(func(ctx *context.Context) { WriteDeprecated(ctx, options) ctx.Next() })