form_mapping.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. "time"
  10. )
  11. func mapForm(ptr interface{}, form map[string][]string) error {
  12. typ := reflect.TypeOf(ptr).Elem()
  13. val := reflect.ValueOf(ptr).Elem()
  14. for i := 0; i < typ.NumField(); i++ {
  15. typeField := typ.Field(i)
  16. structField := val.Field(i)
  17. if !structField.CanSet() {
  18. continue
  19. }
  20. structFieldKind := structField.Kind()
  21. inputFieldName := typeField.Tag.Get("form")
  22. if inputFieldName == "" {
  23. inputFieldName = typeField.Name
  24. // if "form" tag is nil, we inspect if the field is a struct.
  25. // this would not make sense for JSON parsing but it does for a form
  26. // since data is flatten
  27. if structFieldKind == reflect.Struct {
  28. err := mapForm(structField.Addr().Interface(), form)
  29. if err != nil {
  30. return err
  31. }
  32. continue
  33. }
  34. }
  35. inputValue, exists := form[inputFieldName]
  36. if !exists {
  37. continue
  38. }
  39. numElems := len(inputValue)
  40. if structFieldKind == reflect.Slice && numElems > 0 {
  41. sliceOf := structField.Type().Elem().Kind()
  42. slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
  43. for i := 0; i < numElems; i++ {
  44. if err := setWithProperType(sliceOf, inputValue[i], slice.Index(i)); err != nil {
  45. return err
  46. }
  47. }
  48. val.Field(i).Set(slice)
  49. } else {
  50. if _, isTime := structField.Interface().(time.Time); isTime {
  51. if err := setTimeField(inputValue[0], typeField, structField); err != nil {
  52. return err
  53. }
  54. continue
  55. }
  56. if err := setWithProperType(typeField.Type.Kind(), inputValue[0], structField); err != nil {
  57. return err
  58. }
  59. }
  60. }
  61. return nil
  62. }
  63. func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value) error {
  64. switch valueKind {
  65. case reflect.Int:
  66. return setIntField(val, 0, structField)
  67. case reflect.Int8:
  68. return setIntField(val, 8, structField)
  69. case reflect.Int16:
  70. return setIntField(val, 16, structField)
  71. case reflect.Int32:
  72. return setIntField(val, 32, structField)
  73. case reflect.Int64:
  74. return setIntField(val, 64, structField)
  75. case reflect.Uint:
  76. return setUintField(val, 0, structField)
  77. case reflect.Uint8:
  78. return setUintField(val, 8, structField)
  79. case reflect.Uint16:
  80. return setUintField(val, 16, structField)
  81. case reflect.Uint32:
  82. return setUintField(val, 32, structField)
  83. case reflect.Uint64:
  84. return setUintField(val, 64, structField)
  85. case reflect.Bool:
  86. return setBoolField(val, structField)
  87. case reflect.Float32:
  88. return setFloatField(val, 32, structField)
  89. case reflect.Float64:
  90. return setFloatField(val, 64, structField)
  91. case reflect.String:
  92. structField.SetString(val)
  93. default:
  94. return errors.New("Unknown type")
  95. }
  96. return nil
  97. }
  98. func setIntField(val string, bitSize int, field reflect.Value) error {
  99. if val == "" {
  100. val = "0"
  101. }
  102. intVal, err := strconv.ParseInt(val, 10, bitSize)
  103. if err == nil {
  104. field.SetInt(intVal)
  105. }
  106. return err
  107. }
  108. func setUintField(val string, bitSize int, field reflect.Value) error {
  109. if val == "" {
  110. val = "0"
  111. }
  112. uintVal, err := strconv.ParseUint(val, 10, bitSize)
  113. if err == nil {
  114. field.SetUint(uintVal)
  115. }
  116. return err
  117. }
  118. func setBoolField(val string, field reflect.Value) error {
  119. if val == "" {
  120. val = "false"
  121. }
  122. boolVal, err := strconv.ParseBool(val)
  123. if err == nil {
  124. field.SetBool(boolVal)
  125. }
  126. return nil
  127. }
  128. func setFloatField(val string, bitSize int, field reflect.Value) error {
  129. if val == "" {
  130. val = "0.0"
  131. }
  132. floatVal, err := strconv.ParseFloat(val, bitSize)
  133. if err == nil {
  134. field.SetFloat(floatVal)
  135. }
  136. return err
  137. }
  138. func setTimeField(val string, structField reflect.StructField, value reflect.Value) error {
  139. timeFormat := structField.Tag.Get("time_format")
  140. if timeFormat == "" {
  141. return errors.New("Blank time format")
  142. }
  143. if val == "" {
  144. value.Set(reflect.ValueOf(time.Time{}))
  145. return nil
  146. }
  147. l := time.Local
  148. if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC {
  149. l = time.UTC
  150. }
  151. t, err := time.ParseInLocation(timeFormat, val, l)
  152. if err != nil {
  153. return err
  154. }
  155. value.Set(reflect.ValueOf(t))
  156. return nil
  157. }
  158. // Don't pass in pointers to bind to. Can lead to bugs. See:
  159. // https://github.com/codegangsta/martini-contrib/issues/40
  160. // https://github.com/codegangsta/martini-contrib/pull/34#issuecomment-29683659
  161. func ensureNotPointer(obj interface{}) {
  162. if reflect.TypeOf(obj).Kind() == reflect.Ptr {
  163. panic("Pointers are not accepted as binding models")
  164. }
  165. }