infra
/
goutils
Archived
1
0
Fork 0

template 함수 추가

This commit is contained in:
Sangbum Kim 2018-07-13 01:29:26 +09:00
parent c9fbc24e51
commit 08ec19535d
3 changed files with 90 additions and 0 deletions

23
tmpl/internal.go Normal file
View File

@ -0,0 +1,23 @@
package tmpl
import (
"reflect"
_ "unsafe"
)
//go:linkname indirectInterface text/template.indirectInterface
//go:nosplit
func indirectInterface(v reflect.Value) reflect.Value
//go:linkname truth text/template.truth
//go:nosplit
func truth(arg reflect.Value) bool
//go:linkname isTrue text/template.isTrue
//go:nosplit
func isTrue(arg reflect.Value) (truth, ok bool)
func evalTrue(val reflect.Value) (truth bool) {
truth, _ = isTrue(val)
return
}

66
tmpl/map.go Normal file
View File

@ -0,0 +1,66 @@
package tmpl
import (
"reflect"
_ "unsafe"
)
type TmplMap struct {
reflect.Value
}
func (m TmplMap) Keys() ([]reflect.Value) {
return m.MapKeys()
}
func (m TmplMap) MapWithDefaults(arg0 reflect.Value, args ...reflect.Value) (v reflect.Value) {
if iarg0 := indirectInterface(arg0); evalTrue(iarg0) {
if v = m.MapIndex(iarg0); evalTrue(v) {
return
}
}
for _, v = range args {
if truth(v) {
break
}
}
return
}
func (m TmplMap) Map(arg0 reflect.Value, args ...reflect.Value) (v reflect.Value) {
if iarg0 := indirectInterface(arg0); evalTrue(iarg0) {
if v = m.MapIndex(iarg0); evalTrue(v) {
return
}
}
argLen := len(args)
if argLen == 0 {
return
}
keys, defaultValue := args[:argLen-1], args[argLen-1]
for _, k := range keys {
if ik := indirectInterface(k); !evalTrue(ik) {
} else if v = m.Value.MapIndex(ik); evalTrue(v) {
return
}
}
v = defaultValue
return
}
func MakeMapFunc(arg ...reflect.Value) TmplMap {
if len(arg)%2 != 0 {
panic("bad makemap")
}
m := reflect.MakeMapWithSize(reflect.TypeOf(map[interface{}]interface{}{}), len(arg)/2)
for i := 0; i < len(arg); i += 2 {
k, v := indirectInterface(arg[i]), indirectInterface(arg[i+1])
if !(evalTrue(k) && evalTrue(v)) {
continue
}
m.SetMapIndex(k, v)
}
return TmplMap{m}
}

1
tmpl/stub.s Normal file
View File

@ -0,0 +1 @@
// make compiler happy