-
Notifications
You must be signed in to change notification settings - Fork 18
/
posts.go
75 lines (65 loc) · 1.87 KB
/
posts.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
package W
import (
"github.com/kokizzu/gotro/L"
"github.com/kokizzu/gotro/M"
"github.com/kokizzu/gotro/S"
"github.com/valyala/fasthttp"
)
type Posts struct {
*fasthttp.Args
M.SS
}
func (p *Posts) GetBool(key string) bool {
val := p.SS[key]
return !(val == `` || val == `0` || val == `f` || val == `false`)
}
func (p *Posts) GetJsonMap(key string) M.SX {
return S.JsonToMap(p.GetStr(key))
}
func (p *Posts) IsSet(key string) bool {
_, ok := p.SS[key]
return ok
}
func (p *Posts) GetJsonStrArr(key string) []string {
return S.JsonToStrArr(p.GetStr(key))
}
func (p *Posts) GetJsonObjArr(key string) []map[string]interface{} {
return S.JsonToObjArr(p.GetStr(key))
}
func (p *Posts) GetJsonIntArr(key string) []int64 {
return S.JsonToIntArr(p.GetStr(key))
}
func (p *Posts) FromContext(ctx *Context) {
p.Args = ctx.RequestCtx.PostArgs()
p.SS = M.SS{}
p.Args.VisitAll(func(k, v []byte) {
p.SS[string(k)] = string(v)
})
mf, err := ctx.RequestCtx.MultipartForm()
if err == nil {
//L.Print(`Multipart Post Data: ` + I.ToStr(len(mf.Value)) + ` keys`)
for k, v := range mf.Value {
p.SS[k] = v[0]
//L.Print(`* ` + k + `: ` + S.IfElse(len(v[0]) < 128, v[0], `length=`+I.ToStr(len(v[0]))) + S.If(len(v) > 1, ` [warning: array form ignored: `+I.ToStr(len(v))+`]`))
}
} else {
L.Print(`Error Parsing Post Data: ` + err.Error())
}
}
// censor the password string, also when length is too long
func (p *Posts) String() string {
return p.SS.PrettyFunc(` | `, func(key, val string) string {
if len(val) > 64 {
return val[:64] + `...`
}
return S.IfElse(key == `pass` || key == `password`, S.Repeat(`*`, len(val)), val)
})
}
func (p *Posts) NewlineString() string {
return p.SS.PrettyFunc("\n\t", func(key, val string) string {
if len(val) > 4096 {
return val[:4096] + `...`
}
return S.IfElse(key == `pass` || key == `password`, S.Repeat(`*`, len(val)), val)
})
}