|
@@ -1,668 +0,0 @@
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-package context
|
|
|
-
|
|
|
-import (
|
|
|
- "bytes"
|
|
|
- "compress/gzip"
|
|
|
- "errors"
|
|
|
- "io"
|
|
|
- "io/ioutil"
|
|
|
- "net"
|
|
|
- "net/http"
|
|
|
- "net/url"
|
|
|
- "reflect"
|
|
|
- "regexp"
|
|
|
- "strconv"
|
|
|
- "strings"
|
|
|
-
|
|
|
- "github.com/cnlh/nps/vender/github.com/astaxie/beego/session"
|
|
|
-)
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-var (
|
|
|
- acceptsHTMLRegex = regexp.MustCompile(`(text/html|application/xhtml\+xml)(?:,|$)`)
|
|
|
- acceptsXMLRegex = regexp.MustCompile(`(application/xml|text/xml)(?:,|$)`)
|
|
|
- acceptsJSONRegex = regexp.MustCompile(`(application/json)(?:,|$)`)
|
|
|
- acceptsYAMLRegex = regexp.MustCompile(`(application/x-yaml)(?:,|$)`)
|
|
|
- maxParam = 50
|
|
|
-)
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-type BeegoInput struct {
|
|
|
- Context *Context
|
|
|
- CruSession session.Store
|
|
|
- pnames []string
|
|
|
- pvalues []string
|
|
|
- data map[interface{}]interface{}
|
|
|
- RequestBody []byte
|
|
|
- RunMethod string
|
|
|
- RunController reflect.Type
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func NewInput() *BeegoInput {
|
|
|
- return &BeegoInput{
|
|
|
- pnames: make([]string, 0, maxParam),
|
|
|
- pvalues: make([]string, 0, maxParam),
|
|
|
- data: make(map[interface{}]interface{}),
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Reset(ctx *Context) {
|
|
|
- input.Context = ctx
|
|
|
- input.CruSession = nil
|
|
|
- input.pnames = input.pnames[:0]
|
|
|
- input.pvalues = input.pvalues[:0]
|
|
|
- input.data = nil
|
|
|
- input.RequestBody = []byte{}
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Protocol() string {
|
|
|
- return input.Context.Request.Proto
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) URI() string {
|
|
|
- return input.Context.Request.RequestURI
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) URL() string {
|
|
|
- return input.Context.Request.URL.Path
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Site() string {
|
|
|
- return input.Scheme() + "://" + input.Domain()
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Scheme() string {
|
|
|
- if scheme := input.Header("X-Forwarded-Proto"); scheme != "" {
|
|
|
- return scheme
|
|
|
- }
|
|
|
- if input.Context.Request.URL.Scheme != "" {
|
|
|
- return input.Context.Request.URL.Scheme
|
|
|
- }
|
|
|
- if input.Context.Request.TLS == nil {
|
|
|
- return "http"
|
|
|
- }
|
|
|
- return "https"
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Domain() string {
|
|
|
- return input.Host()
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Host() string {
|
|
|
- if input.Context.Request.Host != "" {
|
|
|
- if hostPart, _, err := net.SplitHostPort(input.Context.Request.Host); err == nil {
|
|
|
- return hostPart
|
|
|
- }
|
|
|
- return input.Context.Request.Host
|
|
|
- }
|
|
|
- return "localhost"
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Method() string {
|
|
|
- return input.Context.Request.Method
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Is(method string) bool {
|
|
|
- return input.Method() == method
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) IsGet() bool {
|
|
|
- return input.Is("GET")
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) IsPost() bool {
|
|
|
- return input.Is("POST")
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) IsHead() bool {
|
|
|
- return input.Is("HEAD")
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) IsOptions() bool {
|
|
|
- return input.Is("OPTIONS")
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) IsPut() bool {
|
|
|
- return input.Is("PUT")
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) IsDelete() bool {
|
|
|
- return input.Is("DELETE")
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) IsPatch() bool {
|
|
|
- return input.Is("PATCH")
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) IsAjax() bool {
|
|
|
- return input.Header("X-Requested-With") == "XMLHttpRequest"
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) IsSecure() bool {
|
|
|
- return input.Scheme() == "https"
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) IsWebsocket() bool {
|
|
|
- return input.Header("Upgrade") == "websocket"
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) IsUpload() bool {
|
|
|
- return strings.Contains(input.Header("Content-Type"), "multipart/form-data")
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) AcceptsHTML() bool {
|
|
|
- return acceptsHTMLRegex.MatchString(input.Header("Accept"))
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) AcceptsXML() bool {
|
|
|
- return acceptsXMLRegex.MatchString(input.Header("Accept"))
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) AcceptsJSON() bool {
|
|
|
- return acceptsJSONRegex.MatchString(input.Header("Accept"))
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) AcceptsYAML() bool {
|
|
|
- return acceptsYAMLRegex.MatchString(input.Header("Accept"))
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) IP() string {
|
|
|
- ips := input.Proxy()
|
|
|
- if len(ips) > 0 && ips[0] != "" {
|
|
|
- rip, _, err := net.SplitHostPort(ips[0])
|
|
|
- if err != nil {
|
|
|
- rip = ips[0]
|
|
|
- }
|
|
|
- return rip
|
|
|
- }
|
|
|
- if ip, _, err := net.SplitHostPort(input.Context.Request.RemoteAddr); err == nil {
|
|
|
- return ip
|
|
|
- }
|
|
|
- return input.Context.Request.RemoteAddr
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Proxy() []string {
|
|
|
- if ips := input.Header("X-Forwarded-For"); ips != "" {
|
|
|
- return strings.Split(ips, ",")
|
|
|
- }
|
|
|
- return []string{}
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Referer() string {
|
|
|
- return input.Header("Referer")
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Refer() string {
|
|
|
- return input.Referer()
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) SubDomains() string {
|
|
|
- parts := strings.Split(input.Host(), ".")
|
|
|
- if len(parts) >= 3 {
|
|
|
- return strings.Join(parts[:len(parts)-2], ".")
|
|
|
- }
|
|
|
- return ""
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Port() int {
|
|
|
- if _, portPart, err := net.SplitHostPort(input.Context.Request.Host); err == nil {
|
|
|
- port, _ := strconv.Atoi(portPart)
|
|
|
- return port
|
|
|
- }
|
|
|
- return 80
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) UserAgent() string {
|
|
|
- return input.Header("User-Agent")
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) ParamsLen() int {
|
|
|
- return len(input.pnames)
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Param(key string) string {
|
|
|
- for i, v := range input.pnames {
|
|
|
- if v == key && i <= len(input.pvalues) {
|
|
|
- return input.pvalues[i]
|
|
|
- }
|
|
|
- }
|
|
|
- return ""
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Params() map[string]string {
|
|
|
- m := make(map[string]string)
|
|
|
- for i, v := range input.pnames {
|
|
|
- if i <= len(input.pvalues) {
|
|
|
- m[v] = input.pvalues[i]
|
|
|
- }
|
|
|
- }
|
|
|
- return m
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) SetParam(key, val string) {
|
|
|
-
|
|
|
- for i, v := range input.pnames {
|
|
|
- if v == key && i <= len(input.pvalues) {
|
|
|
- input.pvalues[i] = val
|
|
|
- return
|
|
|
- }
|
|
|
- }
|
|
|
- input.pvalues = append(input.pvalues, val)
|
|
|
- input.pnames = append(input.pnames, key)
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) ResetParams() {
|
|
|
- input.pnames = input.pnames[:0]
|
|
|
- input.pvalues = input.pvalues[:0]
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Query(key string) string {
|
|
|
- if val := input.Param(key); val != "" {
|
|
|
- return val
|
|
|
- }
|
|
|
- if input.Context.Request.Form == nil {
|
|
|
- input.Context.Request.ParseForm()
|
|
|
- }
|
|
|
- return input.Context.Request.Form.Get(key)
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Header(key string) string {
|
|
|
- return input.Context.Request.Header.Get(key)
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Cookie(key string) string {
|
|
|
- ck, err := input.Context.Request.Cookie(key)
|
|
|
- if err != nil {
|
|
|
- return ""
|
|
|
- }
|
|
|
- return ck.Value
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Session(key interface{}) interface{} {
|
|
|
- return input.CruSession.Get(key)
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) CopyBody(MaxMemory int64) []byte {
|
|
|
- if input.Context.Request.Body == nil {
|
|
|
- return []byte{}
|
|
|
- }
|
|
|
-
|
|
|
- var requestbody []byte
|
|
|
- safe := &io.LimitedReader{R: input.Context.Request.Body, N: MaxMemory}
|
|
|
- if input.Header("Content-Encoding") == "gzip" {
|
|
|
- reader, err := gzip.NewReader(safe)
|
|
|
- if err != nil {
|
|
|
- return nil
|
|
|
- }
|
|
|
- requestbody, _ = ioutil.ReadAll(reader)
|
|
|
- } else {
|
|
|
- requestbody, _ = ioutil.ReadAll(safe)
|
|
|
- }
|
|
|
-
|
|
|
- input.Context.Request.Body.Close()
|
|
|
- bf := bytes.NewBuffer(requestbody)
|
|
|
- input.Context.Request.Body = http.MaxBytesReader(input.Context.ResponseWriter, ioutil.NopCloser(bf), MaxMemory)
|
|
|
- input.RequestBody = requestbody
|
|
|
- return requestbody
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Data() map[interface{}]interface{} {
|
|
|
- if input.data == nil {
|
|
|
- input.data = make(map[interface{}]interface{})
|
|
|
- }
|
|
|
- return input.data
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) GetData(key interface{}) interface{} {
|
|
|
- if v, ok := input.data[key]; ok {
|
|
|
- return v
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) SetData(key, val interface{}) {
|
|
|
- if input.data == nil {
|
|
|
- input.data = make(map[interface{}]interface{})
|
|
|
- }
|
|
|
- input.data[key] = val
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) ParseFormOrMulitForm(maxMemory int64) error {
|
|
|
-
|
|
|
- if strings.Contains(input.Header("Content-Type"), "multipart/form-data") {
|
|
|
- if err := input.Context.Request.ParseMultipartForm(maxMemory); err != nil {
|
|
|
- return errors.New("Error parsing request body:" + err.Error())
|
|
|
- }
|
|
|
- } else if err := input.Context.Request.ParseForm(); err != nil {
|
|
|
- return errors.New("Error parsing request body:" + err.Error())
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (input *BeegoInput) Bind(dest interface{}, key string) error {
|
|
|
- value := reflect.ValueOf(dest)
|
|
|
- if value.Kind() != reflect.Ptr {
|
|
|
- return errors.New("beego: non-pointer passed to Bind: " + key)
|
|
|
- }
|
|
|
- value = value.Elem()
|
|
|
- if !value.CanSet() {
|
|
|
- return errors.New("beego: non-settable variable passed to Bind: " + key)
|
|
|
- }
|
|
|
- typ := value.Type()
|
|
|
-
|
|
|
-
|
|
|
- if value.Kind() == reflect.Interface {
|
|
|
- typ = value.Elem().Type()
|
|
|
- }
|
|
|
- rv := input.bind(key, typ)
|
|
|
- if !rv.IsValid() {
|
|
|
- return errors.New("beego: reflect value is empty")
|
|
|
- }
|
|
|
- value.Set(rv)
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (input *BeegoInput) bind(key string, typ reflect.Type) reflect.Value {
|
|
|
- if input.Context.Request.Form == nil {
|
|
|
- input.Context.Request.ParseForm()
|
|
|
- }
|
|
|
- rv := reflect.Zero(typ)
|
|
|
- switch typ.Kind() {
|
|
|
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
- val := input.Query(key)
|
|
|
- if len(val) == 0 {
|
|
|
- return rv
|
|
|
- }
|
|
|
- rv = input.bindInt(val, typ)
|
|
|
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
- val := input.Query(key)
|
|
|
- if len(val) == 0 {
|
|
|
- return rv
|
|
|
- }
|
|
|
- rv = input.bindUint(val, typ)
|
|
|
- case reflect.Float32, reflect.Float64:
|
|
|
- val := input.Query(key)
|
|
|
- if len(val) == 0 {
|
|
|
- return rv
|
|
|
- }
|
|
|
- rv = input.bindFloat(val, typ)
|
|
|
- case reflect.String:
|
|
|
- val := input.Query(key)
|
|
|
- if len(val) == 0 {
|
|
|
- return rv
|
|
|
- }
|
|
|
- rv = input.bindString(val, typ)
|
|
|
- case reflect.Bool:
|
|
|
- val := input.Query(key)
|
|
|
- if len(val) == 0 {
|
|
|
- return rv
|
|
|
- }
|
|
|
- rv = input.bindBool(val, typ)
|
|
|
- case reflect.Slice:
|
|
|
- rv = input.bindSlice(&input.Context.Request.Form, key, typ)
|
|
|
- case reflect.Struct:
|
|
|
- rv = input.bindStruct(&input.Context.Request.Form, key, typ)
|
|
|
- case reflect.Ptr:
|
|
|
- rv = input.bindPoint(key, typ)
|
|
|
- case reflect.Map:
|
|
|
- rv = input.bindMap(&input.Context.Request.Form, key, typ)
|
|
|
- }
|
|
|
- return rv
|
|
|
-}
|
|
|
-
|
|
|
-func (input *BeegoInput) bindValue(val string, typ reflect.Type) reflect.Value {
|
|
|
- rv := reflect.Zero(typ)
|
|
|
- switch typ.Kind() {
|
|
|
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
- rv = input.bindInt(val, typ)
|
|
|
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
- rv = input.bindUint(val, typ)
|
|
|
- case reflect.Float32, reflect.Float64:
|
|
|
- rv = input.bindFloat(val, typ)
|
|
|
- case reflect.String:
|
|
|
- rv = input.bindString(val, typ)
|
|
|
- case reflect.Bool:
|
|
|
- rv = input.bindBool(val, typ)
|
|
|
- case reflect.Slice:
|
|
|
- rv = input.bindSlice(&url.Values{"": {val}}, "", typ)
|
|
|
- case reflect.Struct:
|
|
|
- rv = input.bindStruct(&url.Values{"": {val}}, "", typ)
|
|
|
- case reflect.Ptr:
|
|
|
- rv = input.bindPoint(val, typ)
|
|
|
- case reflect.Map:
|
|
|
- rv = input.bindMap(&url.Values{"": {val}}, "", typ)
|
|
|
- }
|
|
|
- return rv
|
|
|
-}
|
|
|
-
|
|
|
-func (input *BeegoInput) bindInt(val string, typ reflect.Type) reflect.Value {
|
|
|
- intValue, err := strconv.ParseInt(val, 10, 64)
|
|
|
- if err != nil {
|
|
|
- return reflect.Zero(typ)
|
|
|
- }
|
|
|
- pValue := reflect.New(typ)
|
|
|
- pValue.Elem().SetInt(intValue)
|
|
|
- return pValue.Elem()
|
|
|
-}
|
|
|
-
|
|
|
-func (input *BeegoInput) bindUint(val string, typ reflect.Type) reflect.Value {
|
|
|
- uintValue, err := strconv.ParseUint(val, 10, 64)
|
|
|
- if err != nil {
|
|
|
- return reflect.Zero(typ)
|
|
|
- }
|
|
|
- pValue := reflect.New(typ)
|
|
|
- pValue.Elem().SetUint(uintValue)
|
|
|
- return pValue.Elem()
|
|
|
-}
|
|
|
-
|
|
|
-func (input *BeegoInput) bindFloat(val string, typ reflect.Type) reflect.Value {
|
|
|
- floatValue, err := strconv.ParseFloat(val, 64)
|
|
|
- if err != nil {
|
|
|
- return reflect.Zero(typ)
|
|
|
- }
|
|
|
- pValue := reflect.New(typ)
|
|
|
- pValue.Elem().SetFloat(floatValue)
|
|
|
- return pValue.Elem()
|
|
|
-}
|
|
|
-
|
|
|
-func (input *BeegoInput) bindString(val string, typ reflect.Type) reflect.Value {
|
|
|
- return reflect.ValueOf(val)
|
|
|
-}
|
|
|
-
|
|
|
-func (input *BeegoInput) bindBool(val string, typ reflect.Type) reflect.Value {
|
|
|
- val = strings.TrimSpace(strings.ToLower(val))
|
|
|
- switch val {
|
|
|
- case "true", "on", "1":
|
|
|
- return reflect.ValueOf(true)
|
|
|
- }
|
|
|
- return reflect.ValueOf(false)
|
|
|
-}
|
|
|
-
|
|
|
-type sliceValue struct {
|
|
|
- index int
|
|
|
- value reflect.Value
|
|
|
-}
|
|
|
-
|
|
|
-func (input *BeegoInput) bindSlice(params *url.Values, key string, typ reflect.Type) reflect.Value {
|
|
|
- maxIndex := -1
|
|
|
- numNoIndex := 0
|
|
|
- sliceValues := []sliceValue{}
|
|
|
- for reqKey, vals := range *params {
|
|
|
- if !strings.HasPrefix(reqKey, key+"[") {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- index := -1
|
|
|
- leftBracket, rightBracket := len(key), strings.Index(reqKey[len(key):], "]")+len(key)
|
|
|
- if rightBracket > leftBracket+1 {
|
|
|
- index, _ = strconv.Atoi(reqKey[leftBracket+1 : rightBracket])
|
|
|
- }
|
|
|
- subKeyIndex := rightBracket + 1
|
|
|
-
|
|
|
-
|
|
|
- if index > -1 {
|
|
|
- if index > maxIndex {
|
|
|
- maxIndex = index
|
|
|
- }
|
|
|
- sliceValues = append(sliceValues, sliceValue{
|
|
|
- index: index,
|
|
|
- value: input.bind(reqKey[:subKeyIndex], typ.Elem()),
|
|
|
- })
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- numNoIndex += len(vals)
|
|
|
- for _, val := range vals {
|
|
|
-
|
|
|
- sliceValues = append(sliceValues, sliceValue{
|
|
|
- index: -1,
|
|
|
- value: input.bindValue(val, typ.Elem()),
|
|
|
- })
|
|
|
- }
|
|
|
- }
|
|
|
- resultArray := reflect.MakeSlice(typ, maxIndex+1, maxIndex+1+numNoIndex)
|
|
|
- for _, sv := range sliceValues {
|
|
|
- if sv.index != -1 {
|
|
|
- resultArray.Index(sv.index).Set(sv.value)
|
|
|
- } else {
|
|
|
- resultArray = reflect.Append(resultArray, sv.value)
|
|
|
- }
|
|
|
- }
|
|
|
- return resultArray
|
|
|
-}
|
|
|
-
|
|
|
-func (input *BeegoInput) bindStruct(params *url.Values, key string, typ reflect.Type) reflect.Value {
|
|
|
- result := reflect.New(typ).Elem()
|
|
|
- fieldValues := make(map[string]reflect.Value)
|
|
|
- for reqKey, val := range *params {
|
|
|
- var fieldName string
|
|
|
- if strings.HasPrefix(reqKey, key+".") {
|
|
|
- fieldName = reqKey[len(key)+1:]
|
|
|
- } else if strings.HasPrefix(reqKey, key+"[") && reqKey[len(reqKey)-1] == ']' {
|
|
|
- fieldName = reqKey[len(key)+1 : len(reqKey)-1]
|
|
|
- } else {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- if _, ok := fieldValues[fieldName]; !ok {
|
|
|
-
|
|
|
- fieldValue := result.FieldByName(fieldName)
|
|
|
- if !fieldValue.IsValid() {
|
|
|
- continue
|
|
|
- }
|
|
|
- if !fieldValue.CanSet() {
|
|
|
- continue
|
|
|
- }
|
|
|
- boundVal := input.bindValue(val[0], fieldValue.Type())
|
|
|
- fieldValue.Set(boundVal)
|
|
|
- fieldValues[fieldName] = boundVal
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return result
|
|
|
-}
|
|
|
-
|
|
|
-func (input *BeegoInput) bindPoint(key string, typ reflect.Type) reflect.Value {
|
|
|
- return input.bind(key, typ.Elem()).Addr()
|
|
|
-}
|
|
|
-
|
|
|
-func (input *BeegoInput) bindMap(params *url.Values, key string, typ reflect.Type) reflect.Value {
|
|
|
- var (
|
|
|
- result = reflect.MakeMap(typ)
|
|
|
- keyType = typ.Key()
|
|
|
- valueType = typ.Elem()
|
|
|
- )
|
|
|
- for paramName, values := range *params {
|
|
|
- if !strings.HasPrefix(paramName, key+"[") || paramName[len(paramName)-1] != ']' {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- key := paramName[len(key)+1 : len(paramName)-1]
|
|
|
- result.SetMapIndex(input.bindValue(key, keyType), input.bindValue(values[0], valueType))
|
|
|
- }
|
|
|
- return result
|
|
|
-}
|