1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +00:00

minor: versioning: Match: store the matched version and revert the last change

Former-commit-id: e7aa04671d3f54650bb194a97300b6a89e1b0d2b
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-06-20 09:39:42 +03:00
parent 3c86fa8010
commit 878484204e
6 changed files with 35 additions and 18 deletions

View File

@@ -22,7 +22,9 @@ const (
// ctx.Next()
// }
//
// DEPRECATED: May 06 2020: Use `ctx.SetVersion(ctx.URLParamDefault("version", "1"))` instead.
// DEPRECATED: Use:
// version := ctx.URLParamDefault("version", "1")
// versioning.SetVersion(ctx, version) instead.
Key = "iris.api.version"
// NotFound is the key that can be used inside a `Map` or inside `ctx.SetVersion(versioning.NotFound)`
// to tell that a version wasn't found, therefore the not found handler should handle the request instead.
@@ -56,6 +58,8 @@ var NotFoundHandler = func(ctx context.Context) {
//
// However, the end developer can also set a custom version for a handler via a middleware by using the context's store key
// for versions (see `Key` for further details on that).
//
// See `SetVersion` too.
func GetVersion(ctx context.Context) string {
// firstly by context store, if manually set by a middleware.
version := ctx.Values().GetString(ctx.Application().ConfigurationReadOnly().GetVersionContextKey())
@@ -96,3 +100,10 @@ func GetVersion(ctx context.Context) string {
return ""
}
// SetVersion force-sets the API Version.
// It can be used inside a middleware.
// See `GetVersion` too.
func SetVersion(ctx context.Context, constraint string) {
ctx.Values().Set(ctx.Application().ConfigurationReadOnly().GetVersionContextKey(), constraint)
}

View File

@@ -17,7 +17,7 @@ func TestGetVersion(t *testing.T) {
app.Get("/", writeVesion)
app.Get("/manual", func(ctx iris.Context) {
ctx.SetVersion("11.0.5")
versioning.SetVersion(ctx, "11.0.5")
ctx.Next()
}, writeVesion)

View File

@@ -9,23 +9,39 @@ import (
// If reports whether the "version" is matching to the "is".
// the "is" can be a constraint like ">= 1, < 3".
func If(v string, is string) bool {
_, ok := check(v, is)
return ok
}
func check(v string, is string) (string, bool) {
ver, err := version.NewVersion(v)
if err != nil {
return false
return "", false
}
constraints, err := version.NewConstraint(is)
if err != nil {
return false
return "", false
}
return constraints.Check(ver)
// return the extracted version from request, even if not matched.
return ver.String(), constraints.Check(ver)
}
// Match acts exactly the same as `If` does but instead it accepts
// a Context, so it can be called by a handler to determinate the requested version.
//
// If matched then it sets the "X-API-Version" response header and
// stores the matched version into Context (see `GetVersion` too).
func Match(ctx context.Context, expectedVersion string) bool {
return If(GetVersion(ctx), expectedVersion)
versionString, matched := check(GetVersion(ctx), expectedVersion)
if !matched {
return false
}
SetVersion(ctx, versionString)
ctx.Header("X-API-Version", versionString)
return true
}
// Map is a map of versions targets to a handlers,
@@ -63,7 +79,7 @@ func NewMatcher(versions Map) context.Handler {
}
// pass the not matched version so the not found handler can have knowedge about it.
// ctx.SetVersion(versionString)
// SetVersion(ctx, versionString)
// or let a manual cal of GetVersion(ctx) do that instead.
notFoundHandler(ctx)
}