1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-05 08:00:54 +00:00
Beego/context/param/methodparams.go

70 lines
1.4 KiB
Go
Raw Normal View History

2017-04-21 12:26:41 +00:00
package param
import (
"fmt"
"strings"
)
2017-04-30 16:28:26 +00:00
//MethodParam keeps param information to be auto passed to controller methods
2017-04-21 12:26:41 +00:00
type MethodParam struct {
2017-05-11 14:58:25 +00:00
name string
2017-05-12 06:28:46 +00:00
in paramType
2017-05-11 14:58:25 +00:00
required bool
defaultValue string
2017-04-21 12:26:41 +00:00
}
2017-05-12 06:28:46 +00:00
type paramType byte
2017-04-21 12:26:41 +00:00
const (
2017-05-12 06:28:46 +00:00
param paramType = iota
path
2017-04-21 12:26:41 +00:00
body
header
)
2017-04-30 16:28:26 +00:00
//New creates a new MethodParam with name and specific options
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) {
param = &MethodParam{name: name}
2017-04-21 12:26:41 +00:00
for _, option := range opts {
option(param)
}
return
}
2017-04-30 16:28:26 +00:00
//Make creates an array of MethodParmas or an empty array
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")
}
2017-05-12 06:28:46 +00:00
switch mp.in {
case path:
options = append(options, "param.InPath")
case body:
options = append(options, "param.InBody")
case header:
options = append(options, "param.InHeader")
}
2017-05-11 14:58:25 +00:00
if mp.defaultValue != "" {
options = append(options, fmt.Sprintf(`param.Default("%s")`, mp.defaultValue))
}
if len(options) > 0 {
result += ", "
}
result += strings.Join(options, ", ")
result += ")"
return result
}