This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.go
203 lines (171 loc) · 3.65 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
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
201
202
203
package codegen
import (
"github.com/ronaksoft/rony/tools"
"strings"
)
/*
Creation Time: 2021 - Jul - 29
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
type ModelKey struct {
Arg *MessageArg
pks []Prop
cks []Prop
index int
alias string
}
func (m *ModelKey) Name() string {
return m.Arg.name
}
func (m *ModelKey) Alias() string {
return m.alias
}
func (m *ModelKey) PartitionKeys() []Prop {
var all []Prop
all = append(all, m.pks...)
return all
}
func (m *ModelKey) PKs() []Prop {
return m.PartitionKeys()
}
func (m *ModelKey) ClusteringKeys() []Prop {
var all []Prop
all = append(all, m.cks...)
return all
}
func (m *ModelKey) CKs() []Prop {
return m.ClusteringKeys()
}
func (m *ModelKey) Keys() []Prop {
var all []Prop
all = append(all, m.pks...)
all = append(all, m.cks...)
return all
}
func (m *ModelKey) Index() int {
return m.index
}
func (m *ModelKey) IsSubset(n *ModelKey) bool {
for _, np := range n.Keys() {
found := false
for _, mp := range m.Keys() {
if mp.Name == np.Name {
found = true
break
}
}
if !found {
return false
}
}
return true
}
func (m *ModelKey) HasProp(name string) bool {
for _, p := range m.Keys() {
if p.Name == name {
return true
}
}
return false
}
// NameTypes is kind of strings.Join function which returns a custom format of combination of model properties.
// This is a helper function used in code generator templates.
func (m *ModelKey) NameTypes(filter PropFilter, namePrefix string, nameCase TextCase, lang Language) string {
var props []Prop
switch filter {
case PropFilterALL:
props = m.Keys()
case PropFilterCKs:
props = m.ClusteringKeys()
case PropFilterPKs:
props = m.PartitionKeys()
}
sb := strings.Builder{}
for idx, p := range props {
if idx != 0 {
sb.WriteString(", ")
}
sb.WriteString(namePrefix)
switch nameCase {
case LowerCamelCase:
sb.WriteString(tools.ToLowerCamel(p.Name))
case CamelCase:
sb.WriteString(tools.ToCamel(p.Name))
case KebabCase:
sb.WriteString(tools.ToKebab(p.Name))
case SnakeCase:
sb.WriteString(tools.ToSnake(p.Name))
default:
sb.WriteString(p.Name)
}
sb.WriteRune(' ')
switch lang {
case LangGo:
sb.WriteString(p.GoType)
case LangCQL:
sb.WriteString(p.CqlType)
case LangProto:
sb.WriteString(p.ProtoType)
}
}
return sb.String()
}
// Names is kind of strings.Join function which returns a custom format of property names.
// This is a helper function used in code generator templates.
func (m *ModelKey) Names(filter PropFilter, prefix, postfix string, sep string, nameCase TextCase) string {
var props []Prop
switch filter {
case PropFilterALL:
props = m.Keys()
case PropFilterCKs:
props = m.ClusteringKeys()
case PropFilterPKs:
props = m.PartitionKeys()
}
sb := strings.Builder{}
for idx, p := range props {
if idx != 0 {
sb.WriteString(sep)
}
sb.WriteString(prefix)
switch nameCase {
case LowerCamelCase:
sb.WriteString(tools.ToLowerCamel(p.Name))
case CamelCase:
sb.WriteString(tools.ToCamel(p.Name))
case KebabCase:
sb.WriteString(tools.ToKebab(p.Name))
case SnakeCase:
sb.WriteString(tools.ToSnake(p.Name))
default:
sb.WriteString(p.Name)
}
sb.WriteString(postfix)
}
return sb.String()
}
type Prop struct {
Name string
ProtoType string
CqlType string
GoType string
Order Order
}
func (p *Prop) NameSC() string {
return tools.ToSnake(p.Name)
}
type PropFilter string
const (
PropFilterALL = "ALL"
PropFilterPKs = "PKs"
PropFilterCKs = "CKs"
)
type Order string
const (
ASC Order = "ASC"
DESC Order = "DESC"
)