github.com/astaxie/beego@v1.12.3/context/param/methodparams.go (about)

     1  package param
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  //MethodParam keeps param information to be auto passed to controller methods
     9  type MethodParam struct {
    10  	name         string
    11  	in           paramType
    12  	required     bool
    13  	defaultValue string
    14  }
    15  
    16  type paramType byte
    17  
    18  const (
    19  	param paramType = iota
    20  	path
    21  	body
    22  	header
    23  )
    24  
    25  //New creates a new MethodParam with name and specific options
    26  func New(name string, opts ...MethodParamOption) *MethodParam {
    27  	return newParam(name, nil, opts)
    28  }
    29  
    30  func newParam(name string, parser paramParser, opts []MethodParamOption) (param *MethodParam) {
    31  	param = &MethodParam{name: name}
    32  	for _, option := range opts {
    33  		option(param)
    34  	}
    35  	return
    36  }
    37  
    38  //Make creates an array of MethodParmas or an empty array
    39  func Make(list ...*MethodParam) []*MethodParam {
    40  	if len(list) > 0 {
    41  		return list
    42  	}
    43  	return nil
    44  }
    45  
    46  func (mp *MethodParam) String() string {
    47  	options := []string{}
    48  	result := "param.New(\"" + mp.name + "\""
    49  	if mp.required {
    50  		options = append(options, "param.IsRequired")
    51  	}
    52  	switch mp.in {
    53  	case path:
    54  		options = append(options, "param.InPath")
    55  	case body:
    56  		options = append(options, "param.InBody")
    57  	case header:
    58  		options = append(options, "param.InHeader")
    59  	}
    60  	if mp.defaultValue != "" {
    61  		options = append(options, fmt.Sprintf(`param.Default("%s")`, mp.defaultValue))
    62  	}
    63  	if len(options) > 0 {
    64  		result += ", "
    65  	}
    66  	result += strings.Join(options, ", ")
    67  	result += ")"
    68  	return result
    69  }