reply.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package tyr
  2. // Error code & constant
  3. const (
  4. ErrOk = 0
  5. ErrNotFound = 1001 // ErrNotFound 404 route not found
  6. ErrException = 1002 // ErrException 500
  7. ErrBadRequest = 1003 // ErrBadRequest 400 route params error
  8. ErrMethodNotAllowed = 1004 // ErrMethodNotAllowed 405
  9. ErrParamsError = 1005 // ErrParamsError 415
  10. ErrUnAuthorized = 1006 // ErrUnAuthorized 401
  11. ErrDataNotFound = 1007 // ErrDataNotFound 404
  12. ErrNotAllowed = 1008 // ErrNotAllowed 405
  13. ErrDataExists = 1009 // ErrDataExists 400
  14. ErrDataValidate = 1010 // ErrDataValidate 403
  15. VarUserAuthorization = `access_token` // oauth token
  16. HTTPHeaderAuthorization = `Authorization` // HTTP header Authorization
  17. HTTPHeaderToken = `X-Token` // HTTP header Authorization X-Token
  18. )
  19. var (
  20. statusMessage map[int]string
  21. )
  22. // ReplyData define API output data
  23. type ReplyData struct {
  24. Status int `json:"status" xml:"status"` // Status code
  25. Message string `json:"message" xml:"message"` // Message description
  26. Errs map[string]string `json:"errors,omitempty" xml:"errors,omitempty"` // Errs errors
  27. PageCount int `json:"pageCount,omitempty"`
  28. Total int `json:"total,omitempty" xml:"total,omitempty"` // Total data total
  29. List interface{} `json:"rows,omitempty" xml:"rows,omitempty"` // List data list
  30. Data interface{} `json:"data,omitempty" xml:"data,omitempty"` // Data data attribute
  31. }
  32. func init() {
  33. statusMessage = make(map[int]string)
  34. statusMessage[ErrOk] = `ok`
  35. statusMessage[ErrNotFound] = `Not found`
  36. statusMessage[ErrException] = `Exception`
  37. statusMessage[ErrBadRequest] = `Routing parameter error`
  38. statusMessage[ErrMethodNotAllowed] = `Method not allowed`
  39. statusMessage[ErrParamsError] = `Parameter or format error`
  40. statusMessage[ErrUnAuthorized] = `Not sign in or session has expired`
  41. statusMessage[ErrDataNotFound] = `Data not found`
  42. statusMessage[ErrNotAllowed] = `No access`
  43. statusMessage[ErrDataExists] = `Data exists`
  44. statusMessage[ErrDataValidate] = `Data verification failed`
  45. }
  46. // NewReplyData creates and return ReplyData with status and message
  47. func NewReplyData(status int) *ReplyData {
  48. var (
  49. text string
  50. exists bool
  51. )
  52. if text, exists = statusMessage[status]; !exists {
  53. text = `incorrect data type`
  54. }
  55. return &ReplyData{
  56. Status: status,
  57. Message: text,
  58. }
  59. }
  60. // OkReplyData creates and return ReplyData with ok
  61. func OkReplyData() *ReplyData {
  62. message, _ := statusMessage[ErrOk]
  63. return &ReplyData{
  64. Status: ErrOk,
  65. Message: message,
  66. }
  67. }
  68. // ErrReplyData creates and return ReplyData with error and message
  69. func ErrReplyData(status int, message string) *ReplyData {
  70. text, _ := statusMessage[status]
  71. errs := map[string]string{
  72. "message": message,
  73. }
  74. return &ReplyData{
  75. Status: status,
  76. Message: text,
  77. Errs: errs,
  78. }
  79. }
  80. // ErrorsReplyData creates and return ReplyData with errors
  81. func ErrorsReplyData(status int, errors map[string]string) *ReplyData {
  82. message, _ := statusMessage[status]
  83. return &ReplyData{
  84. Status: status,
  85. Message: message,
  86. Errs: errors,
  87. }
  88. }
  89. // RowsReplyData creates and return ReplyData with total and list
  90. func RowsReplyData(total, pageCount int, rows interface{}) *ReplyData {
  91. message, _ := statusMessage[ErrOk]
  92. return &ReplyData{
  93. Status: ErrOk,
  94. Message: message,
  95. List: rows,
  96. Total: total,
  97. PageCount: pageCount,
  98. }
  99. }
  100. // RowReplyData creates and return ReplyData with attr row
  101. func RowReplyData(row interface{}) *ReplyData {
  102. message, _ := statusMessage[ErrOk]
  103. return &ReplyData{
  104. Status: ErrOk,
  105. Message: message,
  106. Data: row,
  107. }
  108. }