From ed3f9c1cf069a80f72224ef5ab1d5bad80caaa92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=95=86=E8=AE=AF=E5=9C=A8=E7=BA=BF?= Date: Sun, 24 Aug 2014 09:30:21 +0800 Subject: [PATCH] improved MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 商讯在线 --- README.md | 10 +++++----- tagfast.go | 28 ++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 18d68c2..6844b10 100644 --- a/README.md +++ b/README.md @@ -19,17 +19,17 @@ type Coscms struct { } func main(){ - m:=Coscms{} + m := Coscms{} t := reflect.TypeOf(m) for i := 0; i < t.NumField(); i++ { - - widget:=tagfast.Tag(t,i,"form_widget") + f := t.Field(i) + widget:=tagfast.Tag(t,f,"form_widget") fmt.Println("widget:",widget) - valid:=tagfast.Tag(t,i,"valid") + valid:=tagfast.Tag(t,f,"valid") fmt.Println("valid:",valid) - xorm:=tagfast.Tag(t,i,"xorm") + xorm:=tagfast.Tag(t,f,"xorm") fmt.Println("xorm:",xorm) } diff --git a/tagfast.go b/tagfast.go index e02ec1c..7cdc2d6 100644 --- a/tagfast.go +++ b/tagfast.go @@ -31,17 +31,33 @@ func GetTag(struct_name string, field_name string) (r TagFast, ok bool) { return } -//usage: Tag(t, i, "form") -func Tag(t reflect.Type, field_no int, key string) (tag string) { - if t.Field(field_no).Tag == "" { +//usage: Tag1(t, i, "form") +func Tag1(t reflect.Type, field_no int, key string) (tag string) { + f := t.Field(field_no) + tag = Tag(t, f, key) + return +} + +//usage: Tag2(t, "Id", "form") +func Tag2(t reflect.Type, field_name string, key string) (tag string) { + f, ok := t.FieldByName(field_name) + if !ok { + return "" + } + tag = Tag(t, f, key) + return +} + +func Tag(t reflect.Type, f reflect.StructField, key string) (tag string) { + if f.Tag == "" { return "" } - if v, ok := GetTag(t.String(), t.Field(field_no).Name); ok { + if v, ok := GetTag(t.String(), f.Name); ok { tag = v.Get(key) } else { - v := TagFast{Tag: t.Field(field_no).Tag} + v := TagFast{Tag: f.Tag} tag = v.Get(key) - CacheTag(t.String(), t.Field(field_no).Name, v) + CacheTag(t.String(), f.Name, v) } return }