Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
1b8f05ce
Commit
1b8f05ce
authored
Apr 30, 2017
by
Eyal Post
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
golint fixes
parent
cfb2f68d
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
27 additions
and
13 deletions
+27
-13
conv.go
context/param/conv.go
+8
-8
methodparams.go
context/param/methodparams.go
+3
-1
options.go
context/param/options.go
+6
-0
renderer.go
context/response/renderer.go
+3
-0
responses.go
context/response/responses.go
+6
-3
router.go
router.go
+1
-1
No files found.
context/param/conv.go
View file @
1b8f05ce
...
...
@@ -8,6 +8,7 @@ import (
"github.com/astaxie/beego/logs"
)
// ConvertParams converts http method params to values that will be passed to the method controller as arguments
func
ConvertParams
(
methodParams
[]
*
MethodParam
,
methodType
reflect
.
Type
,
ctx
*
beecontext
.
Context
)
(
result
[]
reflect
.
Value
)
{
result
=
make
([]
reflect
.
Value
,
0
,
len
(
methodParams
))
for
i
:=
0
;
i
<
len
(
methodParams
);
i
++
{
...
...
@@ -55,15 +56,14 @@ func getParamValue(param *MethodParam, ctx *beecontext.Context) string {
func
parseValue
(
param
*
MethodParam
,
paramValue
string
,
paramType
reflect
.
Type
)
(
result
reflect
.
Value
,
err
error
)
{
if
paramValue
==
""
{
return
reflect
.
Zero
(
paramType
),
nil
}
else
{
parser
:=
getParser
(
param
,
paramType
)
value
,
err
:=
parser
.
parse
(
paramValue
,
paramType
)
if
err
!=
nil
{
return
result
,
err
}
return
safeConvert
(
reflect
.
ValueOf
(
value
),
paramType
)
}
parser
:=
getParser
(
param
,
paramType
)
value
,
err
:=
parser
.
parse
(
paramValue
,
paramType
)
if
err
!=
nil
{
return
result
,
err
}
return
safeConvert
(
reflect
.
ValueOf
(
value
),
paramType
)
}
func
safeConvert
(
value
reflect
.
Value
,
t
reflect
.
Type
)
(
result
reflect
.
Value
,
err
error
)
{
...
...
context/param/methodparams.go
View file @
1b8f05ce
...
...
@@ -5,7 +5,7 @@ import (
"strings"
)
//
K
eeps param information to be auto passed to controller methods
//
MethodParam k
eeps param information to be auto passed to controller methods
type
MethodParam
struct
{
name
string
location
paramLocation
...
...
@@ -22,6 +22,7 @@ const (
header
)
//New creates a new MethodParam with name and specific options
func
New
(
name
string
,
opts
...
MethodParamOption
)
*
MethodParam
{
return
newParam
(
name
,
nil
,
opts
)
}
...
...
@@ -34,6 +35,7 @@ func newParam(name string, parser paramParser, opts []MethodParamOption) (param
return
}
//Make creates an array of MethodParmas or an empty array
func
Make
(
list
...*
MethodParam
)
[]
*
MethodParam
{
if
len
(
list
)
>
0
{
return
list
...
...
context/param/options.go
View file @
1b8f05ce
...
...
@@ -4,24 +4,30 @@ import (
"fmt"
)
// MethodParamOption defines a func which apply options on a MethodParam
type
MethodParamOption
func
(
*
MethodParam
)
// IsRequired indicates that this param is required and can not be ommited from the http request
var
IsRequired
MethodParamOption
=
func
(
p
*
MethodParam
)
{
p
.
required
=
true
}
// InHeader indicates that this param is passed via an http header
var
InHeader
MethodParamOption
=
func
(
p
*
MethodParam
)
{
p
.
location
=
header
}
// InPath indicates that this param is part of the URL path
var
InPath
MethodParamOption
=
func
(
p
*
MethodParam
)
{
p
.
location
=
path
}
// InBody indicates that this param is passed as an http request body
var
InBody
MethodParamOption
=
func
(
p
*
MethodParam
)
{
p
.
location
=
body
}
// Default provides a default value for the http param
func
Default
(
defValue
interface
{})
MethodParamOption
{
return
func
(
p
*
MethodParam
)
{
if
defValue
!=
nil
{
...
...
context/response/renderer.go
View file @
1b8f05ce
...
...
@@ -6,6 +6,7 @@ import (
beecontext
"github.com/astaxie/beego/context"
)
// Renderer defines an http response renderer
type
Renderer
interface
{
Render
(
ctx
*
beecontext
.
Context
)
}
...
...
@@ -16,12 +17,14 @@ func (f rendererFunc) Render(ctx *beecontext.Context) {
f
(
ctx
)
}
// StatusCode sets the http response status code
type
StatusCode
int
func
(
s
StatusCode
)
Error
()
string
{
return
strconv
.
Itoa
(
int
(
s
))
}
// Render sets the http status code
func
(
s
StatusCode
)
Render
(
ctx
*
beecontext
.
Context
)
{
ctx
.
Output
.
SetStatus
(
int
(
s
))
}
...
...
context/response/responses.go
View file @
1b8f05ce
...
...
@@ -4,7 +4,8 @@ import (
beecontext
"github.com/astaxie/beego/context"
)
func
Json
(
value
interface
{},
encoding
...
bool
)
Renderer
{
// JSON renders value to the response as JSON
func
JSON
(
value
interface
{},
encoding
...
bool
)
Renderer
{
return
rendererFunc
(
func
(
ctx
*
beecontext
.
Context
)
{
var
(
hasIndent
=
true
...
...
@@ -28,12 +29,14 @@ func errorRenderer(err error) Renderer {
})
}
func
Redirect
(
localurl
string
)
statusCodeWithRender
{
// Redirect renders http 302 with a URL
func
Redirect
(
localurl
string
)
Renderer
{
return
statusCodeWithRender
{
302
,
func
(
ctx
*
beecontext
.
Context
)
{
ctx
.
Redirect
(
302
,
localurl
)
}}
}
// RenderMethodResult renders the return value of a controller method to the output
func
RenderMethodResult
(
result
interface
{},
ctx
*
beecontext
.
Context
)
{
if
result
!=
nil
{
renderer
,
ok
:=
result
.
(
Renderer
)
...
...
@@ -42,7 +45,7 @@ func RenderMethodResult(result interface{}, ctx *beecontext.Context) {
if
ok
{
renderer
=
errorRenderer
(
err
)
}
else
{
renderer
=
J
son
(
result
)
renderer
=
J
SON
(
result
)
}
}
renderer
.
Render
(
ctx
)
...
...
router.go
View file @
1b8f05ce
...
...
@@ -813,7 +813,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
default
:
if
!
execController
.
HandlerFunc
(
runMethod
)
{
method
:=
vc
.
MethodByName
(
runMethod
)
var
in
[]
reflect
.
Value
=
param
.
ConvertParams
(
methodParams
,
method
.
Type
(),
context
)
in
:
=
param
.
ConvertParams
(
methodParams
,
method
.
Type
(),
context
)
out
:=
method
.
Call
(
in
)
//For backward compatibility we only handle response if we had incoming methodParams
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment