Skip to content

Commit

Permalink
remove any reference to the pre go1.9 context.Context and replace wit…
Browse files Browse the repository at this point in the history
…h iris.Context on some places that were forgotten

got an email feedback about this
  • Loading branch information
kataras committed Aug 11, 2019
1 parent ad3ee6c commit e9e02db
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion _examples/file-server/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 { ... }
Expand Down
2 changes: 1 addition & 1 deletion _examples/hero/smart-contract/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...),
Expand Down
9 changes: 4 additions & 5 deletions _examples/http_request/extract-referer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand Down
8 changes: 2 additions & 6 deletions _examples/http_responsewriter/sse/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion _examples/http_responsewriter/transactions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion _examples/mvc/login/web/viewmodels/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Example:
import (
"github.com/kataras/iris/_examples/mvc/login/datamodels"

"github.com/kataras/iris"
"github.com/kataras/iris/context"
)

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion _examples/mvc/overview/web/viewmodels/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Example:
import (
"github.com/kataras/iris/_examples/mvc/overview/datamodels"

"github.com/kataras/iris"
"github.com/kataras/iris/context"
)

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions _examples/routing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
```
Expand Down Expand Up @@ -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()

// +------------------------------------------------------------+
Expand Down
12 changes: 6 additions & 6 deletions _examples/routing/custom-context/method-overriding/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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),
Expand All @@ -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)
Expand All @@ -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()
}
Expand Down
13 changes: 6 additions & 7 deletions _examples/routing/custom-high-level-router/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

Expand Down Expand Up @@ -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
}
Expand All @@ -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)
})
Expand Down
4 changes: 2 additions & 2 deletions _examples/routing/macros/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down

0 comments on commit e9e02db

Please sign in to comment.