From bcb1db256d2eba7a7ac082689e10f53bff24a53e Mon Sep 17 00:00:00 2001 From: astaxie Date: Tue, 8 Sep 2015 23:41:41 +0800 Subject: [PATCH] golint templatefunc --- template.go | 4 ++-- templatefunc.go | 40 +++++++++++++++++++--------------------- templatefunc_test.go | 30 +++++++++++++++--------------- 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/template.go b/template.go index 6e196c0a..8822c514 100644 --- a/template.go +++ b/template.go @@ -43,13 +43,13 @@ func init() { beegoTplFuncMap["not_nil"] = NotNil beegoTplFuncMap["not_null"] = NotNil beegoTplFuncMap["substr"] = Substr - beegoTplFuncMap["html2str"] = Html2str + beegoTplFuncMap["html2str"] = HTML2str beegoTplFuncMap["str2html"] = Str2html beegoTplFuncMap["htmlquote"] = Htmlquote beegoTplFuncMap["htmlunquote"] = Htmlunquote beegoTplFuncMap["renderform"] = RenderForm beegoTplFuncMap["assets_js"] = AssetsJs - beegoTplFuncMap["assets_css"] = AssetsCss + beegoTplFuncMap["assets_css"] = AssetsCSS beegoTplFuncMap["config"] = Config beegoTplFuncMap["map_get"] = MapGet diff --git a/templatefunc.go b/templatefunc.go index 6b77fda3..62548659 100644 --- a/templatefunc.go +++ b/templatefunc.go @@ -44,8 +44,8 @@ func Substr(s string, start, length int) string { return string(bt[start:end]) } -// Html2str returns escaping text convert from html. -func Html2str(html string) string { +// HTML2str returns escaping text convert from html. +func HTML2str(html string) string { src := string(html) re, _ := regexp.Compile("\\<[\\S\\s]+?\\>") @@ -115,7 +115,7 @@ var datePatterns = []string{ "r", time.RFC1123Z, } -// Parse Date use PHP time format. +// DateParse Parse Date use PHP time format. func DateParse(dateString, format string) (time.Time, error) { replacer := strings.NewReplacer(datePatterns...) format = replacer.Replace(format) @@ -139,14 +139,17 @@ func Compare(a, b interface{}) (equal bool) { return } +// CompareNot !Compare func CompareNot(a, b interface{}) (equal bool) { 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) } +// Config get the Appconfig func Config(returnType, key string, defaultVal interface{}) (value interface{}, err error) { switch returnType { case "String": @@ -162,12 +165,12 @@ func Config(returnType, key string, defaultVal interface{}) (value interface{}, case "DIY": value, err = AppConfig.DIY(key) 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 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 { value, err = defaultVal, nil } @@ -184,7 +187,7 @@ func Config(returnType, key string, defaultVal interface{}) (value interface{}, return } -// Convert string to template.HTML type. +// Str2html Convert string to template.HTML type. func Str2html(raw string) template.HTML { return template.HTML(raw) } @@ -258,7 +261,7 @@ func URLFor(endpoint string, values ...interface{}) string { 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 { text := string(src) @@ -267,8 +270,8 @@ func AssetsJs(src string) template.HTML { return template.HTML(text) } -// returns stylesheet link tag with src string. -func AssetsCss(src string) template.HTML { +// AssetsCSS returns stylesheet link tag with src string. +func AssetsCSS(src string) template.HTML { text := string(src) text = "" @@ -276,7 +279,7 @@ func AssetsCss(src string) template.HTML { 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 { objT := reflect.TypeOf(obj) objV := reflect.ValueOf(obj) @@ -398,7 +401,7 @@ var unKind = map[reflect.Kind]bool{ reflect.UnsafePointer: true, } -// render object to form html. +// RenderForm will render object to form html. // obj must be a struct pointer. func RenderForm(obj interface{}) template.HTML { objT := reflect.TypeOf(obj) @@ -651,9 +654,7 @@ func ge(arg1, arg2 interface{}) (bool, error) { return !lessThan, nil } -// go1.2 added template funcs. end - -// getting value from map by keys +// MapGet getting value from map by keys // usage: // Data["m"] = map[string]interface{} { // "a": 1, @@ -719,14 +720,11 @@ func MapGet(arg1 interface{}, arg2 ...interface{}) (interface{}, error) { // 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 + return result, nil } - - } else { return nil, nil + } + return nil, nil } diff --git a/templatefunc_test.go b/templatefunc_test.go index cc467d95..98fbf7ab 100644 --- a/templatefunc_test.go +++ b/templatefunc_test.go @@ -40,7 +40,7 @@ func TestHtml2str(t *testing.T) { \n` - if Html2str(h) != "123\\n\n\\n" { + if HTML2str(h) != "123\\n\n\\n" { t.Error("should be equal") } } @@ -82,15 +82,15 @@ func TestCompareRelated(t *testing.T) { if !Compare("1", 1) { t.Error("should be equal") } - if CompareNot("abc", "abc") { - t.Error("should be equal") - } - if !CompareNot("abc", "aBc") { - t.Error("should be not equal") - } - if !NotNil("a string") { - t.Error("should not be nil") - } + if CompareNot("abc", "abc") { + t.Error("should be equal") + } + if !CompareNot("abc", "aBc") { + t.Error("should be not equal") + } + if !NotNil("a string") { + t.Error("should not be nil") + } } func TestHtmlquote(t *testing.T) { @@ -111,7 +111,7 @@ func TestHtmlunquote(t *testing.T) { func TestParseForm(t *testing.T) { type user struct { - Id int `form:"-"` + ID int `form:"-"` tag string `form:"tag"` Name interface{} `form:"username"` Age int `form:"age,text"` @@ -123,7 +123,7 @@ func TestParseForm(t *testing.T) { u := user{} form := url.Values{ - "Id": []string{"1"}, + "ID": []string{"1"}, "-": []string{"1"}, "tag": []string{"no"}, "username": []string{"test"}, @@ -139,8 +139,8 @@ func TestParseForm(t *testing.T) { if err := ParseForm(form, &u); err != nil { t.Fatal(err) } - if u.Id != 0 { - t.Errorf("Id should equal 0 but got %v", u.Id) + if u.ID != 0 { + t.Errorf("ID should equal 0 but got %v", u.ID) } if len(u.tag) != 0 { 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) { type user struct { - Id int `form:"-"` + ID int `form:"-"` tag string `form:"tag"` Name interface{} `form:"username"` Age int `form:"age,text,年龄:"`