Skip to content

Commit

Permalink
🧹 chore: address comments, and use more standard definition for
Browse files Browse the repository at this point in the history
functionality we define
  • Loading branch information
yquansah committed Jan 25, 2021
1 parent 2267f0f commit 5941c8c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 32 deletions.
13 changes: 7 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package fiber
import (
"bufio"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
21 changes: 0 additions & 21 deletions utils/json_executor.go

This file was deleted.

5 changes: 5 additions & 0 deletions utils/json_marshal.go
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 5 additions & 4 deletions utils/json_executor_test.go → utils/json_marshal_test.go
Original file line number Diff line number Diff line change
@@ -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"`
}
Expand All @@ -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)
Expand Down

0 comments on commit 5941c8c

Please sign in to comment.