Skip to content

Commit

Permalink
Push branch develop into master
Browse files Browse the repository at this point in the history
  • Loading branch information
javierprovecho committed Apr 14, 2016
2 parents 7d0b329 + 3c3526f commit 5caaac4
Show file tree
Hide file tree
Showing 22 changed files with 135 additions and 91 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<img align="right" src="https://raw.githubusercontent.com/gin-gonic/gin/master/logo.jpg">
[![Build Status](https://travis-ci.org/gin-gonic/gin.svg)](https://travis-ci.org/gin-gonic/gin)
[![Coverage Status](https://coveralls.io/repos/gin-gonic/gin/badge.svg?branch=master)](https://coveralls.io/r/gin-gonic/gin?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/gin-gonic/gin)](https://goreportcard.com/report/github.com/gin-gonic/gin)
[![GoDoc](https://godoc.org/github.com/gin-gonic/gin?status.svg)](https://godoc.org/github.com/gin-gonic/gin)
[![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Expand Down
3 changes: 2 additions & 1 deletion benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gin
import (
"html/template"
"net/http"
"os"
"testing"
)

Expand Down Expand Up @@ -36,7 +37,7 @@ func BenchmarkManyHandlers(B *testing.B) {
}

func Benchmark5Params(B *testing.B) {
DefaultWriter = newMockWriter()
DefaultWriter = os.Stdout
router := New()
router.Use(func(c *Context) {})
router.GET("/param/:param1/:params2/:param3/:param4/:param5", func(c *Context) {})
Expand Down
4 changes: 2 additions & 2 deletions binding/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (

type protobufBinding struct{}

func (_ protobufBinding) Name() string {
func (protobufBinding) Name() string {
return "protobuf"
}

func (_ protobufBinding) Bind(req *http.Request, obj interface{}) error {
func (protobufBinding) Bind(req *http.Request, obj interface{}) error {

buf, err := ioutil.ReadAll(req.Body)
if err != nil {
Expand Down
52 changes: 26 additions & 26 deletions binding/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ type testInterface interface {
String() string
}

type substruct_noValidation struct {
I_String string
I_Int int
type substructNoValidation struct {
IString string
IInt int
}

type mapNoValidationSub map[string]substruct_noValidation
type mapNoValidationSub map[string]substructNoValidation

type struct_noValidation_values struct {
substruct_noValidation
type structNoValidationValues struct {
substructNoValidation

Boolean bool

Expand All @@ -46,16 +46,16 @@ type struct_noValidation_values struct {

Date time.Time

Struct substruct_noValidation
Struct substructNoValidation
InlinedStruct struct {
String []string
Integer int
}

IntSlice []int
IntPointerSlice []*int
StructPointerSlice []*substruct_noValidation
StructSlice []substruct_noValidation
StructPointerSlice []*substructNoValidation
StructSlice []substructNoValidation
InterfaceSlice []testInterface

UniversalInterface interface{}
Expand All @@ -65,9 +65,9 @@ type struct_noValidation_values struct {
StructMap mapNoValidationSub
}

func createNoValidation_values() struct_noValidation_values {
func createNoValidationValues() structNoValidationValues {
integer := 1
s := struct_noValidation_values{
s := structNoValidationValues{
Boolean: true,
Uinteger: 1 << 29,
Integer: -10000,
Expand All @@ -84,33 +84,33 @@ func createNoValidation_values() struct_noValidation_values {
String: "text",
Date: time.Time{},
CustomInterface: &bytes.Buffer{},
Struct: substruct_noValidation{},
Struct: substructNoValidation{},
IntSlice: []int{-3, -2, 1, 0, 1, 2, 3},
IntPointerSlice: []*int{&integer},
StructSlice: []substruct_noValidation{},
StructSlice: []substructNoValidation{},
UniversalInterface: 1.2,
FloatMap: map[string]float32{
"foo": 1.23,
"bar": 232.323,
},
StructMap: mapNoValidationSub{
"foo": substruct_noValidation{},
"bar": substruct_noValidation{},
"foo": substructNoValidation{},
"bar": substructNoValidation{},
},
// StructPointerSlice []noValidationSub
// InterfaceSlice []testInterface
}
s.InlinedStruct.Integer = 1000
s.InlinedStruct.String = []string{"first", "second"}
s.I_String = "substring"
s.I_Int = 987654
s.IString = "substring"
s.IInt = 987654
return s
}

func TestValidateNoValidationValues(t *testing.T) {
origin := createNoValidation_values()
test := createNoValidation_values()
empty := struct_noValidation_values{}
origin := createNoValidationValues()
test := createNoValidationValues()
empty := structNoValidationValues{}

assert.Nil(t, validate(test))
assert.Nil(t, validate(&test))
Expand All @@ -120,8 +120,8 @@ func TestValidateNoValidationValues(t *testing.T) {
assert.Equal(t, origin, test)
}

type struct_noValidation_pointer struct {
substruct_noValidation
type structNoValidationPointer struct {
substructNoValidation

Boolean bool

Expand All @@ -143,12 +143,12 @@ type struct_noValidation_pointer struct {

Date *time.Time

Struct *substruct_noValidation
Struct *substructNoValidation

IntSlice *[]int
IntPointerSlice *[]*int
StructPointerSlice *[]*substruct_noValidation
StructSlice *[]substruct_noValidation
StructPointerSlice *[]*substructNoValidation
StructSlice *[]substructNoValidation
InterfaceSlice *[]testInterface

FloatMap *map[string]float32
Expand All @@ -158,7 +158,7 @@ type struct_noValidation_pointer struct {
func TestValidateNoValidationPointers(t *testing.T) {
//origin := createNoValidation_values()
//test := createNoValidation_values()
empty := struct_noValidation_pointer{}
empty := structNoValidationPointer{}

//assert.Nil(t, validate(test))
//assert.Nil(t, validate(&test))
Expand Down
12 changes: 9 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *Context) reset() {
// Copy returns a copy of the current context that can be safely used outside the request's scope.
// This have to be used then the context has to be passed to a goroutine.
func (c *Context) Copy() *Context {
var cp Context = *c
var cp = *c
cp.writermem.ResponseWriter = nil
cp.Writer = &cp.writermem
cp.index = abortIndex
Expand Down Expand Up @@ -115,6 +115,7 @@ func (c *Context) Abort() {
// For example, a failed attempt to authentificate a request could use: context.AbortWithStatus(401).
func (c *Context) AbortWithStatus(code int) {
c.Status(code)
c.Writer.WriteHeaderNow()
c.Abort()
}

Expand Down Expand Up @@ -171,7 +172,7 @@ func (c *Context) Get(key string) (value interface{}, exists bool) {
return
}

// Returns the value for the given key if it exists, otherwise it panics.
// MustGet returns the value for the given key if it exists, otherwise it panics.
func (c *Context) MustGet(key string) interface{} {
if value, exists := c.Get(key); exists {
return value
Expand Down Expand Up @@ -243,7 +244,7 @@ func (c *Context) PostForm(key string) string {
return value
}

// PostForm returns the specified key from a POST urlencoded form or multipart form
// DefaultPostForm returns the specified key from a POST urlencoded form or multipart form
// when it exists, otherwise it returns the specified defaultValue string.
// See: PostForm() and GetPostForm() for further information.
func (c *Context) DefaultPostForm(key, defaultValue string) string {
Expand Down Expand Up @@ -426,6 +427,11 @@ func (c *Context) XML(code int, obj interface{}) {
c.Render(code, render.XML{Data: obj})
}

// YAML serializes the given struct as YAML into the response body.
func (c *Context) YAML(code int, obj interface{}) {
c.Render(code, render.YAML{Data: obj})
}

// String writes the given string into the response body.
func (c *Context) String(code int, format string, values ...interface{}) {
c.Status(code)
Expand Down
17 changes: 13 additions & 4 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,14 @@ func TestContextPostFormMultipart(t *testing.T) {
Bar string `form:"bar"`
BarAsInt int `form:"bar"`
Array []string `form:"array"`
Id string `form:"id"`
ID string `form:"id"`
}
assert.NoError(t, c.Bind(&obj))
assert.Equal(t, obj.Foo, "bar")
assert.Equal(t, obj.Bar, "10")
assert.Equal(t, obj.BarAsInt, 10)
assert.Equal(t, obj.Array, []string{"first", "second"})
assert.Equal(t, obj.Id, "")
assert.Equal(t, obj.ID, "")

value, ok := c.GetQuery("foo")
assert.False(t, ok)
Expand Down Expand Up @@ -433,6 +433,17 @@ func TestContextRenderFile(t *testing.T) {
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")
}

// TestContextRenderYAML tests that the response is serialized as YAML
// and Content-Type is set to application/x-yaml
func TestContextRenderYAML(t *testing.T) {
c, w, _ := CreateTestContext()
c.YAML(201, H{"foo": "bar"})

assert.Equal(t, w.Code, 201)
assert.Equal(t, w.Body.String(), "foo: bar\n")
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/x-yaml; charset=utf-8")
}

func TestContextHeaders(t *testing.T) {
c, _, _ := CreateTestContext()
c.Header("Content-Type", "text/plain")
Expand Down Expand Up @@ -545,7 +556,6 @@ func TestContextAbortWithStatus(t *testing.T) {
c, w, _ := CreateTestContext()
c.index = 4
c.AbortWithStatus(401)
c.Writer.WriteHeaderNow()

assert.Equal(t, c.index, abortIndex)
assert.Equal(t, c.Writer.Status(), 401)
Expand Down Expand Up @@ -596,7 +606,6 @@ func TestContextTypedError(t *testing.T) {
func TestContextAbortWithError(t *testing.T) {
c, w, _ := CreateTestContext()
c.AbortWithError(401, errors.New("bad input")).SetMeta("some input")
c.Writer.WriteHeaderNow()

assert.Equal(t, w.Code, 401)
assert.Equal(t, c.index, abortIndex)
Expand Down
4 changes: 2 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (msg *Error) JSON() interface{} {
return json
}

// Implements the json.Marshaller interface
// MarshalJSON implements the json.Marshaller interface
func (msg *Error) MarshalJSON() ([]byte, error) {
return json.Marshal(msg.JSON())
}
Expand All @@ -89,7 +89,7 @@ func (a errorMsgs) ByType(typ ErrorType) errorMsgs {
if typ == ErrorTypeAny {
return a
}
var result errorMsgs = nil
var result errorMsgs
for _, msg := range a {
if msg.IsType(typ) {
result = append(result, msg)
Expand Down
6 changes: 3 additions & 3 deletions examples/realtime-advanced/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var savedStats map[string]uint64

func statsWorker() {
c := time.Tick(1 * time.Second)
var lastMallocs uint64 = 0
var lastFrees uint64 = 0
for _ = range c {
var lastMallocs uint64
var lastFrees uint64
for range c {
var stats runtime.MemStats
runtime.ReadMemStats(&stats)

Expand Down
8 changes: 4 additions & 4 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/gin-gonic/gin/render"
)

// Framework's version
// Version is Framework's version
const Version = "v1.0rc2"

var default404Body = []byte("404 page not found")
Expand Down Expand Up @@ -147,19 +147,19 @@ func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
engine.HTMLRender = render.HTMLProduction{Template: templ}
}

// Adds handlers for NoRoute. It return a 404 code by default.
// NoRoute adds handlers for NoRoute. It return a 404 code by default.
func (engine *Engine) NoRoute(handlers ...HandlerFunc) {
engine.noRoute = handlers
engine.rebuild404Handlers()
}

// Sets the handlers called when... TODO
// NoMethod sets the handlers called when... TODO
func (engine *Engine) NoMethod(handlers ...HandlerFunc) {
engine.noMethod = handlers
engine.rebuild405Handlers()
}

// Attachs a global middleware to the router. ie. the middleware attached though Use() will be
// Use attachs a global middleware to the router. ie. the middleware attached though Use() will be
// included in the handlers chain for every single request. Even 404, 405, static files...
// For example, this is the right place for a logger or error management middleware.
func (engine *Engine) Use(middleware ...HandlerFunc) IRoutes {
Expand Down
14 changes: 7 additions & 7 deletions ginS/gins.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ func SetHTMLTemplate(templ *template.Template) {
engine().SetHTMLTemplate(templ)
}

// Adds handlers for NoRoute. It return a 404 code by default.
// NoRoute adds handlers for NoRoute. It return a 404 code by default.
func NoRoute(handlers ...HandlerFunc) {
engine().NoRoute(handlers...)
}

// Sets the handlers called when... TODO
// NoMethod sets the handlers called when... TODO
func NoMethod(handlers ...HandlerFunc) {
engine().NoMethod(handlers...)
}

// Creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
// Group creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
// For example, all the routes that use a common middlware for authorization could be grouped.
func Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {
return engine().Group(relativePath, handlers...)
Expand Down Expand Up @@ -111,28 +111,28 @@ func StaticFS(relativePath string, fs http.FileSystem) IRoutes {
return engine().StaticFS(relativePath, fs)
}

// Attachs a global middleware to the router. ie. the middlewares attached though Use() will be
// Use attachs a global middleware to the router. ie. the middlewares attached though Use() will be
// included in the handlers chain for every single request. Even 404, 405, static files...
// For example, this is the right place for a logger or error management middleware.
func Use(middlewares ...HandlerFunc) IRoutes {
return engine().Use(middlewares...)
}

// The router is attached to a http.Server and starts listening and serving HTTP requests.
// Run : The router is attached to a http.Server and starts listening and serving HTTP requests.
// It is a shortcut for http.ListenAndServe(addr, router)
// Note: this method will block the calling goroutine undefinitelly unless an error happens.
func Run(addr ...string) (err error) {
return engine().Run(addr...)
}

// The router is attached to a http.Server and starts listening and serving HTTPS requests.
// RunTLS : The router is attached to a http.Server and starts listening and serving HTTPS requests.
// It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
// Note: this method will block the calling goroutine undefinitelly unless an error happens.
func RunTLS(addr string, certFile string, keyFile string) (err error) {
return engine().RunTLS(addr, certFile, keyFile)
}

// The router is attached to a http.Server and starts listening and serving HTTP requests
// RunUnix : The router is attached to a http.Server and starts listening and serving HTTP requests
// through the specified unix socket (ie. a file)
// Note: this method will block the calling goroutine undefinitelly unless an error happens.
func RunUnix(file string) (err error) {
Expand Down
Loading

0 comments on commit 5caaac4

Please sign in to comment.