gin/input_holder.go

70 lines
1.6 KiB
Go
Raw Normal View History

2015-03-31 16:35:36 +00:00
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package gin
2015-04-08 12:24:49 +00:00
// Param is a single URL parameter, consisting of a key and a value.
type Param struct {
Key string
Value string
}
// Params is a Param-slice, as returned by the router.
// The slice is ordered, the first URL parameter is also the first slice value.
// It is therefore safe to read values by the index.
type Params []Param
// ByName returns the value of the first Param which key matches the given name.
// If no matching Param is found, an empty string is returned.
func (ps Params) ByName(name string) string {
for _, entry := range ps {
if entry.Key == name {
return entry.Value
}
}
return ""
}
2015-03-31 16:35:36 +00:00
type inputHolder struct {
context *Context
}
func (i inputHolder) FromGET(key string) (va string) {
va, _ = i.fromGET(key)
return
}
func (i inputHolder) FromPOST(key string) (va string) {
va, _ = i.fromPOST(key)
return
}
func (i inputHolder) Get(key string) string {
2015-04-07 21:28:49 +00:00
if value, exists := i.fromPOST(key); exists {
2015-03-31 16:35:36 +00:00
return value
}
2015-04-07 21:28:49 +00:00
if value, exists := i.fromGET(key); exists {
2015-03-31 16:35:36 +00:00
return value
}
return ""
}
func (i inputHolder) fromGET(key string) (string, bool) {
req := i.context.Request
req.ParseForm()
2015-04-07 21:28:49 +00:00
if values, ok := req.Form[key]; ok && len(values) > 0 {
2015-03-31 16:35:36 +00:00
return values[0], true
}
2015-04-07 21:28:49 +00:00
return "", false
2015-03-31 16:35:36 +00:00
}
func (i inputHolder) fromPOST(key string) (string, bool) {
req := i.context.Request
req.ParseForm()
2015-04-07 21:28:49 +00:00
if values, ok := req.PostForm[key]; ok && len(values) > 0 {
2015-03-31 16:35:36 +00:00
return values[0], true
}
2015-04-07 21:28:49 +00:00
return "", false
2015-03-31 16:35:36 +00:00
}