Skip to content

Commit

Permalink
feat: replace interface{} param with generics
Browse files Browse the repository at this point in the history
  • Loading branch information
ayaanqui committed Feb 12, 2024
1 parent 6d7dda8 commit 3b0fc4b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/utils/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// Given a JSON file, map the contents into any struct dest
func FileMapper(filename string, dest interface{}) error {
func FileMapper[T any](filename string, dest T) error {
file, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("%s not found", filename)
Expand Down
8 changes: 4 additions & 4 deletions src/utils/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"github.com/gofiber/fiber/v2"
)

func ResponseWithStatusCode(c *fiber.Ctx, statusCode int, data interface{}) error {
func ResponseWithStatusCode[T any](c *fiber.Ctx, statusCode int, data T) error {
return c.Status(statusCode).JSON(data)
}

// Generic json response with status code 200
func JsonResponse(c *fiber.Ctx, data interface{}) error {
func JsonResponse[T any](c *fiber.Ctx, data T) error {
return ResponseWithStatusCode(c, fiber.StatusOK, data)
}

Expand All @@ -36,11 +36,11 @@ func FailResponseNotFound(c *fiber.Ctx, errors ...string) error {
}

// types.Data json response with status code 200
func DataResponse(c *fiber.Ctx, data interface{}) error {
func DataResponse[T any](c *fiber.Ctx, data T) error {
return ResponseWithStatusCode(c, fiber.StatusOK, data)
}

// types.Data json response with status code 201
func DataResponseCreated(c *fiber.Ctx, data interface{}) error {
func DataResponseCreated[T any](c *fiber.Ctx, data T) error {
return ResponseWithStatusCode(c, fiber.StatusCreated, data)
}

0 comments on commit 3b0fc4b

Please sign in to comment.