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
d4e15c0b
Commit
d4e15c0b
authored
Aug 16, 2015
by
Sergey Shcherbina
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added MapGet template func
parent
4857e384
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
0 deletions
+74
-0
template.go
template.go
+1
-0
templatefunc.go
templatefunc.go
+73
-0
No files found.
template.go
View file @
d4e15c0b
...
...
@@ -54,6 +54,7 @@ func init() {
beegoTplFuncMap
[
"assets_js"
]
=
AssetsJs
beegoTplFuncMap
[
"assets_css"
]
=
AssetsCss
beegoTplFuncMap
[
"config"
]
=
Config
beegoTplFuncMap
[
"map_get"
]
=
MapGet
// go1.2 added template funcs
// Comparisons
...
...
templatefunc.go
View file @
d4e15c0b
...
...
@@ -652,3 +652,76 @@ func ge(arg1, arg2 interface{}) (bool, error) {
}
// go1.2 added template funcs. end
// getting value from map by keys
// usage:
// Data["m"] = map[string]interface{} {
// "a": 1,
// "1": map[string]float64{
// "c": 4,
// },
// }
//
// {{ map_get m "a" }} // return 1
// {{ map_get m 1 "c" }} // return 4
func
MapGet
(
arg1
interface
{},
arg2
...
interface
{})
(
interface
{},
error
)
{
arg1Type
:=
reflect
.
TypeOf
(
arg1
)
arg1Val
:=
reflect
.
ValueOf
(
arg1
)
if
arg1Type
.
Kind
()
==
reflect
.
Map
&&
len
(
arg2
)
>
0
{
// check whether arg2[0] type equals to arg1 key type
// if they are different, make convertion
arg2Val
:=
reflect
.
ValueOf
(
arg2
[
0
])
arg2Type
:=
reflect
.
TypeOf
(
arg2
[
0
])
if
arg2Type
.
Kind
()
!=
arg1Type
.
Key
()
.
Kind
()
{
// convert arg2Value to string
var
arg2ConvertedVal
interface
{}
arg2String
:=
fmt
.
Sprintf
(
"%v"
,
arg2
[
0
])
// convert string representation to any other type
switch
arg1Type
.
Key
()
.
Kind
()
{
case
reflect
.
Bool
:
arg2ConvertedVal
,
_
=
strconv
.
ParseBool
(
arg2String
)
case
reflect
.
Int
,
reflect
.
Int8
,
reflect
.
Int16
,
reflect
.
Int32
,
reflect
.
Int64
:
arg2ConvertedVal
,
_
=
strconv
.
ParseInt
(
arg2String
,
0
,
64
)
case
reflect
.
Uint
,
reflect
.
Uint8
,
reflect
.
Uint16
,
reflect
.
Uint32
,
reflect
.
Uint64
,
reflect
.
Uintptr
:
arg2ConvertedVal
,
_
=
strconv
.
ParseUint
(
arg2String
,
0
,
64
)
case
reflect
.
Float32
,
reflect
.
Float64
:
arg2ConvertedVal
,
_
=
strconv
.
ParseFloat
(
arg2String
,
64
)
case
reflect
.
String
:
arg2ConvertedVal
=
arg2String
default
:
arg2ConvertedVal
=
arg2Val
.
Interface
()
}
arg2Val
=
reflect
.
ValueOf
(
arg2ConvertedVal
)
}
storedVal
:=
arg1Val
.
MapIndex
(
arg2Val
)
var
result
interface
{}
switch
arg1Type
.
Elem
()
.
Kind
()
{
case
reflect
.
Bool
:
result
=
storedVal
.
Bool
()
case
reflect
.
Int
,
reflect
.
Int8
,
reflect
.
Int16
,
reflect
.
Int32
,
reflect
.
Int64
:
result
=
storedVal
.
Int
()
case
reflect
.
Uint
,
reflect
.
Uint8
,
reflect
.
Uint16
,
reflect
.
Uint32
,
reflect
.
Uint64
,
reflect
.
Uintptr
:
result
=
storedVal
.
Uint
()
case
reflect
.
Float32
,
reflect
.
Float64
:
result
=
storedVal
.
Float
()
case
reflect
.
String
:
result
=
storedVal
.
String
()
default
:
result
=
storedVal
.
Interface
()
}
// if there is more keys, handle this recursively
if
len
(
arg2
)
>
1
{
return
MapGet
(
result
,
arg2
[
1
:
]
...
)
}
else
{
return
result
,
nil
}
}
else
{
return
nil
,
nil
}
}
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