Skip to content

Commit

Permalink
Merge pull request #61 from go-park-mail-ru/f-easyjson
Browse files Browse the repository at this point in the history
Easyjson везде
  • Loading branch information
Regikon authored Dec 18, 2024
2 parents c539c4a + 8a05b79 commit feda9e0
Show file tree
Hide file tree
Showing 15 changed files with 4,002 additions and 188 deletions.
23 changes: 3 additions & 20 deletions internal/middleware/middleware_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package middleware_test

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
Expand All @@ -13,24 +12,8 @@ import (

func TestUniversalMarshalExpectSuccessMiddleware(t *testing.T) {
w := httptest.NewRecorder()
body := struct{
Val string `json:"value"`
} {
Val: "test",
}
err := middleware.UniversalMarshal(w, http.StatusOK, body)
err := middleware.UniversalMarshal(w, http.StatusOK, dto.JSONResponse{
HTTPStatus: http.StatusOK})
require.Equal(t, http.StatusOK, w.Result().StatusCode)
require.Nil(t, err)
}

func TestUniversalMarshalExpectErrorMiddleware(t *testing.T) {
w := httptest.NewRecorder()
body := struct{
Val chan int
} {
Val: make(chan int),
}
err := middleware.UniversalMarshal(w, http.StatusOK, body)
require.Equal(t, http.StatusOK, w.Result().StatusCode)
require.Equal(t, fmt.Errorf(dto.MsgUnableToMarshalJSON), err)
}
}
15 changes: 10 additions & 5 deletions internal/middleware/universalMarshal.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package middleware

import (
"encoding/json"
"fmt"
"net/http"

"github.com/go-park-mail-ru/2024_2_VKatuny/internal/pkg/dto"
"github.com/mailru/easyjson"
)

// UniversalMarshal marshal any struct to json (use it for any answer from handlers).
// Writes http status to http.ResponseWriter
func UniversalMarshal(w http.ResponseWriter, status int, body interface{}) error {
func UniversalMarshal(w http.ResponseWriter, status int, body dto.JSONResponse) error {
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(body); err != nil {
return fmt.Errorf(dto.MsgUnableToMarshalJSON)
jsonBytes, err := easyjson.Marshal(body)
if err != nil {
return err
}
_, err = w.Write(jsonBytes)
if err != nil {
return err
}
return nil
}
6 changes: 3 additions & 3 deletions internal/mux/staff_handlers.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package mux

import (
"encoding/json"
"net/http"

"github.com/go-park-mail-ru/2024_2_VKatuny/internal/pkg/commonerrors"
"github.com/go-park-mail-ru/2024_2_VKatuny/internal/pkg/dto"
"github.com/mailru/easyjson"
)

func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -16,7 +16,7 @@ func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
HTTPStatus: http.StatusNotFound,
Error: commonerrors.ErrFrontServiceNotFound.Error(),
}
JSONResponse, err := json.Marshal(response)
JSONResponse, err := easyjson.Marshal(response)
if err != nil {
return
}
Expand All @@ -31,7 +31,7 @@ func MethodNotAllowedHandler(w http.ResponseWriter, r *http.Request) {
HTTPStatus: http.StatusMethodNotAllowed,
Error: commonerrors.ErrFrontMethodNotAllowed.Error(),
}
JSONResponse, err := json.Marshal(response)
JSONResponse, err := easyjson.Marshal(response)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/applicant/delivery/applicant_handlers.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package delivery

import (
"encoding/json"
"net/http"

"github.com/go-park-mail-ru/2024_2_VKatuny/internal/middleware"
"github.com/go-park-mail-ru/2024_2_VKatuny/internal/pkg/dto"
"github.com/go-park-mail-ru/2024_2_VKatuny/internal/utils"
grpc_auth "github.com/go-park-mail-ru/2024_2_VKatuny/microservices/auth/gen"
"github.com/mailru/easyjson"
)

// CreateApplicantHandler creates applicant in db
Expand All @@ -30,7 +30,7 @@ func (h *ApplicantHandlers) ApplicantRegistration(w http.ResponseWriter, r *http

applicantRegistrationForm := new(dto.JSONApplicantRegistrationForm)

err := json.NewDecoder(r.Body).Decode(applicantRegistrationForm)
err := easyjson.UnmarshalFromReader(r.Body, applicantRegistrationForm)
if err != nil {
h.logger.Errorf("%s: got err %s", fn, err)
middleware.UniversalMarshal(w, http.StatusBadRequest, dto.JSONResponse{
Expand Down
19 changes: 10 additions & 9 deletions internal/pkg/applicant/delivery/applicant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/go-park-mail-ru/2024_2_VKatuny/internal/pkg/dto"
portfolio_mock "github.com/go-park-mail-ru/2024_2_VKatuny/internal/pkg/portfolio/mock"
vacancies_mock "github.com/go-park-mail-ru/2024_2_VKatuny/internal/pkg/vacancies/mock"
"github.com/mailru/easyjson"

file_loading_mock "github.com/go-park-mail-ru/2024_2_VKatuny/internal/pkg/file_loading/mock"
auth_grpc "github.com/go-park-mail-ru/2024_2_VKatuny/microservices/auth/gen"
Expand Down Expand Up @@ -45,7 +46,7 @@ func createMultipartForm(jsonForm *dto.JSONUpdateApplicantProfile) (*bytes.Buffe
func TestGetProfileHandler(t *testing.T) {
t.Parallel()
type usecase struct {
profile *mock.MockIApplicantUsecase
profile *mock.MockIApplicantUsecase
fileLoadingUsecase *file_loading_mock.MockIFileLoadingUsecase
}
tests := []struct {
Expand Down Expand Up @@ -146,7 +147,7 @@ func TestGetProfileHandler(t *testing.T) {
defer ctrl.Finish()

usecase := &usecase{
profile: mock.NewMockIApplicantUsecase(ctrl),
profile: mock.NewMockIApplicantUsecase(ctrl),
fileLoadingUsecase: file_loading_mock.NewMockIFileLoadingUsecase(ctrl),
}
tt.w, tt.r = tt.prepare(tt.r, tt.w, usecase)
Expand Down Expand Up @@ -669,7 +670,7 @@ func TestRegistration(t *testing.T) {
Password: "password",
}

jsonForm, _ := json.Marshal(form)
jsonForm, _ := easyjson.Marshal(form)
usecase.registration.
EXPECT().
Create(gomock.Any(), form).
Expand Down Expand Up @@ -702,8 +703,8 @@ func TestRegistration(t *testing.T) {
}

requestID := "1234567890"
jsonForm, _ := json.Marshal(form)
grpc_request := &auth_grpc.AuthRequest{
jsonForm, _ := easyjson.Marshal(form)
grpc_request := &auth_grpc.AuthRequest{
RequestID: requestID,
UserType: dto.UserTypeApplicant,
Email: form.Email,
Expand Down Expand Up @@ -751,7 +752,7 @@ func TestRegistration(t *testing.T) {
}

requestID := "1234567890"
jsonForm, _ := json.Marshal(form)
jsonForm, _ := easyjson.Marshal(form)
grpc_request := &auth_grpc.AuthRequest{
RequestID: requestID,
UserType: dto.UserTypeApplicant,
Expand Down Expand Up @@ -1017,8 +1018,8 @@ func TestGetFavoriteVacancies(t *testing.T) {
var vacancies = []*dto.JSONGetEmployerVacancy{
{
ID: 1,
EmployerID: 1,
Position: "химик",
EmployerID: 1,
Position: "химик",
Description: "нужен химик",
PositionCategoryName: "chemistry",
},
Expand Down Expand Up @@ -1053,7 +1054,7 @@ func TestGetFavoriteVacancies(t *testing.T) {
BackendAddress: "http://localhost:8080",
Usecases: &internal.Usecases{
ApplicantUsecase: nil,
VacanciesUsecase: usecase.vacancy,
VacanciesUsecase: usecase.vacancy,
PortfolioUsecase: nil,
FileLoadingUsecase: nil,
},
Expand Down
23 changes: 11 additions & 12 deletions internal/pkg/cvs/delivery/cvs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package delivery_test
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand All @@ -22,6 +21,7 @@ import (
"github.com/go-park-mail-ru/2024_2_VKatuny/internal/pkg/dto"
file_loading_mock "github.com/go-park-mail-ru/2024_2_VKatuny/internal/pkg/file_loading/mock"
"github.com/gorilla/mux"
"github.com/mailru/easyjson"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -267,7 +267,7 @@ func TestCreateCVHandler(t *testing.T) {
require.NoError(t, err)

gotJson := new(dto.JSONResponse)
err = json.Unmarshal(jsonData, gotJson)
err = easyjson.Unmarshal(jsonData, gotJson)
require.NoError(t, err)

require.EqualExportedValues(t, &dto.JSONResponse{
Expand All @@ -280,7 +280,7 @@ func TestCreateCVHandler(t *testing.T) {
require.NoError(t, err)

gotJson := new(dto.JSONResponse)
err = json.Unmarshal(jsonData, gotJson)
err = easyjson.Unmarshal(jsonData, gotJson)
require.NoError(t, err)

require.EqualExportedValues(t, &dto.JSONResponse{
Expand All @@ -293,7 +293,7 @@ func TestCreateCVHandler(t *testing.T) {
require.NoError(t, err)

gotJson := new(dto.JSONResponse)
err = json.Unmarshal(jsonData, gotJson)
err = easyjson.Unmarshal(jsonData, gotJson)
require.NoError(t, err)

require.EqualExportedValues(t, &dto.JSONResponse{
Expand All @@ -313,7 +313,7 @@ func TestGetCVHandler(t *testing.T) {
w *httptest.ResponseRecorder
}
type dependencies struct {
cvsUsecase *mock.MockICVsUsecase
cvsUsecase *mock.MockICVsUsecase
fileLoadingUsecase *file_loading_mock.MockIFileLoadingUsecase

cv dto.JSONCv
Expand Down Expand Up @@ -390,7 +390,7 @@ func TestGetCVHandler(t *testing.T) {
defer ctrl.Finish()

d := dependencies{
cvsUsecase: mock.NewMockICVsUsecase(ctrl),
cvsUsecase: mock.NewMockICVsUsecase(ctrl),
fileLoadingUsecase: file_loading_mock.NewMockIFileLoadingUsecase(ctrl),
}

Expand All @@ -404,9 +404,8 @@ func TestGetCVHandler(t *testing.T) {
app := &internal.App{
Logger: logger,
Usecases: &internal.Usecases{
CVUsecase: d.cvsUsecase,
CVUsecase: d.cvsUsecase,
FileLoadingUsecase: d.fileLoadingUsecase,

},
Microservices: &internal.Microservices{
Compress: nil,
Expand Down Expand Up @@ -630,7 +629,7 @@ func TestUpdateCVHandler(t *testing.T) {
)

jsonResonse := new(dto.JSONResponse)
err := json.NewDecoder(args.w.Result().Body).Decode(jsonResonse)
err := easyjson.UnmarshalFromReader(args.w.Result().Body, jsonResonse)
require.NoError(t, err)

require.Equalf(t, out.response, jsonResonse,
Expand Down Expand Up @@ -796,7 +795,7 @@ func TestDeleteCVHandler(t *testing.T) {
)

jsonResonse := new(dto.JSONResponse)
err := json.NewDecoder(args.w.Result().Body).Decode(jsonResonse)
err := easyjson.UnmarshalFromReader(args.w.Result().Body, jsonResonse)
require.NoError(t, err)

require.Equalf(t, out.response, jsonResonse,
Expand Down Expand Up @@ -859,7 +858,7 @@ func TestCVtoPDF(t *testing.T) {
out.response = &dto.JSONResponse{
HTTPStatus: out.status,
Body: map[string]interface{}{
"FileName": "/"+res.FileName,
"FileName": "/" + res.FileName,
},
}
slugInt, _ := strconv.Atoi(in.slug)
Expand Down Expand Up @@ -982,7 +981,7 @@ func TestCVtoPDF(t *testing.T) {
)

jsonResonse := new(dto.JSONResponse)
err := json.NewDecoder(args.w.Result().Body).Decode(jsonResonse)
err := easyjson.UnmarshalFromReader(args.w.Result().Body, jsonResonse)
require.NoError(t, err)
require.Equalf(t, out.response, jsonResonse,
"got response %v, expected %v",
Expand Down
Loading

0 comments on commit feda9e0

Please sign in to comment.