-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from go-park-mail-ru/f-easyjson
Easyjson везде
- Loading branch information
Showing
15 changed files
with
4,002 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.