-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtag.go
52 lines (49 loc) · 1.11 KB
/
tag.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
package sq
import (
xerr "github.com/goclub/error"
"reflect"
"strings"
)
type Tag struct {
Value string
}
func (t Tag) IsIgnoreInsert() bool {
sqTags := strings.Split(t.Value, "|")
for _, tag := range sqTags {
if strings.TrimSpace(tag) == "ignoreInsert" {
return true
}
}
return false
}
func TagToColumns(v interface{}) (columns []Column) {
rValue := reflect.ValueOf(v)
rType := rValue.Type()
if rType.Kind() == reflect.Ptr {
rValue = rValue.Elem()
rType = rValue.Type()
}
tier := 0
scanTagToColumns(rValue, rType, &columns, &tier)
return
}
func scanTagToColumns(rValue reflect.Value, rType reflect.Type, columns *[]Column, tier *int) {
if *tier > 10 {
panic(xerr.New("goclub/sql: Too many structures are nested"))
}
for i := 0; i < rType.NumField(); i++ {
structField := rType.Field(i)
tag, has := structField.Tag.Lookup("db")
if !has {
if structField.Type.Kind() == reflect.Struct {
fieldValue := rValue.Field(i)
scanTagToColumns(fieldValue, structField.Type, columns, tier)
// fieldValue
}
continue
}
if tag != "" {
*columns = append(*columns, Column(tag))
}
}
}