1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 04:30:55 +00:00

golint templatefunc

This commit is contained in:
astaxie 2015-09-08 23:41:41 +08:00
parent 44bd3beb5e
commit bcb1db256d
3 changed files with 36 additions and 38 deletions

View File

@ -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

View File

@ -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 = "<link href=\"" + src + "\" rel=\"stylesheet\" />"
@ -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
}

View File

@ -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,年龄:"`