remove any reference to the pre go1.9 context.Context and replace with iris.Context on some places that were forgotten

got an email feedback about this


Former-commit-id: b1caa261a0d425d8db2091bffb8cfd6065c4e1fa
This commit is contained in:
Gerasimos (Makis) Maropoulos 2019-08-11 15:43:47 +03:00
parent 943c3f77cb
commit 5c91440e46
11 changed files with 29 additions and 33 deletions

View File

@ -26,7 +26,7 @@ func newApp() *iris.Application {
// List the files inside the current requested directory if `IndexName` not found.
ShowList: false,
// If `ShowList` is true then this function will be used instead of the default one to show the list of files of a current requested directory(dir).
// DirList: func(ctx context.Context, dirName string, dir http.File) error { ... }
// DirList: func(ctx iris.Context, dirName string, dir http.File) error { ... }
//
// Optional validator that loops through each requested resource.
// AssetValidator: func(ctx iris.Context, name string) bool { ... }

View File

@ -147,7 +147,7 @@ func (h httpError) Error() string {
return fmt.Sprintf("Status Code: %d\nReason: %s", h.Code, h.Reason)
}
func fail(ctx context.Context, statusCode int, format string, a ...interface{}) {
func fail(ctx iris.Context, statusCode int, format string, a ...interface{}) {
err := httpError{
Code: statusCode,
Reason: fmt.Sprintf(format, a...),

View File

@ -2,23 +2,22 @@ package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
func main() {
app := iris.New()
app.Get("/", func(ctx context.Context) /* or iris.Context, it's the same for Go 1.9+. */ {
app.Get("/", func(ctx iris.Context) {
// GetReferrer extracts and returns the information from the "Referer" header as specified
// in https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy or by the URL query parameter "referer".
r := ctx.GetReferrer()
switch r.Type {
case context.ReferrerSearch:
case iris.ReferrerSearch:
ctx.Writef("Search %s: %s\n", r.Label, r.Query)
ctx.Writef("Google: %s\n", r.GoogleType)
case context.ReferrerSocial:
case iris.ReferrerSocial:
ctx.Writef("Social %s\n", r.Label)
case context.ReferrerIndirect:
case iris.ReferrerIndirect:
ctx.Writef("Indirect: %s\n", r.URL)
}
})

View File

@ -10,10 +10,6 @@ import (
"github.com/kataras/golog"
"github.com/kataras/iris"
// Note:
// For some reason the latest vscode-go language extension does not provide enough intelligence (parameters documentation and go to definition features)
// for the `iris.Context` alias, therefore if you use VS Code, import the original import path of the `Context`, that will do it:
"github.com/kataras/iris/context"
)
// A Broker holds open client connections,
@ -76,7 +72,7 @@ func (b *Broker) listen() {
}
}
func (b *Broker) ServeHTTP(ctx context.Context) {
func (b *Broker) ServeHTTP(ctx iris.Context) {
// Make sure that the writer supports flushing.
flusher, ok := ctx.ResponseWriter().Flusher()
@ -172,7 +168,7 @@ func main() {
}()
app := iris.New()
app.Get("/", func(ctx context.Context) {
app.Get("/", func(ctx iris.Context) {
ctx.HTML(
`<html><head><title>SSE</title>` + script + `</head>
<body>

View File

@ -10,7 +10,7 @@ func main() {
// subdomains works with all available routers, like other features too.
app.Get("/", func(ctx context.Context) {
app.Get("/", func(ctx iris.Context) {
ctx.BeginTransaction(func(t *context.Transaction) {
// OPTIONAl STEP: , if true then the next transictions will not be executed if this transiction fails
// t.SetScope(context.RequestTransactionScope)

View File

@ -8,6 +8,7 @@ Example:
import (
"github.com/kataras/iris/_examples/mvc/login/datamodels"
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
@ -41,7 +42,7 @@ so theoretically, something like the following is permitted if it's really neces
// This is called where the return value from a controller's method functions
// is type of `User`.
// For example the `controllers/user_controller.go#GetBy`.
func (m User) Dispatch(ctx context.Context) {
func (m User) Dispatch(ctx iris.Context) {
if !m.IsValid() {
ctx.NotFound()
return

View File

@ -8,6 +8,7 @@ Example:
import (
"github.com/kataras/iris/_examples/mvc/overview/datamodels"
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)
@ -41,7 +42,7 @@ so theoretically, something like the following is permitted if it's really neces
// This is called where the return value from a controller's method functions
// is type of `Movie`.
// For example the `controllers/movie_controller.go#GetBy`.
func (m Movie) Dispatch(ctx context.Context) {
func (m Movie) Dispatch(ctx iris.Context) {
if !m.IsValid() {
ctx.NotFound()
return

View File

@ -663,7 +663,7 @@ func internalServerError(ctx iris.Context) {
ctx.WriteString("Oups something went wrong, try again")
}
func index(ctx context.Context) {
func index(ctx iris.Context) {
ctx.View("index.html")
}
```
@ -1149,7 +1149,7 @@ type Context interface {
// to be executed. Next handlers are being executed on iris because you can alt the
// error code and change it to a more specific one, i.e
// users := app.Party("/users")
// users.Done(func(ctx context.Context){ if ctx.StatusCode() == 400 { /* custom error code for /users */ }})
// users.Done(func(ctx iris.Context){ if ctx.StatusCode() == 400 { /* custom error code for /users */ }})
NotFound()
// +------------------------------------------------------------+

View File

@ -15,10 +15,10 @@ import (
// Create your own custom Context, put any fields you wanna need.
type MyContext struct {
// Optional Part 1: embed (optional but required if you don't want to override all context's methods)
context.Context // it's the context/context.go#context struct but you don't need to know it.
iris.Context
}
var _ context.Context = &MyContext{} // optionally: validate on compile-time if MyContext implements context.Context.
var _ iris.Context = &MyContext{} // optionally: validate on compile-time if MyContext implements context.Context.
// The only one important if you will override the Context
// with an embedded context.Context inside it.
@ -51,7 +51,7 @@ func main() {
// The only one Required:
// here is how you define how your own context will
// be created and acquired from the iris' generic context pool.
app.ContextPool.Attach(func() context.Context {
app.ContextPool.Attach(func() iris.Context {
return &MyContext{
// Optional Part 3:
Context: context.NewContext(app),
@ -62,14 +62,14 @@ func main() {
app.RegisterView(iris.HTML("./view", ".html"))
// register your route, as you normally do
app.Handle("GET", "/", recordWhichContextJustForProofOfConcept, func(ctx context.Context) {
app.Handle("GET", "/", recordWhichContextJustForProofOfConcept, func(ctx iris.Context) {
// use the context's overridden HTML method.
ctx.HTML("<h1> Hello from my custom context's HTML! </h1>")
})
// this will be executed by the MyContext.Context
// if MyContext is not directly define the View function by itself.
app.Handle("GET", "/hi/{firstname:alphabetical}", recordWhichContextJustForProofOfConcept, func(ctx context.Context) {
app.Handle("GET", "/hi/{firstname:alphabetical}", recordWhichContextJustForProofOfConcept, func(ctx iris.Context) {
firstname := ctx.Values().GetString("firstname")
ctx.ViewData("firstname", firstname)
@ -82,7 +82,7 @@ func main() {
}
// should always print "($PATH) Handler is executing from 'MyContext'"
func recordWhichContextJustForProofOfConcept(ctx context.Context) {
func recordWhichContextJustForProofOfConcept(ctx iris.Context) {
ctx.Application().Logger().Infof("(%s) Handler is executing from: '%s'", ctx.Path(), reflect.TypeOf(ctx).Elem().Name())
ctx.Next()
}

View File

@ -4,17 +4,16 @@ import (
"strings"
"github.com/kataras/iris"
"github.com/kataras/iris/context"
"github.com/kataras/iris/core/router"
)
/* A Router should contain all three of the following methods:
- HandleRequest should handle the request based on the Context.
HandleRequest(ctx context.Context)
HandleRequest(ctx iris.Context)
- Build should builds the handler, it's being called on router's BuildRouter.
Build(provider router.RoutesProvider) error
- RouteExists reports whether a particular route exists.
RouteExists(ctx context.Context, method, path string) bool
RouteExists(ctx iris.Context, method, path string) bool
For a more detailed, complete and useful example
you can take a look at the iris' router itself which is located at:
@ -30,7 +29,7 @@ type customRouter struct {
// HandleRequest a silly example which finds routes based only on the first part of the requested path
// which must be a static one as well, the rest goes to fill the parameters.
func (r *customRouter) HandleRequest(ctx context.Context) {
func (r *customRouter) HandleRequest(ctx iris.Context) {
path := ctx.Path()
ctx.Application().Logger().Infof("Requested resource path: %s", path)
@ -67,7 +66,7 @@ func (r *customRouter) Build(provider router.RoutesProvider) error {
return nil
}
func (r *customRouter) RouteExists(ctx context.Context, method, path string) bool {
func (r *customRouter) RouteExists(ctx iris.Context, method, path string) bool {
// [...]
return false
}
@ -79,12 +78,12 @@ func main() {
// your custom router if you fetch by the Route's Handler
// because they are middlewares under the hood, so you don't have to implement the logic of handling them manually,
// though you have to match what requested path is what route and fill the ctx.Params(), this is the work of your custom router.
app.Get("/hello/{name}", func(ctx context.Context) {
app.Get("/hello/{name}", func(ctx iris.Context) {
name := ctx.Params().Get("name")
ctx.Writef("Hello %s\n", name)
})
app.Get("/cs/{num:uint64 min(10) else 400}", func(ctx context.Context) {
app.Get("/cs/{num:uint64 min(10) else 400}", func(ctx iris.Context) {
num := ctx.Params().GetUint64Default("num", 0)
ctx.Writef("num is: %d\n", num)
})

View File

@ -42,7 +42,7 @@ func main() {
// The new value and its type(from string to your new custom type) it is stored only once now,
// you don't have to do any conversions for simple cases like this.
context.ParamResolvers[reflect.TypeOf([]string{})] = func(paramIndex int) interface{} {
return func(ctx context.Context) []string {
return func(ctx iris.Context) []string {
// When you want to retrieve a parameter with a value type that it is not supported by-default, such as ctx.Params().GetInt
// then you can use the `GetEntry` or `GetEntryAt` and cast its underline `ValueRaw` to the desired type.
// The type should be the same as the macro's evaluator function (last argument on the Macros#Register) return value.
@ -65,7 +65,7 @@ func main() {
http://localhost:8080/test_slice_contains/value1/value2 ->
myparam's value (a trailing path parameter type) is: []string{"value1", "value2"}
*/
app.Get("/test_slice_contains/{myparam:slice contains([value1,value2])}", func(ctx context.Context) {
app.Get("/test_slice_contains/{myparam:slice contains([value1,value2])}", func(ctx iris.Context) {
// When it is not a builtin function available to retrieve your value with the type you want, such as ctx.Params().GetInt
// then you can use the `GetEntry.ValueRaw` to get the real value, which is set-ed by your macro above.
myparam := ctx.Params().GetEntry("myparam").ValueRaw.([]string)