form_mapping.go 5.4 KB

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