util.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2016 Qiang Xue. 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 validation
  5. import (
  6. "database/sql/driver"
  7. "errors"
  8. "fmt"
  9. "reflect"
  10. "time"
  11. )
  12. var (
  13. bytesType = reflect.TypeOf([]byte(nil))
  14. valuerType = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
  15. )
  16. // EnsureString ensures the given value is a string.
  17. // If the value is a byte slice, it will be typecast into a string.
  18. // An error is returned otherwise.
  19. func EnsureString(value interface{}) (string, error) {
  20. v := reflect.ValueOf(value)
  21. if v.Kind() == reflect.String {
  22. return v.String(), nil
  23. }
  24. if v.Type() == bytesType {
  25. return string(v.Interface().([]byte)), nil
  26. }
  27. return "", errors.New("must be either a string or byte slice")
  28. }
  29. // StringOrBytes typecasts a value into a string or byte slice.
  30. // Boolean flags are returned to indicate if the typecasting succeeds or not.
  31. func StringOrBytes(value interface{}) (isString bool, str string, isBytes bool, bs []byte) {
  32. v := reflect.ValueOf(value)
  33. if v.Kind() == reflect.String {
  34. str = v.String()
  35. isString = true
  36. } else if v.Kind() == reflect.Slice && v.Type() == bytesType {
  37. bs = v.Interface().([]byte)
  38. isBytes = true
  39. }
  40. return
  41. }
  42. // LengthOfValue returns the length of a value that is a string, slice, map, or array.
  43. // An error is returned for all other types.
  44. func LengthOfValue(value interface{}) (int, error) {
  45. v := reflect.ValueOf(value)
  46. switch v.Kind() {
  47. case reflect.String, reflect.Slice, reflect.Map, reflect.Array:
  48. return v.Len(), nil
  49. }
  50. return 0, fmt.Errorf("cannot get the length of %v", v.Kind())
  51. }
  52. // ToInt converts the given value to an int64.
  53. // An error is returned for all incompatible types.
  54. func ToInt(value interface{}) (int64, error) {
  55. v := reflect.ValueOf(value)
  56. switch v.Kind() {
  57. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  58. return v.Int(), nil
  59. }
  60. return 0, fmt.Errorf("cannot convert %v to int64", v.Kind())
  61. }
  62. // ToUint converts the given value to an uint64.
  63. // An error is returned for all incompatible types.
  64. func ToUint(value interface{}) (uint64, error) {
  65. v := reflect.ValueOf(value)
  66. switch v.Kind() {
  67. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  68. return v.Uint(), nil
  69. }
  70. return 0, fmt.Errorf("cannot convert %v to uint64", v.Kind())
  71. }
  72. // ToFloat converts the given value to a float64.
  73. // An error is returned for all incompatible types.
  74. func ToFloat(value interface{}) (float64, error) {
  75. v := reflect.ValueOf(value)
  76. switch v.Kind() {
  77. case reflect.Float32, reflect.Float64:
  78. return v.Float(), nil
  79. }
  80. return 0, fmt.Errorf("cannot convert %v to float64", v.Kind())
  81. }
  82. // IsEmpty checks if a value is empty or not.
  83. // A value is considered empty if
  84. // - integer, float: zero
  85. // - bool: false
  86. // - string, array: len() == 0
  87. // - slice, map: nil or len() == 0
  88. // - interface, pointer: nil or the referenced value is empty
  89. func IsEmpty(value interface{}) bool {
  90. v := reflect.ValueOf(value)
  91. switch v.Kind() {
  92. case reflect.String, reflect.Array, reflect.Map, reflect.Slice:
  93. return v.Len() == 0
  94. case reflect.Bool:
  95. return !v.Bool()
  96. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  97. return v.Int() == 0
  98. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  99. return v.Uint() == 0
  100. case reflect.Float32, reflect.Float64:
  101. return v.Float() == 0
  102. case reflect.Invalid:
  103. return true
  104. case reflect.Interface, reflect.Ptr:
  105. if v.IsNil() {
  106. return true
  107. }
  108. return IsEmpty(v.Elem().Interface())
  109. case reflect.Struct:
  110. v, ok := value.(time.Time)
  111. if ok && v.IsZero() {
  112. return true
  113. }
  114. }
  115. return false
  116. }
  117. // Indirect returns the value that the given interface or pointer references to.
  118. // If the value implements driver.Valuer, it will deal with the value returned by
  119. // the Value() method instead. A boolean value is also returned to indicate if
  120. // the value is nil or not (only applicable to interface, pointer, map, and slice).
  121. // If the value is neither an interface nor a pointer, it will be returned back.
  122. func Indirect(value interface{}) (interface{}, bool) {
  123. rv := reflect.ValueOf(value)
  124. kind := rv.Kind()
  125. switch kind {
  126. case reflect.Invalid:
  127. return nil, true
  128. case reflect.Ptr, reflect.Interface:
  129. if rv.IsNil() {
  130. return nil, true
  131. }
  132. return Indirect(rv.Elem().Interface())
  133. case reflect.Slice, reflect.Map, reflect.Func, reflect.Chan:
  134. if rv.IsNil() {
  135. return nil, true
  136. }
  137. }
  138. if rv.Type().Implements(valuerType) {
  139. return indirectValuer(value.(driver.Valuer))
  140. }
  141. return value, false
  142. }
  143. func indirectValuer(valuer driver.Valuer) (interface{}, bool) {
  144. if value, err := valuer.Value(); value != nil && err == nil {
  145. return Indirect(value)
  146. }
  147. return nil, true
  148. }