2017-04-21 12:26:41 +00:00
|
|
|
package param
|
|
|
|
|
2017-04-25 13:00:49 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-04-21 12:26:41 +00:00
|
|
|
//Keeps param information to be auto passed to controller methods
|
|
|
|
type MethodParam struct {
|
|
|
|
name string
|
|
|
|
location paramLocation
|
|
|
|
required bool
|
2017-04-22 22:33:50 +00:00
|
|
|
defValue string
|
2017-04-21 12:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type paramLocation byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
param paramLocation = iota
|
2017-04-25 13:00:49 +00:00
|
|
|
path
|
2017-04-21 12:26:41 +00:00
|
|
|
body
|
|
|
|
header
|
|
|
|
)
|
|
|
|
|
2017-04-25 13:00:49 +00:00
|
|
|
func New(name string, opts ...MethodParamOption) *MethodParam {
|
|
|
|
return newParam(name, nil, opts)
|
2017-04-23 18:37:09 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 12:26:41 +00:00
|
|
|
func newParam(name string, parser paramParser, opts []MethodParamOption) (param *MethodParam) {
|
2017-04-25 13:00:49 +00:00
|
|
|
param = &MethodParam{name: name}
|
2017-04-21 12:26:41 +00:00
|
|
|
for _, option := range opts {
|
|
|
|
option(param)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-04-25 13:00:49 +00:00
|
|
|
|
|
|
|
func Make(list ...*MethodParam) []*MethodParam {
|
|
|
|
if len(list) > 0 {
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mp *MethodParam) String() string {
|
|
|
|
options := []string{}
|
|
|
|
result := "param.New(\"" + mp.name + "\""
|
|
|
|
if mp.required {
|
|
|
|
options = append(options, "param.IsRequired")
|
|
|
|
}
|
|
|
|
switch mp.location {
|
|
|
|
case path:
|
|
|
|
options = append(options, "param.InPath")
|
|
|
|
case body:
|
|
|
|
options = append(options, "param.InBody")
|
|
|
|
case header:
|
|
|
|
options = append(options, "param.InHeader")
|
|
|
|
}
|
|
|
|
if mp.defValue != "" {
|
|
|
|
options = append(options, fmt.Sprintf(`param.Default("%s")`, mp.defValue))
|
|
|
|
}
|
|
|
|
if len(options) > 0 {
|
|
|
|
result += ", "
|
|
|
|
}
|
|
|
|
result += strings.Join(options, ", ")
|
|
|
|
result += ")"
|
|
|
|
return result
|
|
|
|
}
|