forked from si3nloong/go-rsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflect.go
73 lines (63 loc) · 1.23 KB
/
reflect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package rsql
import (
"log"
"reflect"
"strings"
)
// StructField :
type StructField struct {
Name string
Tag *StructTag
Type reflect.Type
}
// StructTag :
type StructTag struct {
name string
values map[string]string
}
// Struct :
type Struct struct {
Fields []*StructField
Names map[string]*StructField
}
func NewTag(name string, tag reflect.StructTag) *StructTag {
paths := strings.Split(tag.Get(name), ",")
t := new(StructTag)
t.name = paths[0]
t.values = make(map[string]string)
for _, v := range paths[1:] {
p := strings.SplitN(v, "=", 2)
t.values[p[0]] = ""
if len(p) > 1 {
t.values[p[0]] = p[1]
}
}
return t
}
func (t StructTag) Lookup(key string) (value string, ok bool) {
value, ok = t.values[key]
return
}
func getCodec(t reflect.Type) *Struct {
fields := make([]*StructField, 0)
codec := new(Struct)
for i := 0; i < t.NumField(); i++ {
fv := t.Field(i)
log.Println(fv)
tag := NewTag("rsql", fv.Tag)
f := new(StructField)
f.Name = fv.Name
f.Tag = tag
if tag.name != "" {
f.Name = tag.name
}
f.Type = fv.Type
fields = append(fields, f)
}
codec.Fields = fields
codec.Names = make(map[string]*StructField)
for _, f := range fields {
codec.Names[f.Name] = f
}
return codec
}