-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathservice.go
200 lines (161 loc) · 4.96 KB
/
service.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package vt
import (
"html/template"
"github.com/vmkteam/mfd-generator/generators/model"
base "github.com/vmkteam/mfd-generator/generators/repo"
"github.com/vmkteam/mfd-generator/mfd"
"github.com/dizzyfool/genna/util"
)
// ServiceNamespaceData stores namespace info
type ServiceNamespaceData struct {
Package string
ModelPackage string
EmbedLogPackage string
Name string
VarName string
HasImports bool
Imports []string
Entities []ServiceEntityData
}
// PackServiceNamespace packs mfd vt namespace to template data
func PackServiceNamespace(namespace *mfd.VTNamespace, options Options) ServiceNamespaceData {
imports := mfd.NewSet()
var entities []ServiceEntityData
for _, entity := range namespace.Entities {
if entity.Mode == mfd.ModeNone {
continue
}
packed := PackServiceEntity(*entity, options)
entities = append(entities, packed)
for _, imp := range packed.Imports {
imports.Append(imp)
}
}
name := util.CamelCased(util.Sanitize(namespace.Name))
return ServiceNamespaceData{
Package: options.Package,
ModelPackage: options.ModelPackage,
EmbedLogPackage: options.EmbedLogPackage,
Name: name,
VarName: mfd.VarName(name),
HasImports: imports.Len() > 0,
Imports: imports.Elements(),
Entities: entities,
}
}
func (tp ServiceNamespaceData) Raw(s string) template.HTML {
return template.HTML(s)
}
// ServiceEntityData stores entity info
type ServiceEntityData struct {
Name string
NamePlural string
VarName string
VarNamePlural string
ShortVarName string
Imports []string
HasSortColumns bool
SortColumns []string
PKs []base.PKPair
HasAlias bool
PKSearches []base.PKPair
AliasField string
AliasArg string
HasRelations bool
Relations []ServiceRelationData
UniqueRelations []ServiceRelationData
ReadOnly bool
}
// PackServiceEntity packs mfd vt entity to template data
func PackServiceEntity(vtEntity mfd.VTEntity, options Options) ServiceEntityData {
baseEntity := base.PackEntity(*vtEntity.Entity, base.Options{
Package: options.Package,
GoPGVer: options.GoPGVer,
CustomTypes: options.CustomTypes,
})
var relations []ServiceRelationData
var uniqueRelations []ServiceRelationData
var sortColumns []string
foreignKeys := make(map[string]struct{})
for _, vtAttr := range vtEntity.Attributes {
if vtAttr.AttrName != "" {
if !vtAttr.Attribute.IsArray && vtAttr.Summary {
sortColumns = append(sortColumns, vtAttr.Attribute.Name)
}
if vtAttr.Attribute.ForeignKey != "" && vtAttr.Attribute.ForeignEntity != nil {
serviceRelationData := PackServiceRelationData(*vtAttr, *vtAttr.Attribute.ForeignEntity)
relations = append(relations, serviceRelationData)
if _, ok := foreignKeys[vtAttr.Attribute.ForeignEntity.Namespace]; !ok {
foreignKeys[vtAttr.Attribute.ForeignEntity.Namespace] = struct{}{}
uniqueRelations = append(uniqueRelations, serviceRelationData)
}
}
}
}
// setting search for alias unique
var pkSearches []base.PKPair
var aliasField, aliasArg string
for _, vtAttr := range vtEntity.Attributes {
if vtAttr.AttrName == "" {
continue
}
attr := vtAttr.Attribute
if attr.DBName == "alias" {
aliasField, aliasArg = attr.Name, attr.Name
}
if attr.PrimaryKey {
search := vtEntity.Entity.SearchByAttrName(attr.Name, mfd.SearchNotEquals)
if search == nil {
continue
}
column := model.CustomSearchAttribute(*vtEntity.Entity, *search, model.Options{})
pkSearches = append(pkSearches, base.PKPair{
Field: attr.Name,
Arg: column.Name,
})
}
}
return ServiceEntityData{
Name: baseEntity.Name,
NamePlural: baseEntity.NamePlural,
VarName: baseEntity.VarName,
VarNamePlural: baseEntity.VarNamePlural,
ShortVarName: mfd.ShortVarName(baseEntity.Name),
HasSortColumns: len(sortColumns) > 0,
SortColumns: sortColumns,
PKs: baseEntity.PKs,
Imports: baseEntity.Imports,
HasAlias: aliasField != "",
PKSearches: pkSearches,
AliasField: aliasField,
AliasArg: aliasArg,
HasRelations: len(relations) > 0,
Relations: relations,
UniqueRelations: uniqueRelations,
ReadOnly: vtEntity.Mode == mfd.ModeReadOnly || vtEntity.Mode == mfd.ModeReadOnlyWithTemplates,
}
}
// ServiceRelationData stores relation info
type ServiceRelationData struct {
Name string
NameSpace string
FK string
PluralFK string
JSONName string
Nullable bool
IsArray bool
}
// PackServiceRelationData packs mfd vt attribute to relation template data
func PackServiceRelationData(vtAttr mfd.VTAttribute, foreign mfd.Entity) ServiceRelationData {
attr := vtAttr.Attribute
baseRelation := model.PackRelation(*attr, model.Options{})
return ServiceRelationData{
Name: attr.Name,
JSONName: mfd.JSONName(vtAttr.Name),
FK: baseRelation.ForeignEntity.Name,
PluralFK: mfd.MakePlural(util.CamelCased(baseRelation.ForeignEntity.Name)),
NameSpace: foreign.Namespace,
Nullable: attr.Nullable(),
IsArray: attr.IsArray,
}
}