-
Notifications
You must be signed in to change notification settings - Fork 3
/
context.go
99 lines (86 loc) · 2.09 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package goui
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
)
type Context struct {
Data string `json:"data"`
SuccessCallback string `json:"success"`
ErrorCallback string `json:"error"`
params map[string]string
}
// GetParam get a string parameter from the url
func (ctx *Context) GetParam(name string) (val string) {
val, _ = url.PathUnescape(ctx.params[name])
return
}
// GetBoolParam get a bool parameter from the url
func (ctx *Context) GetBoolParam(name string) (b bool, err error) {
str := ctx.GetParam(name)
b, err = strconv.ParseBool(str)
if err != nil {
Log("convert data to bool failed:", err)
}
return
}
// GetIntParam get a int parameter from the url
func (ctx *Context) GetIntParam(name string) (i int, err error) {
str := ctx.GetParam(name)
i, err = strconv.Atoi(str)
if err != nil {
Log("convert data to int failed:", err)
}
return
}
func (ctx *Context) GetIntParamOr(name string, defaultVal int) (i int) {
str := ctx.GetParam(name)
var err error
i, err = strconv.Atoi(str)
if err != nil {
i = defaultVal
}
return
}
// GetFloatParam get a float parameter from the url
func (ctx *Context) GetFloatParam(name string) (f float64, err error) {
str := ctx.GetParam(name)
f, err = strconv.ParseFloat(str, 32)
if err != nil {
Log("convert data to float failed:", err)
}
return
}
// GetParam get an entity from the requested data
func (ctx *Context) GetEntity(v interface{}) (err error) {
err = json.Unmarshal([]byte(ctx.Data), v)
if err != nil {
Log("get entity failed:", err)
}
return
}
func (ctx *Context) Success(feedback interface{}) {
if ctx.SuccessCallback != "" {
if feedback == nil {
invokeJS(ctx.SuccessCallback+"()", true)
} else {
invokeJS(fmt.Sprintf("%s(\"%v\")", ctx.SuccessCallback, feedback), true)
}
}
}
func (ctx *Context) Error(err interface{}) {
if ctx.ErrorCallback != "" {
if err == nil {
invokeJS(ctx.ErrorCallback+"()", true)
} else {
invokeJS(fmt.Sprintf("%s('%v')", ctx.ErrorCallback, err), true)
}
}
}
func (ctx *Context) Ok() {
ctx.Success(nil)
}
func (ctx *Context) Fail() {
ctx.Error(nil)
}