-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.go
159 lines (135 loc) · 3.39 KB
/
model.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package raiden
import (
"reflect"
"regexp"
"strings"
"github.com/sev-2/raiden/pkg/utils"
)
type (
ModelBase struct {
}
// definition of column tag, example :
// column:"name:id;type:bigint;primaryKey;autoIncrement;nullable:false;unique;default:now()"
ColumnTag struct {
Name string
Type string
PrimaryKey bool
AutoIncrement bool
Nullable bool
Default any
Unique bool
Index bool
}
// definition of join tag, example:
// - join:"joinType:hasOne;primaryKey:id;foreignKey:candidate_id"
// - join:"joinType:hasMany;primaryKey:id;foreignKey:scouter_id"
// - join:"joinType:manyToMany;through:submission;sourcePrimaryKey:id;sourceForeignKey:candidate_id;targetPrimaryKey:id;targetForeign:candidate_id"
JoinTag struct {
JoinType RelationType
PrimaryKey string
ForeignKey string
Through string
SourcePrimaryKey string
SourceForeignKey string
TargetPrimaryKey string
TargetForeignKey string
}
RelationType string
)
var (
RelationTypeHasOne RelationType = "hasOne"
RelationTypeHasMany RelationType = "hasMany"
RelationTypeManyToMany RelationType = "manyToMany"
)
func UnmarshalColumnTag(tag string) ColumnTag {
columnTag := ColumnTag{
Nullable: true,
PrimaryKey: false,
AutoIncrement: false,
Unique: false,
}
tagSplit := strings.Split(tag, ";")
tagMap := make(map[string]string)
for _, c := range tagSplit {
cSplit := strings.Split(c, ":")
if len(cSplit) == 2 {
tagMap[cSplit[0]] = cSplit[1]
} else if len(cSplit) == 1 {
tagMap[cSplit[0]] = ""
}
}
defaultValue := "default"
// Iterate over matches and populate ColumnTag fields
for key, value := range tagMap {
switch key {
case "name":
columnTag.Name = value
case "type":
columnTag.Type = value
case "primaryKey":
columnTag.PrimaryKey = true
case "autoIncrement":
columnTag.AutoIncrement = true
case "nullable":
if value != "" {
columnTag.Nullable = value == "true"
}
case "default":
if value != "" {
defaultValue = value
}
case "unique":
columnTag.Unique = true
}
}
if defaultValue != "default" {
columnTag.Default = &defaultValue
}
return columnTag
}
func UnmarshalJoinTag(tag string) JoinTag {
joinTag := JoinTag{}
// Regular expression to match key-value pairs
re := regexp.MustCompile(`(\w+):([^;]+);?`)
// Find all matches in the tag string
matches := re.FindAllStringSubmatch(tag, -1)
// Iterate over matches and populate JoinTag fields
for _, match := range matches {
key := match[1]
value := match[2]
switch key {
case "joinType":
joinTag.JoinType = RelationType(value)
case "primaryKey":
joinTag.PrimaryKey = value
case "foreignKey":
joinTag.ForeignKey = value
case "through":
joinTag.Through = value
case "sourcePrimaryKey":
joinTag.SourcePrimaryKey = value
case "sourceForeignKey":
joinTag.SourceForeignKey = value
case "targetPrimaryKey":
joinTag.TargetPrimaryKey = value
case "targetForeign":
joinTag.TargetForeignKey = value
}
}
return joinTag
}
func GetTableName(model any) (tableName string) {
rt := reflect.TypeOf(model)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
tableName = strings.ToLower(utils.ToSnakeCase(rt.Name()))
field, found := rt.FieldByName("Metadata")
if found {
foundTableName := field.Tag.Get("tableName")
if foundTableName != "" {
tableName = foundTableName
}
}
return
}