-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_descriptor.go
68 lines (63 loc) · 1.38 KB
/
field_descriptor.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
package model
import (
"strings"
)
//
// fieldDescriptor hold the col info and relate to struct field info
//
type fieldDescriptor struct {
fieldname string
colname string
coltype string
protected bool
nullable bool
isuk bool
ispk bool
isindex bool
}
//
// new a fieldDescriptor
//
func newFd(fieldname string, src string) *fieldDescriptor {
fd := new(fieldDescriptor)
fd.fieldname = fieldname
fd.parse(src)
return fd
}
//
// type User struct {
// Id int `db:"id int pk,index"`
// Name string `db:"name varchar(63) index"`
// Age int `db:"age int nil"`
// Addr string `db:"address varchar(256) nil"`
// Code string `db:"code varchar(32)"`
// Area string `db:"area varchar(32)"`
// AreaCode string `db:"area_code varchar(32)"`
// }
//
// type Book struct {
// Id int `db:"id int pk"`
// Title string `db:"title varchar(256) index"`
// AuthorId int `db:"author_id int index"`
// }
//
func (fd *fieldDescriptor) parse(src string) {
arr := strings.Split(src, "|")
fd.colname = strings.Trim(arr[0], " ")
fd.coltype = strings.Trim(arr[1], " ")
if len(arr) == 3 {
opt := strings.Split(arr[2], ",")
for _, o := range opt {
switch strings.Trim(o, " ") {
case "pk":
fd.ispk = true
case "nil":
fd.nullable = true
case "index":
fd.isindex = true
case "protected":
fd.protected = true
}
}
}
}