http urls monitor.

form_mapping.go 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package binding
  5. import (
  6. "errors"
  7. "reflect"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. func mapForm(ptr interface{}, form map[string][]string) error {
  13. typ := reflect.TypeOf(ptr).Elem()
  14. val := reflect.ValueOf(ptr).Elem()
  15. for i := 0; i < typ.NumField(); i++ {
  16. typeField := typ.Field(i)
  17. structField := val.Field(i)
  18. if !structField.CanSet() {
  19. continue
  20. }
  21. structFieldKind := structField.Kind()
  22. inputFieldName := typeField.Tag.Get("form")
  23. inputFieldNameList := strings.Split(inputFieldName, ",")
  24. inputFieldName = inputFieldNameList[0]
  25. var defaultValue string
  26. if len(inputFieldNameList) > 1 {
  27. defaultList := strings.SplitN(inputFieldNameList[1], "=", 2)
  28. if defaultList[0] == "default" {
  29. defaultValue = defaultList[1]
  30. }
  31. }
  32. if inputFieldName == "" {
  33. inputFieldName = typeField.Name
  34. // if "form" tag is nil, we inspect if the field is a struct or struct pointer.
  35. // this would not make sense for JSON parsing but it does for a form
  36. // since data is flatten
  37. if structFieldKind == reflect.Ptr {
  38. if !structField.Elem().IsValid() {
  39. structField.Set(reflect.New(structField.Type().Elem()))
  40. }
  41. structField = structField.Elem()
  42. structFieldKind = structField.Kind()
  43. }
  44. if structFieldKind == reflect.Struct {
  45. err := mapForm(structField.Addr().Interface(), form)
  46. if err != nil {
  47. return err
  48. }
  49. continue
  50. }
  51. }
  52. inputValue, exists := form[inputFieldName]
  53. if !exists {
  54. if defaultValue == "" {
  55. continue
  56. }
  57. inputValue = make([]string, 1)
  58. inputValue[0] = defaultValue
  59. }
  60. numElems := len(inputValue)
  61. if structFieldKind == reflect.Slice && numElems > 0 {
  62. sliceOf := structField.Type().Elem().Kind()
  63. slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
  64. for i := 0; i < numElems; i++ {
  65. if err := setWithProperType(sliceOf, inputValue[i], slice.Index(i)); err != nil {
  66. return err
  67. }
  68. }
  69. val.Field(i).Set(slice)
  70. } else {
  71. if _, isTime := structField.Interface().(time.Time); isTime {
  72. if err := setTimeField(inputValue[0], typeField, structField); err != nil {
  73. return err
  74. }
  75. continue
  76. }
  77. if err := setWithProperType(typeField.Type.Kind(), inputValue[0], structField); err != nil {
  78. return err
  79. }
  80. }
  81. }
  82. return nil
  83. }
  84. func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value) error {
  85. switch valueKind {
  86. case reflect.Int:
  87. return setIntField(val, 0, structField)
  88. case reflect.Int8:
  89. return setIntField(val, 8, structField)
  90. case reflect.Int16:
  91. return setIntField(val, 16, structField)
  92. case reflect.Int32:
  93. return setIntField(val, 32, structField)
  94. case reflect.Int64:
  95. return setIntField(val, 64, structField)
  96. case reflect.Uint:
  97. return setUintField(val, 0, structField)
  98. case reflect.Uint8:
  99. return setUintField(val, 8, structField)
  100. case reflect.Uint16:
  101. return setUintField(val, 16, structField)
  102. case reflect.Uint32:
  103. return setUintField(val, 32, structField)
  104. case reflect.Uint64:
  105. return setUintField(val, 64, structField)
  106. case reflect.Bool:
  107. return setBoolField(val, structField)
  108. case reflect.Float32:
  109. return setFloatField(val, 32, structField)
  110. case reflect.Float64:
  111. return setFloatField(val, 64, structField)
  112. case reflect.String:
  113. structField.SetString(val)
  114. case reflect.Ptr:
  115. if !structField.Elem().IsValid() {
  116. structField.Set(reflect.New(structField.Type().Elem()))
  117. }
  118. structFieldElem := structField.Elem()
  119. return setWithProperType(structFieldElem.Kind(), val, structFieldElem)
  120. default:
  121. return errors.New("Unknown type")
  122. }
  123. return nil
  124. }
  125. func setIntField(val string, bitSize int, field reflect.Value) error {
  126. if val == "" {
  127. val = "0"
  128. }
  129. intVal, err := strconv.ParseInt(val, 10, bitSize)
  130. if err == nil {
  131. field.SetInt(intVal)
  132. }
  133. return err
  134. }
  135. func setUintField(val string, bitSize int, field reflect.Value) error {
  136. if val == "" {
  137. val = "0"
  138. }
  139. uintVal, err := strconv.ParseUint(val, 10, bitSize)
  140. if err == nil {
  141. field.SetUint(uintVal)
  142. }
  143. return err
  144. }
  145. func setBoolField(val string, field reflect.Value) error {
  146. if val == "" {
  147. val = "false"
  148. }
  149. boolVal, err := strconv.ParseBool(val)
  150. if err == nil {
  151. field.SetBool(boolVal)
  152. }
  153. return err
  154. }
  155. func setFloatField(val string, bitSize int, field reflect.Value) error {
  156. if val == "" {
  157. val = "0.0"
  158. }
  159. floatVal, err := strconv.ParseFloat(val, bitSize)
  160. if err == nil {
  161. field.SetFloat(floatVal)
  162. }
  163. return err
  164. }
  165. func setTimeField(val string, structField reflect.StructField, value reflect.Value) error {
  166. timeFormat := structField.Tag.Get("time_format")
  167. if timeFormat == "" {
  168. return errors.New("Blank time format")
  169. }
  170. if val == "" {
  171. value.Set(reflect.ValueOf(time.Time{}))
  172. return nil
  173. }
  174. l := time.Local
  175. if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC {
  176. l = time.UTC
  177. }
  178. if locTag := structField.Tag.Get("time_location"); locTag != "" {
  179. loc, err := time.LoadLocation(locTag)
  180. if err != nil {
  181. return err
  182. }
  183. l = loc
  184. }
  185. t, err := time.ParseInLocation(timeFormat, val, l)
  186. if err != nil {
  187. return err
  188. }
  189. value.Set(reflect.ValueOf(t))
  190. return nil
  191. }