form_mapping.go 5.3 KB

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