Skip to content

Commit

Permalink
improved
Browse files Browse the repository at this point in the history
Signed-off-by: 商讯在线 <[email protected]>
  • Loading branch information
admpub committed Aug 24, 2014
1 parent 560f3ef commit ed3f9c1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
28 changes: 22 additions & 6 deletions tagfast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit ed3f9c1

Please sign in to comment.