forked from ungerik/go-start
/
context.go
60 lines (49 loc) · 1.4 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package view
import (
"fmt"
"github.com/ungerik/web.go"
)
func newContext(webContext *web.Context, respondingView View, urlArgs []string) *Context {
ctx := &Context{
URLArgs: urlArgs,
Request: newRequest(webContext),
Response: newResponse(webContext),
RespondingView: respondingView,
}
ctx.Session = newSession(ctx)
return ctx
}
type Context struct {
Request *Request
Response *Response
Session *Session
// View that responds to the HTTP request
RespondingView View
// Arguments parsed from the URL path
URLArgs []string
// Custom response wide data that can be set by the application
Data interface{}
DebugData interface{}
}
/*
ForURLArgs returns an altered Context copy where
Context.URLArgs is set to urlArgs.
Can be used for calling the the URL() method of a URL interface
to get the URL of another view, defined by urlArgs.
The following example gets the URL of MyPage with the first
URL argument is that of the current page and the second
URL argument is "second-arg":
MyPage.URL(ctx.ForURLArgs(ctx.URLArgs[0], "second-arg"))
*/
func (self *Context) ForURLArgs(urlArgs ...string) *Context {
clone := *self
clone.URLArgs = urlArgs
return &clone
}
func (self *Context) ForURLArgsConvert(urlArgs ...interface{}) *Context {
stringArgs := make([]string, len(urlArgs))
for i := range urlArgs {
stringArgs[i] = fmt.Sprint(urlArgs[i])
}
return self.ForURLArgs(stringArgs...)
}