From 5941c8c4e2d80c9d99668868c2a2bce901839a25 Mon Sep 17 00:00:00 2001 From: Yoofi Quansah Date: Sat, 23 Jan 2021 02:15:59 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20chore:=20address=20comments,=20a?= =?UTF-8?q?nd=20use=20more=20standard=20definition=20for=20functionality?= =?UTF-8?q?=20we=20define?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.go | 13 ++++++------ ctx.go | 2 +- utils/json_executor.go | 21 ------------------- utils/json_marshal.go | 5 +++++ ..._executor_test.go => json_marshal_test.go} | 9 ++++---- 5 files changed, 18 insertions(+), 32 deletions(-) delete mode 100644 utils/json_executor.go create mode 100644 utils/json_marshal.go rename utils/{json_executor_test.go => json_marshal_test.go} (70%) diff --git a/app.go b/app.go index 9f946b57bb..4c039cfe01 100644 --- a/app.go +++ b/app.go @@ -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 diff --git a/ctx.go b/ctx.go index 57b7996af2..acb1492aa7 100644 --- a/ctx.go +++ b/ctx.go @@ -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 } diff --git a/utils/json_executor.go b/utils/json_executor.go deleted file mode 100644 index 9fd7c5b7d9..0000000000 --- a/utils/json_executor.go +++ /dev/null @@ -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) -} diff --git a/utils/json_marshal.go b/utils/json_marshal.go new file mode 100644 index 0000000000..692b49a4b7 --- /dev/null +++ b/utils/json_marshal.go @@ -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) diff --git a/utils/json_executor_test.go b/utils/json_marshal_test.go similarity index 70% rename from utils/json_executor_test.go rename to utils/json_marshal_test.go index b0ea40a5be..08501d9621 100644 --- a/utils/json_executor_test.go +++ b/utils/json_marshal_test.go @@ -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"}` - ) - jsonExecutor := DefaultJSONExecutor{} + jsonEncoder JSONMarshal = json.Marshal + ) - raw, err := jsonExecutor.Marshal(sampleStructure) + raw, err := jsonEncoder(sampleStructure) AssertEqual(t, err, nil) AssertEqual(t, string(raw), importantString)