github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/common/types.go (about) 1 package common 2 3 import ( 4 "encoding/json" 5 ) 6 7 // BaseResponse is the main frame of response 8 type BaseResponse struct { 9 Code uint32 `json:"code"` 10 Msg string `json:"msg"` 11 DetailMsg string `json:"detail_msg"` 12 Data interface{} `json:"data"` 13 } 14 15 // GetErrorResponse creates an error base response 16 func GetErrorResponse(code uint32, msg, detailMsg string) *BaseResponse { 17 return &BaseResponse{ 18 Code: code, 19 DetailMsg: detailMsg, 20 Msg: msg, 21 Data: nil, 22 } 23 } 24 25 // GetErrorResponseJSON marshals the base response into JSON bytes 26 func GetErrorResponseJSON(code uint32, msg, detailMsg string) []byte { 27 res, err := json.Marshal(BaseResponse{ 28 Code: code, 29 DetailMsg: detailMsg, 30 Msg: msg, 31 Data: nil, 32 }) 33 if err != nil { 34 return []byte(err.Error()) 35 } 36 return res 37 } 38 39 // GetBaseResponse gets a default base response 40 func GetBaseResponse(data interface{}) *BaseResponse { 41 return &BaseResponse{ 42 Code: 0, 43 Msg: "", 44 DetailMsg: "", 45 Data: data, 46 } 47 } 48 49 // ParamPage is the struct of params page 50 type ParamPage struct { 51 Page int `json:"page"` 52 PerPage int `json:"per_page"` 53 Total int `json:"total"` 54 } 55 56 // ListDataRes is the struct of list data result 57 type ListDataRes struct { 58 Data interface{} `json:"data"` 59 ParamPage ParamPage `json:"param_page"` 60 } 61 62 // ListResponse is the frame of list response 63 type ListResponse struct { 64 Code int `json:"code"` 65 Msg string `json:"msg"` 66 DetailMsg string `json:"detail_msg"` 67 Data ListDataRes `json:"data"` 68 } 69 70 // GetListResponse returns a list response 71 func GetListResponse(total, page, perPage int, data interface{}) *ListResponse { 72 return &ListResponse{ 73 Code: 0, 74 Msg: "", 75 DetailMsg: "", 76 Data: ListDataRes{ 77 Data: data, 78 ParamPage: ParamPage{page, perPage, total}, 79 }, 80 } 81 } 82 83 // GetEmptyListResponse returns an empty list response 84 func GetEmptyListResponse(total, page, perPage int) *ListResponse { 85 return &ListResponse{ 86 Code: 0, 87 Msg: "", 88 DetailMsg: "", 89 Data: ListDataRes{ 90 Data: []string{}, 91 ParamPage: ParamPage{page, perPage, total}, 92 }, 93 } 94 }