-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparams.go
109 lines (97 loc) · 2.28 KB
/
params.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
// Copyright 2015 Eryx <evorui at gmail dot com>, All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package httpsrv
import (
"net/http"
"net/url"
"strconv"
)
type Params struct {
inited bool
// A unified view of all the individual param maps below
values url.Values
request *http.Request
}
func ParamsFilter(c *Controller) {
if c.Params == nil {
c.Params = &Params{
request: c.Request.Request,
}
} else {
c.Params.request = c.Request.Request
}
}
func (p *Params) reset() *Params {
p.values = make(url.Values)
p.inited = false
p.request = nil
return p
}
func (p *Params) init() {
if p.inited {
return
}
p.inited = true
if p.request != nil {
p.values = p.request.URL.Query()
if p.request.Method == "POST" ||
p.request.Method == "PUT" ||
p.request.Method == "PATCH" {
p.request.ParseForm()
}
}
}
func (p *Params) SetValue(key, value string) {
p.init()
if p.values == nil {
p.values = make(url.Values)
}
if p.values.Has(key) {
p.values[key] = append(p.values[key], value)
} else {
p.values[key] = []string{value}
}
}
func (p *Params) Value(key string) string {
p.init()
if v := p.request.PathValue(key); v != "" {
return v
}
if p.values != nil && p.values.Has(key) {
return p.values.Get(key)
}
if p.request.Form != nil && p.request.Form.Has(key) {
return p.request.Form.Get(key)
}
if p.request.PostForm != nil && p.request.PostForm.Has(key) {
return p.request.PostForm.Get(key)
}
return ""
}
func (p *Params) IntValue(key string) int64 {
if s := p.Value(key); s != "" {
if i, e := strconv.ParseInt(s, 10, 64); e == nil {
return i
}
}
return 0
}
func (p *Params) FloatValue(key string) float64 {
if s := p.Value(key); s != "" {
if f, e := strconv.ParseFloat(s, 64); e == nil {
return f
}
}
return 0
}