forked from qiangxue/fasthttp-routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.go
113 lines (95 loc) · 2.17 KB
/
header.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package routing
import (
"github.com/valyala/fasthttp"
"strconv"
)
type Header struct {
c *fasthttp.RequestCtx
}
func (p Header) get(key string) string {
return string(p.c.Request.Header.Peek(key))
}
func (p Header) Set(key string, value string) {
p.c.Request.Header.Set(key, value)
}
func (p Header) SetInt(key string, value int) {
vStr := strconv.Itoa(value)
p.c.Request.Header.Set(key, vStr)
}
func (p Header) SetInt32(key string, value int32) {
vStr := strconv.FormatInt(int64(value), 10)
p.c.Request.Header.Set(key, vStr)
}
func (p Header) SetInt64(key string, value int64) {
vStr := strconv.FormatInt(value, 10)
p.c.Request.Header.Set(key, vStr)
}
func (p Header) SetBool(key string, value bool) {
vStr := strconv.FormatBool(value)
p.c.Request.Header.Set(key, vStr)
}
func (p Header) Int(key string) (int, error) {
vStr := p.get(key)
return strconv.Atoi(vStr)
}
func (p Header) IntDefault(key string, def int) int {
vStr := p.get(key)
value, err := strconv.Atoi(vStr)
if err != nil{
return def
}
return value
}
func (p Header) Int32(key string) (int32, error) {
vStr := p.get(key)
value, err := strconv.ParseInt(vStr, 10, 32)
if err != nil{
return 0, err
}
return int32(value), nil
}
func (p Header) Int32Default(key string, def int32) int32 {
vStr := p.get(key)
value, err := strconv.ParseInt(vStr, 10, 32)
if err != nil{
return def
}
return int32(value)
}
func (p Header) Int64(key string) (int32, error) {
vStr := p.get(key)
value, err := strconv.ParseInt(vStr, 10, 32)
if err != nil{
return 0, err
}
return int32(value), nil
}
func (p Header) Int64Default(key string, def int64) int64 {
vStr := p.get(key)
value, err := strconv.ParseInt(vStr, 10, 64)
if err != nil{
return def
}
return value
}
func (p Header) Value(key string) string {
return p.get(key)
}
func (p Header) ValueDefault(key string, def string) string {
if p.get(key) == "" {
return def
}
return p.get(key)
}
func (p Header) Bool(key string) (bool, error) {
vStr := p.get(key)
return strconv.ParseBool(vStr)
}
func (p Header) BoolDefault(key string, def bool) bool {
vStr := p.get(key)
value, err := strconv.ParseBool(vStr)
if err != nil{
return def
}
return value
}