mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 10:30:55 +00:00
golint templatefunc
This commit is contained in:
parent
44bd3beb5e
commit
bcb1db256d
@ -43,13 +43,13 @@ func init() {
|
|||||||
beegoTplFuncMap["not_nil"] = NotNil
|
beegoTplFuncMap["not_nil"] = NotNil
|
||||||
beegoTplFuncMap["not_null"] = NotNil
|
beegoTplFuncMap["not_null"] = NotNil
|
||||||
beegoTplFuncMap["substr"] = Substr
|
beegoTplFuncMap["substr"] = Substr
|
||||||
beegoTplFuncMap["html2str"] = Html2str
|
beegoTplFuncMap["html2str"] = HTML2str
|
||||||
beegoTplFuncMap["str2html"] = Str2html
|
beegoTplFuncMap["str2html"] = Str2html
|
||||||
beegoTplFuncMap["htmlquote"] = Htmlquote
|
beegoTplFuncMap["htmlquote"] = Htmlquote
|
||||||
beegoTplFuncMap["htmlunquote"] = Htmlunquote
|
beegoTplFuncMap["htmlunquote"] = Htmlunquote
|
||||||
beegoTplFuncMap["renderform"] = RenderForm
|
beegoTplFuncMap["renderform"] = RenderForm
|
||||||
beegoTplFuncMap["assets_js"] = AssetsJs
|
beegoTplFuncMap["assets_js"] = AssetsJs
|
||||||
beegoTplFuncMap["assets_css"] = AssetsCss
|
beegoTplFuncMap["assets_css"] = AssetsCSS
|
||||||
beegoTplFuncMap["config"] = Config
|
beegoTplFuncMap["config"] = Config
|
||||||
beegoTplFuncMap["map_get"] = MapGet
|
beegoTplFuncMap["map_get"] = MapGet
|
||||||
|
|
||||||
|
@ -44,8 +44,8 @@ func Substr(s string, start, length int) string {
|
|||||||
return string(bt[start:end])
|
return string(bt[start:end])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Html2str returns escaping text convert from html.
|
// HTML2str returns escaping text convert from html.
|
||||||
func Html2str(html string) string {
|
func HTML2str(html string) string {
|
||||||
src := string(html)
|
src := string(html)
|
||||||
|
|
||||||
re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
|
re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
|
||||||
@ -115,7 +115,7 @@ var datePatterns = []string{
|
|||||||
"r", time.RFC1123Z,
|
"r", time.RFC1123Z,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse Date use PHP time format.
|
// DateParse Parse Date use PHP time format.
|
||||||
func DateParse(dateString, format string) (time.Time, error) {
|
func DateParse(dateString, format string) (time.Time, error) {
|
||||||
replacer := strings.NewReplacer(datePatterns...)
|
replacer := strings.NewReplacer(datePatterns...)
|
||||||
format = replacer.Replace(format)
|
format = replacer.Replace(format)
|
||||||
@ -139,14 +139,17 @@ func Compare(a, b interface{}) (equal bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CompareNot !Compare
|
||||||
func CompareNot(a, b interface{}) (equal bool) {
|
func CompareNot(a, b interface{}) (equal bool) {
|
||||||
return !Compare(a, b)
|
return !Compare(a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NotNil(a interface{}) (is_nil bool) {
|
// NotNil the same as CompareNot
|
||||||
|
func NotNil(a interface{}) (isNil bool) {
|
||||||
return CompareNot(a, nil)
|
return CompareNot(a, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Config get the Appconfig
|
||||||
func Config(returnType, key string, defaultVal interface{}) (value interface{}, err error) {
|
func Config(returnType, key string, defaultVal interface{}) (value interface{}, err error) {
|
||||||
switch returnType {
|
switch returnType {
|
||||||
case "String":
|
case "String":
|
||||||
@ -162,12 +165,12 @@ func Config(returnType, key string, defaultVal interface{}) (value interface{},
|
|||||||
case "DIY":
|
case "DIY":
|
||||||
value, err = AppConfig.DIY(key)
|
value, err = AppConfig.DIY(key)
|
||||||
default:
|
default:
|
||||||
err = errors.New("Config keys must be of type String, Bool, Int, Int64, Float, or DIY!")
|
err = errors.New("Config keys must be of type String, Bool, Int, Int64, Float, or DIY")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if reflect.TypeOf(returnType) != reflect.TypeOf(defaultVal) {
|
if reflect.TypeOf(returnType) != reflect.TypeOf(defaultVal) {
|
||||||
err = errors.New("defaultVal type does not match returnType!")
|
err = errors.New("defaultVal type does not match returnType")
|
||||||
} else {
|
} else {
|
||||||
value, err = defaultVal, nil
|
value, err = defaultVal, nil
|
||||||
}
|
}
|
||||||
@ -184,7 +187,7 @@ func Config(returnType, key string, defaultVal interface{}) (value interface{},
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert string to template.HTML type.
|
// Str2html Convert string to template.HTML type.
|
||||||
func Str2html(raw string) template.HTML {
|
func Str2html(raw string) template.HTML {
|
||||||
return template.HTML(raw)
|
return template.HTML(raw)
|
||||||
}
|
}
|
||||||
@ -258,7 +261,7 @@ func URLFor(endpoint string, values ...interface{}) string {
|
|||||||
return BeeApp.Handlers.URLFor(endpoint, values...)
|
return BeeApp.Handlers.URLFor(endpoint, values...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns script tag with src string.
|
// AssetsJs returns script tag with src string.
|
||||||
func AssetsJs(src string) template.HTML {
|
func AssetsJs(src string) template.HTML {
|
||||||
text := string(src)
|
text := string(src)
|
||||||
|
|
||||||
@ -267,8 +270,8 @@ func AssetsJs(src string) template.HTML {
|
|||||||
return template.HTML(text)
|
return template.HTML(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns stylesheet link tag with src string.
|
// AssetsCSS returns stylesheet link tag with src string.
|
||||||
func AssetsCss(src string) template.HTML {
|
func AssetsCSS(src string) template.HTML {
|
||||||
text := string(src)
|
text := string(src)
|
||||||
|
|
||||||
text = "<link href=\"" + src + "\" rel=\"stylesheet\" />"
|
text = "<link href=\"" + src + "\" rel=\"stylesheet\" />"
|
||||||
@ -276,7 +279,7 @@ func AssetsCss(src string) template.HTML {
|
|||||||
return template.HTML(text)
|
return template.HTML(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse form values to struct via tag.
|
// ParseForm will parse form values to struct via tag.
|
||||||
func ParseForm(form url.Values, obj interface{}) error {
|
func ParseForm(form url.Values, obj interface{}) error {
|
||||||
objT := reflect.TypeOf(obj)
|
objT := reflect.TypeOf(obj)
|
||||||
objV := reflect.ValueOf(obj)
|
objV := reflect.ValueOf(obj)
|
||||||
@ -398,7 +401,7 @@ var unKind = map[reflect.Kind]bool{
|
|||||||
reflect.UnsafePointer: true,
|
reflect.UnsafePointer: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
// render object to form html.
|
// RenderForm will render object to form html.
|
||||||
// obj must be a struct pointer.
|
// obj must be a struct pointer.
|
||||||
func RenderForm(obj interface{}) template.HTML {
|
func RenderForm(obj interface{}) template.HTML {
|
||||||
objT := reflect.TypeOf(obj)
|
objT := reflect.TypeOf(obj)
|
||||||
@ -651,9 +654,7 @@ func ge(arg1, arg2 interface{}) (bool, error) {
|
|||||||
return !lessThan, nil
|
return !lessThan, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// go1.2 added template funcs. end
|
// MapGet getting value from map by keys
|
||||||
|
|
||||||
// getting value from map by keys
|
|
||||||
// usage:
|
// usage:
|
||||||
// Data["m"] = map[string]interface{} {
|
// Data["m"] = map[string]interface{} {
|
||||||
// "a": 1,
|
// "a": 1,
|
||||||
@ -719,14 +720,11 @@ func MapGet(arg1 interface{}, arg2 ...interface{}) (interface{}, error) {
|
|||||||
// if there is more keys, handle this recursively
|
// if there is more keys, handle this recursively
|
||||||
if len(arg2) > 1 {
|
if len(arg2) > 1 {
|
||||||
return MapGet(result, arg2[1:]...)
|
return MapGet(result, arg2[1:]...)
|
||||||
} else {
|
|
||||||
return result, nil
|
|
||||||
}
|
}
|
||||||
} else {
|
return result, nil
|
||||||
return nil, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ func TestHtml2str(t *testing.T) {
|
|||||||
|
|
||||||
|
|
||||||
\n`
|
\n`
|
||||||
if Html2str(h) != "123\\n\n\\n" {
|
if HTML2str(h) != "123\\n\n\\n" {
|
||||||
t.Error("should be equal")
|
t.Error("should be equal")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -82,15 +82,15 @@ func TestCompareRelated(t *testing.T) {
|
|||||||
if !Compare("1", 1) {
|
if !Compare("1", 1) {
|
||||||
t.Error("should be equal")
|
t.Error("should be equal")
|
||||||
}
|
}
|
||||||
if CompareNot("abc", "abc") {
|
if CompareNot("abc", "abc") {
|
||||||
t.Error("should be equal")
|
t.Error("should be equal")
|
||||||
}
|
}
|
||||||
if !CompareNot("abc", "aBc") {
|
if !CompareNot("abc", "aBc") {
|
||||||
t.Error("should be not equal")
|
t.Error("should be not equal")
|
||||||
}
|
}
|
||||||
if !NotNil("a string") {
|
if !NotNil("a string") {
|
||||||
t.Error("should not be nil")
|
t.Error("should not be nil")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHtmlquote(t *testing.T) {
|
func TestHtmlquote(t *testing.T) {
|
||||||
@ -111,7 +111,7 @@ func TestHtmlunquote(t *testing.T) {
|
|||||||
|
|
||||||
func TestParseForm(t *testing.T) {
|
func TestParseForm(t *testing.T) {
|
||||||
type user struct {
|
type user struct {
|
||||||
Id int `form:"-"`
|
ID int `form:"-"`
|
||||||
tag string `form:"tag"`
|
tag string `form:"tag"`
|
||||||
Name interface{} `form:"username"`
|
Name interface{} `form:"username"`
|
||||||
Age int `form:"age,text"`
|
Age int `form:"age,text"`
|
||||||
@ -123,7 +123,7 @@ func TestParseForm(t *testing.T) {
|
|||||||
|
|
||||||
u := user{}
|
u := user{}
|
||||||
form := url.Values{
|
form := url.Values{
|
||||||
"Id": []string{"1"},
|
"ID": []string{"1"},
|
||||||
"-": []string{"1"},
|
"-": []string{"1"},
|
||||||
"tag": []string{"no"},
|
"tag": []string{"no"},
|
||||||
"username": []string{"test"},
|
"username": []string{"test"},
|
||||||
@ -139,8 +139,8 @@ func TestParseForm(t *testing.T) {
|
|||||||
if err := ParseForm(form, &u); err != nil {
|
if err := ParseForm(form, &u); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if u.Id != 0 {
|
if u.ID != 0 {
|
||||||
t.Errorf("Id should equal 0 but got %v", u.Id)
|
t.Errorf("ID should equal 0 but got %v", u.ID)
|
||||||
}
|
}
|
||||||
if len(u.tag) != 0 {
|
if len(u.tag) != 0 {
|
||||||
t.Errorf("tag's length should equal 0 but got %v", len(u.tag))
|
t.Errorf("tag's length should equal 0 but got %v", len(u.tag))
|
||||||
@ -168,7 +168,7 @@ func TestParseForm(t *testing.T) {
|
|||||||
|
|
||||||
func TestRenderForm(t *testing.T) {
|
func TestRenderForm(t *testing.T) {
|
||||||
type user struct {
|
type user struct {
|
||||||
Id int `form:"-"`
|
ID int `form:"-"`
|
||||||
tag string `form:"tag"`
|
tag string `form:"tag"`
|
||||||
Name interface{} `form:"username"`
|
Name interface{} `form:"username"`
|
||||||
Age int `form:"age,text,年龄:"`
|
Age int `form:"age,text,年龄:"`
|
||||||
|
Loading…
Reference in New Issue
Block a user