package
1.4.2
Repository: https://github.com/forkyid/go-utils.git
Documentation: pkg.go.dev

# README

Contents

Response Helper

Each of the three functions listed below returns a logger containing the current context, and a uuid. This allows the function to be chained with .Log(msg string) to create a log in kibana. Alternatively, should there be a need for multiple logs, or further processing after the response, the logger could be stored in a variable to be used later.

Examples

Chaining

rest.ResponseMessage(context, http.StatusInternalServerError).Log(
 "get bank accounts failed: " + err.Error())

Stored logger

logger := rest.ResponseMessage(context, http.StatusOK)

err := someProcess()
if err != nil {
    logger.Log(err.Error()
}

err = moreProcess()
if err != nil {
    logger.Log(err.Error()
}

Response Message

For non-2xx status codes, creates response in the form of

{
    "error": uuid,
    "message": message
}

For 2xx status codes, the error uuid is omitted.

Example

Default message

rest.ResponseMessage(context, http.StatusOK)

Custom Message

rest.ResponseMessage(context, http.StatusOK, "payment success")

Response Data

"result": payload,
"message": message

Accepts payload of type interface{}

Example
result, _ := service.DonationService.GetDonationByID(donationID)
rest.ResponseData(context, http.StatusOK, result)

Response Error

{
    "error": uuid,
    "message": message,
    "detail": {
        "field": error_detail
    }
}

ResponseError accepts an additional parameter of type validator.ValidationErrors, map[string]string, validation.ErrorDetails, or string.

Details in the form of string:

"detail": {
    "error": detail
}

validator.ValidationErrors:

"detail": {
    lowercase_error_field: error_tag
    ...
    lowercase_error_field: error_tag
}

map[string]string, validation.ErrorDetails:

"detail": detail
Examples

validator.ValidationErrors:

err = constants.Validator.Struct(requestBody)
if err != nil {
 rest.ResponseError(context, http.StatusBadRequest, err)
 return
}

map[string]string:

rest.ResponseError(context, http.StatusBadRequest, map[string]string{"id": "invalid id"})

validation.ErrorDetails:

det, code := validation.Validate(requestBody)
if det != nil {
    rest.ResponseError(context, code, det)
}

# Packages

No description provided by the author

# Functions

@BindFormData params @ctx: *gin.Context @v: interface{} return error.
BindJSON params @ctx: *gin.Context @v: interface{} return error.
BindMultipartFormData params @ctx: *gin.Context @v: interface{} return error.
BindQuery params @ctx: *gin.Context @v: interface{} return error.
GetData unwraps "result" object.
MultipartForm creates multipart payload.
PublishLog params @context: *gin.Context @status: int @payload: interface @msg: []string @return error.
ResponseData params @context: *gin.Context status: int msg: string.
ResponseError params @context: *gin.Context status: int msg: string detail: array.
ResponseMessage params @context: *gin.Context status: int msg: string.
ResponsePagination params @context: *gin.Context @status: int @params: ResponsePaginationParams return ResponseResult.
ValidMethod params @method: string return bool.

# Variables

Validator validator.

# Structs

400 Bad Request.
201 Created.
403 Forbidden.
Generic error message.
410 Gone.
500 Internal Server Error.
423 Locked.
LogData types.
LogRequest types.
404 Not Found.
200 OK.
Request type.
409 Resource Conflict.
Response types.
ResponsePaginationParams types.
ResponsePaginationResult types.
ResponseResult types.
401 Unauthorized.
422 Unprocessable Entity.

# Type aliases

ErrorDetails contains '|' separated details for each field.
No description provided by the author