Skip to content

Commit

Permalink
feat: added json encoder and decoder wrappers
Browse files Browse the repository at this point in the history
* Added json encoder and decoder wrappers
* Added .gitignore file
  • Loading branch information
ralvarezdev committed Dec 29, 2024
1 parent fcb5e94 commit 41a3b9b
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# IDE folders
/.idea
/.vscode

# Environment variables
/**/*.env

# Executable files
/**/*.exe

# Private and public keys
/**/*.ed
/**/*.pub
/**/*.pem

# Commands
/**/*.cmd
/**/*.sh
/**/*.bat
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module github.com/ralvarezdev/go-net

go 1.23.4

require github.com/ralvarezdev/go-flags v0.2.1

require github.com/ralvarezdev/go-logger v0.2.1 // indirect
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/ralvarezdev/go-flags v0.2.1 h1:zLhEZcb8jaaq5y+Gh7ot1NRaBLKvl0zyNA4st7dT5p0=
github.com/ralvarezdev/go-flags v0.2.1/go.mod h1:pYw9H7NJ07Y5asZDC/EI5bpBLR0kdL2ISsh6X5ws+3s=
github.com/ralvarezdev/go-logger v0.1.0 h1:i2AI1nlxU6Hizvk75Vc8wtFydiVrqIeeRbJwiuO/69A=
github.com/ralvarezdev/go-logger v0.1.0/go.mod h1:v5OvFrkS+wsYNTCVegXWiRhBtcYrQJr4LDMDntvpAos=
github.com/ralvarezdev/go-logger v0.2.1 h1:DamvM2hbiJlxlQ+3pd2yNWowSWm7XBFVjJPg74s+/t0=
github.com/ralvarezdev/go-logger v0.2.1/go.mod h1:v5OvFrkS+wsYNTCVegXWiRhBtcYrQJr4LDMDntvpAos=
10 changes: 10 additions & 0 deletions http/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package http

import (
"net/http"
)

var (
InternalServerError = http.StatusText(http.StatusInternalServerError)
BadRequest = http.StatusText(http.StatusBadRequest)
)
47 changes: 47 additions & 0 deletions http/json/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package json

import (
goflagsmode "github.com/ralvarezdev/go-flags/mode"
gonethttp "github.com/ralvarezdev/go-net/http"
"net/http"
"reflect"
)

// checkJSONData checks if the given JSON data is nil or if the reflected data is not a pointer
func checkJSONData(
w http.ResponseWriter,
data interface{},
mode *goflagsmode.Flag,
) error {
// Check if data is nil
if data == nil {
return handleDataTypeError(w, ErrNilJSONData, mode)
}

// Check if the reflected data is a pointer
if reflect.ValueOf(data).Kind() != reflect.Ptr {
return handleDataTypeError(w, ErrJSONDataMustBeAPointer, mode)
}
return nil
}

// handleDataTypeError handles the data type error
func handleDataTypeError(
w http.ResponseWriter,
err error,
mode *goflagsmode.Flag,
) error {
if mode != nil && mode.IsDebug() {
http.Error(
w,
err.Error(),
http.StatusInternalServerError,
)
}
http.Error(
w,
gonethttp.InternalServerError,
http.StatusInternalServerError,
)
return err
}
54 changes: 54 additions & 0 deletions http/json/decoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package json

import (
"encoding/json"
goflagsmode "github.com/ralvarezdev/go-flags/mode"
"net/http"
)

type (
// Decoder is the JSON decoder interface
Decoder interface {
Decode(
w http.ResponseWriter,
r *http.Request,
data interface{},
) (err error)
}

// DefaultDecoder is the JSON decoder struct
DefaultDecoder struct {
mode *goflagsmode.Flag
}
)

// NewDefaultDecoder creates a new JSON decoder
func NewDefaultDecoder(mode *goflagsmode.Flag) *DefaultDecoder {
return &DefaultDecoder{
mode: mode,
}
}

// Decode decodes the JSON data
func (d *DefaultDecoder) Decode(
w http.ResponseWriter,
r *http.Request,
data interface{},
) (err error) {
// Check the data type
if err = checkJSONData(w, data, d.mode); err != nil {
return err
}

// Decode JSON data
err = json.NewDecoder(r.Body).Decode(data)
if err != nil {
http.Error(
w,
err.Error(),
http.StatusBadRequest,
)
return err
}
return nil
}
45 changes: 45 additions & 0 deletions http/json/encoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package json

import (
"encoding/json"
goflagsmode "github.com/ralvarezdev/go-flags/mode"
"net/http"
)

type (
// Encoder is an interface for encoding JSON data
Encoder interface {
Encode(w http.ResponseWriter, data interface{}) (err error)
}

// DefaultEncoder is the JSON encoder struct
DefaultEncoder struct {
mode *goflagsmode.Flag
}
)

// NewDefaultEncoder creates a new JSON encoder
func NewDefaultEncoder(mode *goflagsmode.Flag) *DefaultEncoder {
return &DefaultEncoder{
mode: mode,
}
}

// Encode encodes the data into JSON
func (d *DefaultEncoder) Encode(
w http.ResponseWriter,
data interface{},
) (err error) {
// Check the data type
if err = checkJSONData(w, data, d.mode); err != nil {
return err
}

// Encode JSON data
w.Header().Set("Content-Type", "application/json")
if err = json.NewEncoder(w).Encode(data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return err
}
return nil
}
10 changes: 10 additions & 0 deletions http/json/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package json

import (
"errors"
)

var (
ErrNilJSONData = errors.New("json data is nil")
ErrJSONDataMustBeAPointer = errors.New("json data must be a pointer")
)

0 comments on commit 41a3b9b

Please sign in to comment.