🧹 chore: address comments, and use more standard definition for

functionality we define
pull/1121/head
Yoofi Quansah 2021-01-23 02:15:59 -06:00
parent 2267f0f76f
commit 5941c8c4e2
5 changed files with 18 additions and 32 deletions

13
app.go
View File

@ -12,6 +12,7 @@ package fiber
import (
"bufio"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net"
@ -272,11 +273,11 @@ type Config struct {
// RedirectFixedPath bool
// When set by an external client of Fiber it will use the provided implementation of a
// JSONExectuor
// JSONMarshal
//
// Allowing for flexibility in using another json library for marshalling/unmarshalling
// Default: utils.DefaultJSONExecutor
JSONEngineExecutor utils.JSONExecutor `json:"-"`
// Allowing for flexibility in using another json library for encoding
// Default: json.Marshal
JSONEncoder utils.JSONMarshal `json:"-"`
}
// Static defines configuration options when defining static assets.
@ -385,8 +386,8 @@ func New(config ...Config) *App {
if app.config.ErrorHandler == nil {
app.config.ErrorHandler = DefaultErrorHandler
}
if app.config.JSONEngineExecutor == nil {
app.config.JSONEngineExecutor = &utils.DefaultJSONExecutor{}
if app.config.JSONEncoder == nil {
app.config.JSONEncoder = json.Marshal
}
// Init app

2
ctx.go
View File

@ -536,7 +536,7 @@ func (c *Ctx) Is(extension string) bool {
// and a nil slice encodes as the null JSON value.
// This method also sets the content header to application/json.
func (c *Ctx) JSON(data interface{}) error {
raw, err := c.app.config.JSONEngineExecutor.Marshal(data)
raw, err := c.app.config.JSONEncoder(data)
if err != nil {
return err
}

View File

@ -1,21 +0,0 @@
package utils
import (
"encoding/json"
)
// JSONExecutor provides the minimal API for basic JSON engine functionality
type JSONExecutor interface {
Marshal(interface{}) ([]byte, error)
}
// DefaultJSONExecutor is a blank structure, in place to satisfy the API
// of a JSONExecutor
type DefaultJSONExecutor struct {
}
// Marshal takes in an arbitrary interface and returns an encoding of
// the provided interface
func (d *DefaultJSONExecutor) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}

5
utils/json_marshal.go Normal file
View File

@ -0,0 +1,5 @@
package utils
// JSONMarshal is the standard definition of representing a Go structure in
// json format
type JSONMarshal func(interface{}) ([]byte, error)

View File

@ -1,10 +1,11 @@
package utils
import (
"encoding/json"
"testing"
)
func TestDefaultJSONExecutor(t *testing.T) {
func TestDefaultJSONEncoder(t *testing.T) {
type SampleStructure struct {
ImportantString string `json:"important_string"`
}
@ -14,11 +15,11 @@ func TestDefaultJSONExecutor(t *testing.T) {
ImportantString: "Hello World",
}
importantString = `{"important_string":"Hello World"}`
jsonEncoder JSONMarshal = json.Marshal
)
jsonExecutor := DefaultJSONExecutor{}
raw, err := jsonExecutor.Marshal(sampleStructure)
raw, err := jsonEncoder(sampleStructure)
AssertEqual(t, err, nil)
AssertEqual(t, string(raw), importantString)