form_mapping.go 5.5 KB

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