Skip to content

Commit

Permalink
Linting and optimizing struct memory signature. (gin-gonic#1184)
Browse files Browse the repository at this point in the history
* fix cleanPath spell (gin-gonic#969)

* linter and optimize structs
  • Loading branch information
BorisBorshevsky authored and yi.d.liu committed Dec 18, 2017
1 parent 3645c51 commit 491d958
Show file tree
Hide file tree
Showing 11 changed files with 1,416 additions and 59 deletions.
1,402 changes: 1,390 additions & 12 deletions README.md

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ func BenchmarkOneRouteJSON(B *testing.B) {
runRequest(B, router, "GET", "/json")
}

var htmlContentType = []string{"text/html; charset=utf-8"}

func BenchmarkOneRouteHTML(B *testing.B) {
router := New()
t := template.Must(template.New("index").Parse(`
Expand Down
9 changes: 0 additions & 9 deletions binding/form_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,3 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
value.Set(reflect.ValueOf(t))
return nil
}

// Don't pass in pointers to bind to. Can lead to bugs. See:
// https://github.com/codegangsta/martini-contrib/issues/40
// https://github.com/codegangsta/martini-contrib/pull/34#issuecomment-29683659
func ensureNotPointer(obj interface{}) {
if reflect.TypeOf(obj).Kind() == reflect.Ptr {
panic("Pointers are not accepted as binding models")
}
}
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (a errorMsgs) String() string {
}
var buffer bytes.Buffer
for i, msg := range a {
fmt.Fprintf(&buffer, "Error #%02d: %s\n", (i + 1), msg.Err)
fmt.Fprintf(&buffer, "Error #%02d: %s\n", i+1, msg.Err)
if msg.Meta != nil {
fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-validation/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
validator "gopkg.in/go-playground/validator.v8"
"gopkg.in/go-playground/validator.v8"
)

type Booking struct {
Expand Down
4 changes: 2 additions & 2 deletions examples/realtime-advanced/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func statsWorker() {
"timestamp": uint64(time.Now().Unix()),
"HeapInuse": stats.HeapInuse,
"StackInuse": stats.StackInuse,
"Mallocs": (stats.Mallocs - lastMallocs),
"Frees": (stats.Frees - lastFrees),
"Mallocs": stats.Mallocs - lastMallocs,
"Frees": stats.Frees - lastFrees,
"Inbound": uint64(messages.Get("inbound")),
"Outbound": uint64(messages.Get("outbound")),
"Connected": connectedUsers(),
Expand Down
21 changes: 11 additions & 10 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ type RoutesInfo []RouteInfo
// Create an instance of Engine, by using New() or Default()
type Engine struct {
RouterGroup
delims render.Delims
secureJsonPrefix string
HTMLRender render.HTMLRender
FuncMap template.FuncMap
allNoRoute HandlersChain
allNoMethod HandlersChain
noRoute HandlersChain
noMethod HandlersChain
pool sync.Pool
trees methodTrees

// Enables automatic redirection if the current route can't be matched but a
// handler for the path with (without) the trailing slash exists.
Expand Down Expand Up @@ -102,6 +92,17 @@ type Engine struct {
// Value of 'maxMemory' param that is given to http.Request's ParseMultipartForm
// method call.
MaxMultipartMemory int64

delims render.Delims
secureJsonPrefix string
HTMLRender render.HTMLRender
FuncMap template.FuncMap
allNoRoute HandlersChain
allNoMethod HandlersChain
noRoute HandlersChain
noMethod HandlersChain
pool sync.Pool
trees methodTrees
}

var _ IRouter = &Engine{}
Expand Down
2 changes: 1 addition & 1 deletion gin_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestUnixSocket(t *testing.T) {
c, err := net.Dial("unix", "/tmp/unix_unit_test")
assert.NoError(t, err)

fmt.Fprintf(c, "GET /example HTTP/1.0\r\n\r\n")
fmt.Fprint(c, "GET /example HTTP/1.0\r\n\r\n")
scanner := bufio.NewScanner(c)
var response string
for scanner.Scan() {
Expand Down
6 changes: 3 additions & 3 deletions mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
const ENV_GIN_MODE = "GIN_MODE"

const (
DebugMode string = "debug"
ReleaseMode string = "release"
TestMode string = "test"
DebugMode = "debug"
ReleaseMode = "release"
TestMode = "test"
)
const (
debugCode = iota
Expand Down
14 changes: 7 additions & 7 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ const (

type node struct {
path string
wildChild bool
nType nodeType
maxParams uint8
indices string
children []*node
handlers HandlersChain
priority uint32
nType nodeType
maxParams uint8
wildChild bool
}

// increments priority of the given child and reorders if necessary.
Expand Down Expand Up @@ -384,7 +384,7 @@ walk: // Outer loop for walking the tree
// Nothing found.
// We can recommend to redirect to the same URL without a
// trailing slash if a leaf exists for that path.
tsr = (path == "/" && n.handlers != nil)
tsr = path == "/" && n.handlers != nil
return
}

Expand Down Expand Up @@ -424,7 +424,7 @@ walk: // Outer loop for walking the tree
}

// ... but we can't
tsr = (len(path) == end+1)
tsr = len(path) == end+1
return
}

Expand All @@ -435,7 +435,7 @@ walk: // Outer loop for walking the tree
// No handle found. Check if a handle for this path + a
// trailing slash exists for TSR recommendation
n = n.children[0]
tsr = (n.path == "/" && n.handlers != nil)
tsr = n.path == "/" && n.handlers != nil
}

return
Expand Down Expand Up @@ -530,7 +530,7 @@ func (n *node) findCaseInsensitivePath(path string, fixTrailingSlash bool) (ciPa

// Nothing found. We can recommend to redirect to the same URL
// without a trailing slash if a leaf exists for that path
found = (fixTrailingSlash && path == "/" && n.handlers != nil)
found = fixTrailingSlash && path == "/" && n.handlers != nil
return
}

Expand Down
11 changes: 0 additions & 11 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,11 @@
package gin

import (
"fmt"
"reflect"
"strings"
"testing"
)

func printChildren(n *node, prefix string) {
fmt.Printf(" %02d:%02d %s%s[%d] %v %t %d \r\n", n.priority, n.maxParams, prefix, n.path, len(n.children), n.handlers, n.wildChild, n.nType)
for l := len(n.path); l > 0; l-- {
prefix += " "
}
for _, child := range n.children {
printChildren(child, prefix)
}
}

// Used as a workaround since we can't compare functions or their addressses
var fakeHandlerValue string

Expand Down

0 comments on commit 491d958

Please sign in to comment.