github.com/aaabigfish/gopkg@v1.1.0/ginx/response.go (about)

     1  package ginx
     2  
     3  import "fmt"
     4  
     5  const (
     6  	ResultFail = 0
     7  	ResultOk   = 200
     8  )
     9  
    10  const (
    11  	ErrorCodeSuccess         = "200"
    12  	ErrorCodeFail            = "0"
    13  	ErrorCodeInvalidArgument = "400"
    14  	ErrorCodeUnauthenticated = "401"
    15  )
    16  
    17  const (
    18  	ErrorMsgSuccess         = "OK"
    19  	ErrorMsgFail            = "ERROR"
    20  	ErrorMsgInvalidArgument = "InvalidArgument"
    21  	ErrorMsgNotLogin        = "Unauthenticated"
    22  )
    23  
    24  type M map[string]interface{}
    25  
    26  type Result struct {
    27  	ResultCode int         `json:"code"`
    28  	ErrorCode  string      `json:"error_code,omitempty"`
    29  	ErrorMsg   string      `json:"message,omitempty"`
    30  	Data       interface{} `json:"data,omitempty"`
    31  }
    32  
    33  type PageInfo struct {
    34  	Total int64       `json:"total"`
    35  	Page  int         `json:"page"`
    36  	Size  int         `json:"size"`
    37  	List  interface{} `json:"list"`
    38  }
    39  
    40  type Option func(*Result)
    41  
    42  func ResultCode(resultCode int) Option {
    43  	return func(ret *Result) {
    44  		ret.ResultCode = resultCode
    45  	}
    46  }
    47  
    48  func ErrorCode(errorCode string) Option {
    49  	return func(ret *Result) {
    50  		ret.ErrorCode = errorCode
    51  	}
    52  }
    53  
    54  func ErrorMsg(errorMsg string) Option {
    55  	return func(ret *Result) {
    56  		ret.ErrorMsg = errorMsg
    57  	}
    58  }
    59  
    60  func Data(data interface{}) Option {
    61  	return func(ret *Result) {
    62  		ret.Data = data
    63  	}
    64  }
    65  
    66  func New(options ...Option) *Result {
    67  	ret := &Result{}
    68  
    69  	for _, opt := range options {
    70  		opt(ret)
    71  	}
    72  
    73  	return ret
    74  }
    75  
    76  // 1:error_msg 2:error_code
    77  func NewFailData(data interface{}, errs ...string) *Result {
    78  	ret := &Result{
    79  		ResultCode: ResultFail,
    80  		ErrorCode:  ErrorCodeFail,
    81  		ErrorMsg:   ErrorMsgFail,
    82  	}
    83  
    84  	ret.Data = data
    85  
    86  	for i, err := range errs {
    87  		if i == 0 { // error_msg
    88  			ret.ErrorMsg = err
    89  		} else if i == 1 { // error_code
    90  			ret.ErrorCode = err
    91  		} else {
    92  			break
    93  		}
    94  	}
    95  
    96  	return ret
    97  }
    98  
    99  // errs 1:error_msg 2:error_code
   100  func NewFail(code int, data interface{}, errs ...string) *Result {
   101  	ret := &Result{
   102  		ResultCode: code,
   103  		ErrorCode:  ErrorCodeFail,
   104  		ErrorMsg:   ErrorMsgFail,
   105  	}
   106  
   107  	ret.Data = data
   108  
   109  	for i, err := range errs {
   110  		if i == 0 { // error_msg
   111  			ret.ErrorMsg = err
   112  		} else if i == 1 { // error_code
   113  			ret.ErrorCode = err
   114  		} else {
   115  			break
   116  		}
   117  	}
   118  
   119  	return ret
   120  }
   121  
   122  // errs 1:error_msg 2:error_code
   123  func NewFailMsg(errs ...string) *Result {
   124  	ret := &Result{
   125  		ResultCode: ResultFail,
   126  		ErrorCode:  ErrorCodeFail,
   127  		ErrorMsg:   ErrorMsgFail,
   128  	}
   129  
   130  	for i, err := range errs {
   131  		if i == 0 { // error_msg
   132  			ret.ErrorMsg = err
   133  		} else if i == 1 { // error_code
   134  			ret.ErrorCode = err
   135  		} else {
   136  			break
   137  		}
   138  	}
   139  
   140  	return ret
   141  }
   142  
   143  // 1:data 2:error_msg 3:error_code
   144  func NewOk(data interface{}, errs ...string) *Result {
   145  	ret := &Result{
   146  		ResultCode: ResultOk,
   147  		ErrorCode:  ErrorCodeSuccess,
   148  		ErrorMsg:   ErrorMsgSuccess,
   149  	}
   150  
   151  	ret.Data = data
   152  
   153  	for i, err := range errs {
   154  		if i == 0 { // error_msg
   155  			ret.ErrorMsg = err
   156  		} else if i == 1 { // error_code
   157  			ret.ErrorCode = err
   158  		} else {
   159  			break
   160  		}
   161  	}
   162  
   163  	return ret
   164  }
   165  
   166  // 1:PageInfo 2:error_msg 3:error_code
   167  func Page(page *PageInfo, errs ...string) *Result {
   168  	ret := &Result{
   169  		ResultCode: ResultOk,
   170  		ErrorCode:  ErrorCodeSuccess,
   171  		ErrorMsg:   ErrorMsgSuccess,
   172  	}
   173  
   174  	ret.Data = page
   175  
   176  	for i, err := range errs {
   177  		if i == 0 { // error_msg
   178  			ret.ErrorMsg = err
   179  		} else if i == 1 { // error_code
   180  			ret.ErrorCode = err
   181  		} else {
   182  			break
   183  		}
   184  	}
   185  
   186  	return ret
   187  }
   188  
   189  func InvalidArgumentError() *Result {
   190  	ret := &Result{
   191  		ResultCode: ResultFail,
   192  		ErrorCode:  ErrorCodeInvalidArgument,
   193  		ErrorMsg:   ErrorMsgInvalidArgument,
   194  	}
   195  
   196  	return ret
   197  }
   198  
   199  func NotLoginError() *Result {
   200  	ret := &Result{
   201  		ResultCode: ResultFail,
   202  		ErrorCode:  ErrorCodeUnauthenticated,
   203  		ErrorMsg:   ErrorMsgNotLogin,
   204  	}
   205  
   206  	return ret
   207  }
   208  
   209  func (s *Result) String() string {
   210  	return fmt.Sprintf("ResultCode:[%d] ErrorCode:[%s]%s", s.ResultCode, s.ErrorCode, s.ErrorMsg)
   211  }