github.heygears.com/openimsdk/tools@v0.0.49/apiresp/resp.go (about)

     1  // Copyright © 2023 OpenIM. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package apiresp
    16  
    17  import (
    18  	"encoding/json"
    19  	"reflect"
    20  
    21  	"github.com/openimsdk/tools/errs"
    22  	"github.com/openimsdk/tools/utils/jsonutil"
    23  )
    24  
    25  type ApiResponse struct {
    26  	ErrCode int    `json:"errCode"`
    27  	ErrMsg  string `json:"errMsg"`
    28  	ErrDlt  string `json:"errDlt"`
    29  	Data    any    `json:"data,omitempty"`
    30  }
    31  
    32  func (r *ApiResponse) MarshalJSON() ([]byte, error) {
    33  	type apiResponse ApiResponse
    34  	tmp := (*apiResponse)(r)
    35  	if tmp.Data != nil {
    36  		if format, ok := tmp.Data.(ApiFormat); ok {
    37  			format.ApiFormat()
    38  		}
    39  		if isAllFieldsPrivate(tmp.Data) {
    40  			tmp.Data = json.RawMessage(nil)
    41  		} else {
    42  			data, err := jsonutil.JsonMarshal(tmp.Data)
    43  			if err != nil {
    44  				return nil, err
    45  			}
    46  			tmp.Data = json.RawMessage(data)
    47  		}
    48  	}
    49  	return jsonutil.JsonMarshal(tmp)
    50  }
    51  
    52  func isAllFieldsPrivate(v any) bool {
    53  	typeOf := reflect.TypeOf(v)
    54  	if typeOf == nil {
    55  		return false
    56  	}
    57  	for typeOf.Kind() == reflect.Ptr {
    58  		typeOf = typeOf.Elem()
    59  	}
    60  	if typeOf.Kind() != reflect.Struct {
    61  		return false
    62  	}
    63  	num := typeOf.NumField()
    64  	for i := 0; i < num; i++ {
    65  		c := typeOf.Field(i).Name[0]
    66  		if c >= 'A' && c <= 'Z' {
    67  			return false
    68  		}
    69  	}
    70  	return true
    71  }
    72  
    73  func ApiSuccess(data any) *ApiResponse {
    74  	return &ApiResponse{Data: data}
    75  }
    76  
    77  func ParseError(err error) *ApiResponse {
    78  	if err == nil {
    79  		return ApiSuccess(nil)
    80  	}
    81  	unwrap := errs.Unwrap(err)
    82  	if codeErr, ok := unwrap.(errs.CodeError); ok {
    83  		resp := ApiResponse{ErrCode: codeErr.Code(), ErrMsg: codeErr.Msg(), ErrDlt: codeErr.Detail()}
    84  		if resp.ErrDlt == "" {
    85  			resp.ErrDlt = err.Error()
    86  		}
    87  		return &resp
    88  	}
    89  	return &ApiResponse{ErrCode: errs.ServerInternalError, ErrMsg: err.Error()}
    90  }