Skip to content

Commit

Permalink
add easy json
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyKomarovCoder committed Dec 19, 2023
1 parent 63dd28b commit c8a36b0
Show file tree
Hide file tree
Showing 23 changed files with 2,123 additions and 116 deletions.
29 changes: 17 additions & 12 deletions internal/common/http/response.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package http

import (
"encoding/json"
"github.com/mailru/easyjson"
"net/http"

"github.com/go-park-mail-ru/2023_2_Hamster/internal/common/logger"
Expand All @@ -15,11 +15,13 @@ const (

const minErrorToLogCode = 500

type Response[T any] struct {
Status int `json:"status"`
Body T `json:"body"`
//easyjson:json
type Response struct {
Status int `json:"status"`
Body interface{} `json:"body"`
}

//easyjson:json
type ResponseError struct {
Status int `json:"status"`
ErrMes string `json:"message"`
Expand All @@ -46,8 +48,9 @@ func ErrorResponse(w http.ResponseWriter, code int, err error, message string, l
log.Error(err.Error())
}

encoder := json.NewEncoder(w)
if err := encoder.Encode(errorMsg); err != nil {
// Marshal response using easyjson
_, _, err = easyjson.MarshalToHTTPResponseWriter(errorMsg, w)
if err != nil {
log.Errorf("Error failed to marshal error message: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)

Expand All @@ -58,13 +61,15 @@ func ErrorResponse(w http.ResponseWriter, code int, err error, message string, l
}

func SuccessResponse[T any](w http.ResponseWriter, status int, response T) {
date := Response[T]{Status: status, Body: response}
encoder := json.NewEncoder(w)
if err := encoder.Encode(date); err != nil {
w.WriteHeader(status)
return
}
date := Response{Status: status, Body: response}

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)

// Marshal response using easyjson
_, _, err := easyjson.MarshalToHTTPResponseWriter(date, w)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
177 changes: 177 additions & 0 deletions internal/common/http/response_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/microservices/account/delivery/http/handler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package http

import (
"encoding/json"
"errors"
"github.com/mailru/easyjson"
"net/http"

commonHttp "github.com/go-park-mail-ru/2023_2_Hamster/internal/common/http"
Expand Down Expand Up @@ -51,7 +51,7 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {

var accountInput CreateAccount

if err := json.NewDecoder(r.Body).Decode(&accountInput); err != nil {
if err := easyjson.UnmarshalFromReader(r.Body, &accountInput); err != nil {
commonHttp.ErrorResponse(w, http.StatusBadRequest, err, commonHttp.InvalidBodyRequest, h.logger)
return
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {

var updateAccountInput UpdateAccount

if err := json.NewDecoder(r.Body).Decode(&updateAccountInput); err != nil {
if err := easyjson.UnmarshalFromReader(r.Body, &updateAccountInput); err != nil {
commonHttp.ErrorResponse(w, http.StatusBadRequest, err, commonHttp.InvalidBodyRequest, h.logger)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ type AccountCreateResponse struct {
AccountID uuid.UUID `json:"account_id"`
}

//easyjson:json
type CreateAccount struct {
Balance float64 `json:"balance" valid:"-"`
Accumulation bool `json:"accumulation" valid:"-"`
BalanceEnabled bool `json:"balance_enabled" valid:"-"`
MeanPayment string `json:"mean_payment" valid:"required,length(1|30)"`
}

//easyjson:json
type UpdateAccount struct {
ID uuid.UUID `json:"id" valid:"required"`
Balance float64 `json:"balance" valid:""`
Expand Down
Loading

0 comments on commit c8a36b0

Please sign in to comment.