Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type (
binder struct{}
)

//Bind decode req.Body or form-value to struct
// Bind decode req.Body or form-value to struct
func (b *binder) Bind(i interface{}, ctx Context) (err error) {
req := ctx.Request()
ctype := req.Header.Get(HeaderContentType)
Expand All @@ -38,22 +38,22 @@ func (b *binder) Bind(i interface{}, ctx Context) (err error) {
err = json.Unmarshal(ctx.Request().PostBody(), i)
case strings.HasPrefix(ctype, MIMEApplicationXML):
err = xml.Unmarshal(ctx.Request().PostBody(), i)
//case strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm),
// strings.HasPrefix(ctype, MIMETextHTML):
// err = reflects.ConvertMapToStruct(defaultTagName, i, ctx.FormValues())
// case strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm),
// strings.HasPrefix(ctype, MIMETextHTML):
// err = reflects.ConvertMapToStruct(defaultTagName, i, ctx.FormValues())
default:
//check is use json tag, fixed for issue #91
// check is use json tag, fixed for issue #91
tagName := defaultTagName
if ctx.HttpServer().ServerConfig().EnabledBindUseJsonTag {
tagName = jsonTagName
}
//no check content type for fixed issue #6
// no check content type for fixed issue #6
err = reflects.ConvertMapToStruct(tagName, i, ctx.Request().FormValues())
}
return err
}

//BindJsonBody default use json decode req.Body to struct
// BindJsonBody default use json decode req.Body to struct
func (b *binder) BindJsonBody(i interface{}, ctx Context) (err error) {
if ctx.Request().PostBody() == nil {
err = errors.New("request body can't be empty")
Expand Down
96 changes: 48 additions & 48 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Person struct {
Legs []string
}

//json
// json
func TestBinder_Bind_json(t *testing.T) {

binder := newBinder()
Expand All @@ -22,48 +22,48 @@ func TestBinder_Bind_json(t *testing.T) {
t.Error("binder can not be nil!")
}

//init DotServer
// init DotServer
app := New()

if app == nil {
t.Error("app can not be nil!")
}

//expected
// expected
expected := &Person{
Hair: "Brown",
HasGlass: true,
Age: 10,
Legs: []string{"Left", "Right"},
}

//init param
// init param
param := &InitContextParam{
t,
expected,
"application/json",
test.ToJson,
}

//init param
// init param
context := initContext(param)
//actual
// actual
person := &Person{}

err := binder.Bind(person, context)

//check error must nil
// check error must nil
test.Nil(t, err)

//check expected
// check expected
test.Equal(t, expected, person)

t.Log("person:", person)
t.Log("expected:", expected)

}

//json
// json
func TestBinder_Bind_json_error(t *testing.T) {

binder := newBinder()
Expand All @@ -72,41 +72,41 @@ func TestBinder_Bind_json_error(t *testing.T) {
t.Error("binder can not be nil!")
}

//init DotServer
// init DotServer
app := New()

if app == nil {
t.Error("app can not be nil!")
}

//expected
// expected
expected := &Person{
Hair: "Brown",
HasGlass: true,
Age: 10,
Legs: []string{"Left", "Right"},
}

//init param
// init param
param := &InitContextParam{
t,
expected,
"application/xml",
test.ToJson,
}

//init param
// init param
context := initContext(param)
//actual
// actual
person := &Person{}

err := binder.Bind(person, context)

//check error must not nil
// check error must not nil
test.NotNil(t, err)
}

//xml
// xml
func TestBinder_Bind_xml(t *testing.T) {

binder := newBinder()
Expand All @@ -115,14 +115,14 @@ func TestBinder_Bind_xml(t *testing.T) {
t.Error("binder can not be nil!")
}

//init DotServer
// init DotServer
app := New()

if app == nil {
t.Error("app can not be nil!")
}

//expected
// expected
expected := &Person{
Hair: "Brown",
HasGlass: true,
Expand All @@ -136,25 +136,25 @@ func TestBinder_Bind_xml(t *testing.T) {
test.ToXML,
}

//init param
// init param
context := initContext(param)
//actual
// actual
person := &Person{}

err := binder.Bind(person, context)

//check error must nil
// check error must nil
test.Nil(t, err)

//check expected
// check expected
test.Equal(t, expected, person)

t.Log("person:", person)
t.Log("expected:", expected)

}

//xml
// xml
func TestBinder_Bind_xml_error(t *testing.T) {

binder := newBinder()
Expand All @@ -163,14 +163,14 @@ func TestBinder_Bind_xml_error(t *testing.T) {
t.Error("binder can not be nil!")
}

//init DotServer
// init DotServer
app := New()

if app == nil {
t.Error("app can not be nil!")
}

//expected
// expected
expected := &Person{
Hair: "Brown",
HasGlass: true,
Expand All @@ -184,18 +184,18 @@ func TestBinder_Bind_xml_error(t *testing.T) {
test.ToXML,
}

//init param
// init param
context := initContext(param)
//actual
// actual
person := &Person{}

err := binder.Bind(person, context)

//check error must not nil
// check error must not nil
test.NotNil(t, err)
}

//else
// else
func TestBinder_Bind_default(t *testing.T) {

binder := newBinder()
Expand All @@ -204,14 +204,14 @@ func TestBinder_Bind_default(t *testing.T) {
t.Error("binder can not be nil!")
}

//init DotServer
// init DotServer
app := New()

if app == nil {
t.Error("app can not be nil!")
}

//expected
// expected
expected := &Person{
Hair: "Brown",
HasGlass: true,
Expand All @@ -225,7 +225,7 @@ func TestBinder_Bind_default(t *testing.T) {
test.ToDefault,
}

//init param
// init param
context := initContext(param)

form := make(map[string][]string)
Expand All @@ -235,23 +235,23 @@ func TestBinder_Bind_default(t *testing.T) {
form["Legs"] = []string{"Left", "Right"}

context.request.Form = form
//actual
// actual
person := &Person{}

err := binder.Bind(person, context)

//check error must nil
// check error must nil
test.Nil(t, err)

//check expected
// check expected
test.Equal(t, expected, person)

t.Log("person:", person)
t.Log("expected:", expected)

}

//else
// else
func TestBinder_Bind_default_error(t *testing.T) {

binder := newBinder()
Expand All @@ -260,14 +260,14 @@ func TestBinder_Bind_default_error(t *testing.T) {
t.Error("binder can not be nil!")
}

//init DotServer
// init DotServer
app := New()

if app == nil {
t.Error("app can not be nil!")
}

//expected
// expected
expected := &Person{
Hair: "Brown",
HasGlass: true,
Expand All @@ -281,7 +281,7 @@ func TestBinder_Bind_default_error(t *testing.T) {
test.ToDefault,
}

//init param
// init param
context := initContext(param)

form := make(map[string][]string)
Expand All @@ -291,18 +291,18 @@ func TestBinder_Bind_default_error(t *testing.T) {
form["Legs"] = []string{"Left", "Right"}

context.request.Form = form
//actual
// actual
person := &Person{}

err := binder.Bind(person, context)

//check error must not nil
// check error must not nil
test.NotNil(t, err)

}

//default
//TODO:content type is null but body not null,is it right??
// default
// TODO:content type is null but body not null,is it right??
func TestBinder_Bind_ContentTypeNull(t *testing.T) {

binder := newBinder()
Expand All @@ -311,14 +311,14 @@ func TestBinder_Bind_ContentTypeNull(t *testing.T) {
t.Error("binder can not be nil!")
}

//init DotServer
// init DotServer
app := New()

if app == nil {
t.Error("app can not be nil!")
}

//expected
// expected
expected := &Person{
Hair: "Brown",
HasGlass: true,
Expand All @@ -332,13 +332,13 @@ func TestBinder_Bind_ContentTypeNull(t *testing.T) {
test.ToXML,
}

//init param
// init param
context := initContext(param)
//actual
// actual
person := &Person{}

err := binder.Bind(person, context)

//check error must nil?
// check error must nil?
test.Nil(t, err)
}
2 changes: 1 addition & 1 deletion cache/redis/cache_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var (
// RedisCache is redis cache adapter.
// it contains serverIp for redis conn.
type RedisCache struct {
serverURL string //connection string, like "redis://:password@10.0.1.11:6379/0"
serverURL string // connection string, like "redis://:password@10.0.1.11:6379/0"
}

// NewRedisCache returns a new *RedisCache.
Expand Down
Loading