formatter.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package rest
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "gorm.io/gorm"
  7. "reflect"
  8. "strconv"
  9. "sync"
  10. "time"
  11. )
  12. var (
  13. DefaultFormatter = NewFormatter()
  14. DefaultNullDisplay = ""
  15. )
  16. func init() {
  17. DefaultFormatter.Register("string", stringFormat)
  18. DefaultFormatter.Register("integer", integerFormat)
  19. DefaultFormatter.Register("decimal", decimalFormat)
  20. DefaultFormatter.Register("date", dateFormat)
  21. DefaultFormatter.Register("time", timeFormat)
  22. DefaultFormatter.Register("datetime", datetimeFormat)
  23. DefaultFormatter.Register("duration", durationFormat)
  24. DefaultFormatter.Register("dropdown", dropdownFormat)
  25. DefaultFormatter.Register("timestamp", datetimeFormat)
  26. }
  27. type FormatFunc func(ctx context.Context, value interface{}, model interface{}, scm *Schema) interface{}
  28. type Formatter struct {
  29. callbacks sync.Map
  30. }
  31. func (formatter *Formatter) Register(f string, fun FormatFunc) {
  32. formatter.callbacks.Store(f, fun)
  33. }
  34. func (formatter *Formatter) Format(ctx context.Context, format string, value interface{}, model interface{}, scm *Schema) interface{} {
  35. v, ok := formatter.callbacks.Load(format)
  36. if ok {
  37. return v.(FormatFunc)(ctx, value, model, scm)
  38. }
  39. return value
  40. }
  41. func (formatter *Formatter) formatModel(ctx context.Context, val interface{}, schemas []*Schema, stmt *gorm.Statement) interface{} {
  42. values := make(map[string]interface{})
  43. refVal := reflect.Indirect(reflect.ValueOf(val))
  44. for _, field := range schemas {
  45. if stmt.Schema != nil {
  46. f := stmt.Schema.LookUpField(field.Column)
  47. values[field.Column] = formatter.Format(ctx, field.Format, refVal.FieldByName(f.Name).Interface(), val, field)
  48. } else {
  49. values[field.Column] = ""
  50. }
  51. }
  52. return values
  53. }
  54. func (formatter *Formatter) formatModels(ctx context.Context, val interface{}, schemas []*Schema, stmt *gorm.Statement) interface{} {
  55. reflectValue := reflect.Indirect(reflect.ValueOf(val))
  56. if reflectValue.Type().Kind() != reflect.Slice {
  57. return nil
  58. }
  59. length := reflectValue.Len()
  60. values := make([]interface{}, length)
  61. for i := 0; i < length; i++ {
  62. values[i] = formatter.formatModel(ctx, reflectValue.Index(i).Interface(), schemas, stmt)
  63. }
  64. return values
  65. }
  66. func stringFormat(ctx context.Context, value interface{}, model interface{}, scm *Schema) interface{} {
  67. return fmt.Sprint(value)
  68. }
  69. func integerFormat(ctx context.Context, value interface{}, model interface{}, scm *Schema) interface{} {
  70. var (
  71. n int
  72. )
  73. switch value.(type) {
  74. case float32, float64:
  75. n = int(reflect.ValueOf(value).Float())
  76. case int, int8, int16, int32, int64:
  77. n = int(reflect.ValueOf(value).Int())
  78. case uint, uint8, uint16, uint32, uint64:
  79. n = int(reflect.ValueOf(value).Uint())
  80. case string:
  81. n, _ = strconv.Atoi(reflect.ValueOf(value).String())
  82. case []byte:
  83. n, _ = strconv.Atoi(string(reflect.ValueOf(value).Bytes()))
  84. }
  85. return n
  86. }
  87. func decimalFormat(ctx context.Context, value interface{}, model interface{}, scm *Schema) interface{} {
  88. var (
  89. n float64
  90. )
  91. switch value.(type) {
  92. case float32, float64:
  93. n = float64(reflect.ValueOf(value).Float())
  94. case int, int8, int16, int32, int64:
  95. n = float64(reflect.ValueOf(value).Int())
  96. case uint, uint8, uint16, uint32, uint64:
  97. n = float64(reflect.ValueOf(value).Uint())
  98. case string:
  99. n, _ = strconv.ParseFloat(reflect.ValueOf(value).String(), 64)
  100. case []byte:
  101. n, _ = strconv.ParseFloat(string(reflect.ValueOf(value).Bytes()), 64)
  102. }
  103. return n
  104. }
  105. func dateFormat(ctx context.Context, value interface{}, model interface{}, scm *Schema) interface{} {
  106. if t, ok := value.(time.Time); ok {
  107. return t.Format("2006-01-02")
  108. }
  109. if t, ok := value.(*sql.NullTime); ok {
  110. if t != nil && t.Valid {
  111. return t.Time.Format("2006-01-02")
  112. }
  113. }
  114. if t, ok := value.(int64); ok {
  115. tm := time.Unix(t, 0)
  116. return tm.Format("2006-01-02")
  117. }
  118. return DefaultNullDisplay
  119. }
  120. func timeFormat(ctx context.Context, value interface{}, model interface{}, scm *Schema) interface{} {
  121. if t, ok := value.(time.Time); ok {
  122. return t.Format("15:04:05")
  123. }
  124. if t, ok := value.(*sql.NullTime); ok {
  125. if t != nil && t.Valid {
  126. return t.Time.Format("15:04:05")
  127. }
  128. }
  129. if t, ok := value.(int64); ok {
  130. tm := time.Unix(t, 0)
  131. return tm.Format("15:04:05")
  132. }
  133. return DefaultNullDisplay
  134. }
  135. func datetimeFormat(ctx context.Context, value interface{}, model interface{}, scm *Schema) interface{} {
  136. if t, ok := value.(time.Time); ok {
  137. return t.Format("2006-01-02 15:04:05")
  138. }
  139. if t, ok := value.(*sql.NullTime); ok {
  140. if t != nil && t.Valid {
  141. return t.Time.Format("2006-01-02 15:04:05")
  142. }
  143. }
  144. if t, ok := value.(int64); ok {
  145. if t > 0 {
  146. tm := time.Unix(t, 0)
  147. return tm.Format("2006-01-02 15:04:05")
  148. }
  149. }
  150. return DefaultNullDisplay
  151. }
  152. func durationFormat(ctx context.Context, value interface{}, model interface{}, scm *Schema) interface{} {
  153. var (
  154. hour int
  155. min int
  156. sec int
  157. )
  158. n := integerFormat(ctx, value, model, scm).(int)
  159. hour = n / 3600
  160. min = (n - hour*3600) / 60
  161. sec = n - hour*3600 - min*60
  162. return fmt.Sprintf("%02d:%02d:%02d", hour, min, sec)
  163. }
  164. func dropdownFormat(ctx context.Context, value interface{}, model interface{}, scm *Schema) interface{} {
  165. attributes := scm.Attribute
  166. if attributes.Values != nil {
  167. for _, v := range attributes.Values {
  168. if v.Value == value {
  169. return v.Label
  170. }
  171. }
  172. }
  173. return value
  174. }
  175. func NewFormatter() *Formatter {
  176. formatter := &Formatter{}
  177. return formatter
  178. }