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
68f2a5dd
Commit
68f2a5dd
authored
Dec 13, 2013
by
mingqi.zhou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add convertor for cache
parent
3c92cce9
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
248 additions
and
0 deletions
+248
-0
conv.go
cache/conv.go
+104
-0
conv_test.go
cache/conv_test.go
+144
-0
No files found.
cache/conv.go
0 → 100644
View file @
68f2a5dd
package
cache
import
(
"fmt"
"strconv"
)
func
GetString
(
v
interface
{})
string
{
switch
result
:=
v
.
(
type
)
{
case
string
:
return
result
case
[]
byte
:
return
string
(
result
)
default
:
if
v
==
nil
{
return
""
}
else
{
return
fmt
.
Sprintf
(
"%v"
,
result
)
}
}
}
func
GetInt
(
v
interface
{})
int
{
switch
result
:=
v
.
(
type
)
{
case
int
:
return
result
case
int32
:
return
int
(
result
)
case
int64
:
return
int
(
result
)
default
:
d
:=
GetString
(
v
)
if
d
!=
""
{
value
,
err
:=
strconv
.
Atoi
(
d
)
if
err
==
nil
{
return
value
}
}
}
return
0
}
func
GetInt64
(
v
interface
{})
int64
{
switch
result
:=
v
.
(
type
)
{
case
int
:
return
int64
(
result
)
case
int32
:
return
int64
(
result
)
case
int64
:
return
result
default
:
d
:=
GetString
(
v
)
if
d
!=
""
{
result
,
err
:=
strconv
.
ParseInt
(
d
,
10
,
64
)
if
err
==
nil
{
return
result
}
}
}
return
0
}
func
GetFloat64
(
v
interface
{})
float64
{
switch
result
:=
v
.
(
type
)
{
case
float64
:
return
result
default
:
d
:=
GetString
(
v
)
if
d
!=
""
{
value
,
err
:=
strconv
.
ParseFloat
(
d
,
64
)
if
err
==
nil
{
return
value
}
}
}
return
0
}
func
GetBool
(
v
interface
{})
bool
{
switch
result
:=
v
.
(
type
)
{
case
bool
:
return
result
default
:
d
:=
GetString
(
v
)
if
d
!=
""
{
result
,
err
:=
strconv
.
ParseBool
(
d
)
if
err
==
nil
{
return
result
}
}
}
return
false
}
func
getByteArray
(
v
interface
{})
[]
byte
{
switch
result
:=
v
.
(
type
)
{
case
[]
byte
:
return
result
case
string
:
return
[]
byte
(
result
)
default
:
return
nil
}
}
cache/conv_test.go
0 → 100644
View file @
68f2a5dd
package
cache
import
(
"testing"
)
func
TestGetString
(
t
*
testing
.
T
)
{
var
t1
=
"test1"
if
"test1"
!=
GetString
(
t1
)
{
t
.
Error
(
"get string from string error"
)
}
var
t2
=
[]
byte
(
"test2"
)
if
"test2"
!=
GetString
(
t2
)
{
t
.
Error
(
"get string from byte array error"
)
}
var
t3
int
=
1
if
"1"
!=
GetString
(
t3
)
{
t
.
Error
(
"get string from int error"
)
}
var
t4
int64
=
1
if
"1"
!=
GetString
(
t4
)
{
t
.
Error
(
"get string from int64 error"
)
}
var
t5
float64
=
1.1
if
"1.1"
!=
GetString
(
t5
)
{
t
.
Error
(
"get string from float64 error"
)
}
if
""
!=
GetString
(
nil
)
{
t
.
Error
(
"get string from nil error"
)
}
}
func
TestGetInt
(
t
*
testing
.
T
)
{
var
t1
int
=
1
if
1
!=
GetInt
(
t1
)
{
t
.
Error
(
"get int from int error"
)
}
var
t2
int32
=
32
if
32
!=
GetInt
(
t2
)
{
t
.
Error
(
"get int from int32 error"
)
}
var
t3
int64
=
64
if
64
!=
GetInt
(
t3
)
{
t
.
Error
(
"get int from int64 error"
)
}
var
t4
=
"128"
if
128
!=
GetInt
(
t4
)
{
t
.
Error
(
"get int from num string error"
)
}
if
0
!=
GetInt
(
nil
)
{
t
.
Error
(
"get int from nil error"
)
}
}
func
TestGetInt64
(
t
*
testing
.
T
)
{
var
i
int64
=
1
var
t1
int
=
1
if
i
!=
GetInt64
(
t1
)
{
t
.
Error
(
"get int64 from int error"
)
}
var
t2
int32
=
1
if
i
!=
GetInt64
(
t2
)
{
t
.
Error
(
"get int64 from int32 error"
)
}
var
t3
int64
=
1
if
i
!=
GetInt64
(
t3
)
{
t
.
Error
(
"get int64 from int64 error"
)
}
var
t4
=
"1"
if
i
!=
GetInt64
(
t4
)
{
t
.
Error
(
"get int64 from num string error"
)
}
if
0
!=
GetInt64
(
nil
)
{
t
.
Error
(
"get int64 from nil"
)
}
}
func
TestGetFloat64
(
t
*
testing
.
T
)
{
var
f
float64
=
1.11
var
t1
float32
=
1.11
if
f
!=
GetFloat64
(
t1
)
{
t
.
Error
(
"get float64 from float32 error"
)
}
var
t2
float64
=
1.11
if
f
!=
GetFloat64
(
t2
)
{
t
.
Error
(
"get float64 from float64 error"
)
}
var
t3
=
"1.11"
if
f
!=
GetFloat64
(
t3
)
{
t
.
Error
(
"get float64 from string error"
)
}
var
f2
float64
=
1
var
t4
int
=
1
if
f2
!=
GetFloat64
(
t4
)
{
t
.
Error
(
"get float64 from int error"
)
}
if
0
!=
GetFloat64
(
nil
)
{
t
.
Error
(
"get float64 from nil error"
)
}
}
func
TestGetBool
(
t
*
testing
.
T
)
{
var
t1
=
true
if
true
!=
GetBool
(
t1
)
{
t
.
Error
(
"get bool from bool error"
)
}
var
t2
=
"true"
if
true
!=
GetBool
(
t2
)
{
t
.
Error
(
"get bool from string error"
)
}
if
false
!=
GetBool
(
nil
)
{
t
.
Error
(
"get bool from nil error"
)
}
}
func
TestGetByteArray
(
t
*
testing
.
T
)
{
var
b
=
[]
byte
(
"test"
)
var
t1
=
[]
byte
(
"test"
)
if
!
byteArrayEquals
(
b
,
getByteArray
(
t1
))
{
t
.
Error
(
"get byte array from byte array error"
)
}
var
t2
=
"test"
if
!
byteArrayEquals
(
b
,
getByteArray
(
t2
))
{
t
.
Error
(
"get byte array from string error"
)
}
if
nil
!=
getByteArray
(
nil
)
{
t
.
Error
(
"get byte array from nil error"
)
}
}
func
byteArrayEquals
(
a
[]
byte
,
b
[]
byte
)
bool
{
if
len
(
a
)
!=
len
(
b
)
{
return
false
}
for
i
,
v
:=
range
a
{
if
v
!=
b
[
i
]
{
return
false
}
}
return
true
}
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