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
1effb6ce
Commit
1effb6ce
authored
Oct 02, 2016
by
Denis Isaev
Committed by
Denis Isaev
Oct 02, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add GetUint(8|16|32|64) to controller
parent
2d87d4fe
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
0 deletions
+85
-0
controller.go
controller.go
+39
-0
controller_test.go
controller_test.go
+46
-0
No files found.
controller.go
View file @
1effb6ce
...
...
@@ -399,6 +399,16 @@ func (c *Controller) GetInt8(key string, def ...int8) (int8, error) {
return
int8
(
i64
),
err
}
// GetUint8 return input as an uint8 or the default value while it's present and input is blank
func
(
c
*
Controller
)
GetUint8
(
key
string
,
def
...
uint8
)
(
uint8
,
error
)
{
strv
:=
c
.
Ctx
.
Input
.
Query
(
key
)
if
len
(
strv
)
==
0
&&
len
(
def
)
>
0
{
return
def
[
0
],
nil
}
u64
,
err
:=
strconv
.
ParseUint
(
strv
,
10
,
8
)
return
uint8
(
u64
),
err
}
// GetInt16 returns input as an int16 or the default value while it's present and input is blank
func
(
c
*
Controller
)
GetInt16
(
key
string
,
def
...
int16
)
(
int16
,
error
)
{
strv
:=
c
.
Ctx
.
Input
.
Query
(
key
)
...
...
@@ -409,6 +419,16 @@ func (c *Controller) GetInt16(key string, def ...int16) (int16, error) {
return
int16
(
i64
),
err
}
// GetUint16 returns input as an uint16 or the default value while it's present and input is blank
func
(
c
*
Controller
)
GetUint16
(
key
string
,
def
...
uint16
)
(
uint16
,
error
)
{
strv
:=
c
.
Ctx
.
Input
.
Query
(
key
)
if
len
(
strv
)
==
0
&&
len
(
def
)
>
0
{
return
def
[
0
],
nil
}
u64
,
err
:=
strconv
.
ParseUint
(
strv
,
10
,
16
)
return
uint16
(
u64
),
err
}
// GetInt32 returns input as an int32 or the default value while it's present and input is blank
func
(
c
*
Controller
)
GetInt32
(
key
string
,
def
...
int32
)
(
int32
,
error
)
{
strv
:=
c
.
Ctx
.
Input
.
Query
(
key
)
...
...
@@ -419,6 +439,16 @@ func (c *Controller) GetInt32(key string, def ...int32) (int32, error) {
return
int32
(
i64
),
err
}
// GetUint32 returns input as an uint32 or the default value while it's present and input is blank
func
(
c
*
Controller
)
GetUint32
(
key
string
,
def
...
uint32
)
(
uint32
,
error
)
{
strv
:=
c
.
Ctx
.
Input
.
Query
(
key
)
if
len
(
strv
)
==
0
&&
len
(
def
)
>
0
{
return
def
[
0
],
nil
}
u64
,
err
:=
strconv
.
ParseUint
(
strv
,
10
,
32
)
return
uint32
(
u64
),
err
}
// GetInt64 returns input value as int64 or the default value while it's present and input is blank.
func
(
c
*
Controller
)
GetInt64
(
key
string
,
def
...
int64
)
(
int64
,
error
)
{
strv
:=
c
.
Ctx
.
Input
.
Query
(
key
)
...
...
@@ -428,6 +458,15 @@ func (c *Controller) GetInt64(key string, def ...int64) (int64, error) {
return
strconv
.
ParseInt
(
strv
,
10
,
64
)
}
// GetUint64 returns input value as uint64 or the default value while it's present and input is blank.
func
(
c
*
Controller
)
GetUint64
(
key
string
,
def
...
uint64
)
(
uint64
,
error
)
{
strv
:=
c
.
Ctx
.
Input
.
Query
(
key
)
if
len
(
strv
)
==
0
&&
len
(
def
)
>
0
{
return
def
[
0
],
nil
}
return
strconv
.
ParseUint
(
strv
,
10
,
64
)
}
// GetBool returns input value as bool or the default value while it's present and input is blank.
func
(
c
*
Controller
)
GetBool
(
key
string
,
def
...
bool
)
(
bool
,
error
)
{
strv
:=
c
.
Ctx
.
Input
.
Query
(
key
)
...
...
controller_test.go
View file @
1effb6ce
...
...
@@ -15,6 +15,8 @@
package
beego
import
(
"math"
"strconv"
"testing"
"github.com/astaxie/beego/context"
...
...
@@ -75,3 +77,47 @@ func TestGetInt64(t *testing.T) {
t
.
Errorf
(
"TestGeetInt64 expect 40,get %T,%v"
,
val
,
val
)
}
}
func
TestGetUint8
(
t
*
testing
.
T
)
{
i
:=
context
.
NewInput
()
i
.
SetParam
(
"age"
,
strconv
.
FormatUint
(
math
.
MaxUint8
,
10
))
ctx
:=
&
context
.
Context
{
Input
:
i
}
ctrlr
:=
Controller
{
Ctx
:
ctx
}
val
,
_
:=
ctrlr
.
GetUint8
(
"age"
)
if
val
!=
math
.
MaxUint8
{
t
.
Errorf
(
"TestGetUint8 expect %v,get %T,%v"
,
math
.
MaxUint8
,
val
,
val
)
}
}
func
TestGetUint16
(
t
*
testing
.
T
)
{
i
:=
context
.
NewInput
()
i
.
SetParam
(
"age"
,
strconv
.
FormatUint
(
math
.
MaxUint16
,
10
))
ctx
:=
&
context
.
Context
{
Input
:
i
}
ctrlr
:=
Controller
{
Ctx
:
ctx
}
val
,
_
:=
ctrlr
.
GetUint16
(
"age"
)
if
val
!=
math
.
MaxUint16
{
t
.
Errorf
(
"TestGetUint16 expect %v,get %T,%v"
,
math
.
MaxUint16
,
val
,
val
)
}
}
func
TestGetUint32
(
t
*
testing
.
T
)
{
i
:=
context
.
NewInput
()
i
.
SetParam
(
"age"
,
strconv
.
FormatUint
(
math
.
MaxUint32
,
10
))
ctx
:=
&
context
.
Context
{
Input
:
i
}
ctrlr
:=
Controller
{
Ctx
:
ctx
}
val
,
_
:=
ctrlr
.
GetUint32
(
"age"
)
if
val
!=
math
.
MaxUint32
{
t
.
Errorf
(
"TestGetUint32 expect %v,get %T,%v"
,
math
.
MaxUint32
,
val
,
val
)
}
}
func
TestGetUint64
(
t
*
testing
.
T
)
{
i
:=
context
.
NewInput
()
i
.
SetParam
(
"age"
,
strconv
.
FormatUint
(
math
.
MaxUint64
,
10
))
ctx
:=
&
context
.
Context
{
Input
:
i
}
ctrlr
:=
Controller
{
Ctx
:
ctx
}
val
,
_
:=
ctrlr
.
GetUint64
(
"age"
)
if
val
!=
math
.
MaxUint64
{
t
.
Errorf
(
"TestGetUint64 expect %v,get %T,%v"
,
uint64
(
math
.
MaxUint64
),
val
,
val
)
}
}
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