-
Notifications
You must be signed in to change notification settings - Fork 98
/
mongodb.go
391 lines (344 loc) · 10.2 KB
/
mongodb.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package parser
import (
"context"
"fmt"
"strings"
"sync/atomic"
"time"
"github.com/huandu/xstrings"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/mongo"
mgoOptions "go.mongodb.org/mongo-driver/mongo/options"
"github.com/zhufuyi/sponge/pkg/mgo"
"github.com/zhufuyi/sponge/pkg/utils"
)
const (
goTypeOID = "primitive.ObjectID"
goTypeInt = "int"
goTypeInt64 = "int64"
goTypeFloat64 = "float64"
goTypeString = "string"
goTypeTime = "time.Time"
goTypeBool = "bool"
goTypeNil = "nil"
goTypeBytes = "[]byte"
goTypeStrings = "[]string"
goTypeInts = "[]int"
goTypeInterface = "interface{}"
goTypeSliceInterface = "[]interface{}"
// SubStructKey sub struct key
SubStructKey = "_sub_struct_"
// ProtoSubStructKey proto sub struct key
ProtoSubStructKey = "_proto_sub_struct_"
oidName = "_id"
)
var mgoTypeToGo = map[bsontype.Type]string{
bson.TypeObjectID: goTypeOID,
bson.TypeInt32: goTypeInt,
bson.TypeInt64: goTypeInt64,
bson.TypeDouble: goTypeFloat64,
bson.TypeString: goTypeString,
bson.TypeArray: goTypeSliceInterface,
bson.TypeEmbeddedDocument: goTypeInterface,
bson.TypeTimestamp: goTypeTime,
bson.TypeDateTime: goTypeTime,
bson.TypeBoolean: goTypeBool,
bson.TypeNull: goTypeNil,
bson.TypeBinary: goTypeBytes,
bson.TypeUndefined: goTypeInterface,
bson.TypeCodeWithScope: goTypeString,
bson.TypeSymbol: goTypeString,
bson.TypeRegex: goTypeString,
bson.TypeDecimal128: goTypeInterface,
bson.TypeDBPointer: goTypeInterface,
bson.TypeMinKey: goTypeInt,
bson.TypeMaxKey: goTypeInt,
bson.TypeJavaScript: goTypeString,
}
var jsonTagFormat int32 = 1 // 0: snake case, 1: camel case
// SetJSONTagSnakeCase set json tag format to snake case
func SetJSONTagSnakeCase() {
atomic.AddInt32(&jsonTagFormat, -jsonTagFormat)
}
// SetJSONTagCamelCase set json tag format to camel case
func SetJSONTagCamelCase() {
atomic.AddInt32(&jsonTagFormat, 1)
}
// MgoField mongo field
type MgoField struct {
Name string `json:"name"`
Type string `json:"type"`
Comment string `json:"comment"`
ObjectStr string `json:"objectStr"`
ProtoObjectStr string `json:"protoObjectStr"`
}
// GetMongodbTableInfo get table info from mongodb
func GetMongodbTableInfo(dsn string, tableName string) ([]*MgoField, error) {
timeout := time.Second * 5
opts := &mgoOptions.ClientOptions{Timeout: &timeout}
dsn = utils.AdaptiveMongodbDsn(dsn)
db, err := mgo.Init(dsn, opts)
if err != nil {
return nil, err
}
return getMongodbTableFields(db, tableName)
}
func getMongodbTableFields(db *mongo.Database, collectionName string) ([]*MgoField, error) {
findOpts := new(mgoOptions.FindOneOptions)
findOpts.Sort = bson.M{oidName: -1}
result := db.Collection(collectionName).FindOne(context.Background(), bson.M{}, findOpts)
raw, err := result.Raw()
if err != nil {
return nil, err
}
elements, err := raw.Elements()
if err != nil {
return nil, err
}
fields := []*MgoField{}
names := []string{}
for _, element := range elements {
name := element.Key()
if name == "deleted_at" { // filter deleted_at, used for soft delete
continue
}
names = append(names, name)
t, o, p := getTypeFromMgo(name, element)
fields = append(fields, &MgoField{
Name: name,
Type: t,
ObjectStr: o,
ProtoObjectStr: p,
})
}
return embedTimeField(names, fields), nil
}
func getTypeFromMgo(name string, element bson.RawElement) (goTypeStr string, goObjectStr string, protoObjectStr string) {
v := element.Value()
switch v.Type {
case bson.TypeEmbeddedDocument:
var br bson.Raw = v.Value
es, err := br.Elements()
if err != nil {
return goTypeInterface, "", ""
}
return parseObject(name, es)
case bson.TypeArray:
var br bson.Raw = v.Value
es, err := br.Elements()
if err != nil {
return goTypeInterface, "", ""
}
if len(es) == 0 {
return goTypeInterface, "", ""
}
t, o, p := parseArray(name, es[0])
return convertToSingular(t, o, p)
}
if goType, ok := mgoTypeToGo[v.Type]; !ok {
return goTypeInterface, "", ""
} else { //nolint
return goType, "", ""
}
}
func parseObject(name string, elements []bson.RawElement) (goTypeStr string, goObjectStr string, protoObjectStr string) {
var goObjStr string
var protoObjStr string
for num, element := range elements {
t, _, _ := getTypeFromMgo(name, element)
k := element.Key()
var jsonTag string
if jsonTagFormat == 0 {
jsonTag = xstrings.ToSnakeCase(k)
} else {
jsonTag = toLowerFirst(xstrings.ToCamelCase(k))
}
goObjStr += fmt.Sprintf(" %s %s `bson:\"%s\" json:\"%s\"`\n", xstrings.ToCamelCase(k), t, k, jsonTag)
num++
protoObjStr += fmt.Sprintf(" %s %s = %d;\n", convertToProtoFieldType(name, t), k, num)
}
return "*" + xstrings.ToCamelCase(name),
fmt.Sprintf("type %s struct {\n%s}\n", xstrings.ToCamelCase(name), goObjStr),
fmt.Sprintf("message %s {\n%s}\n", xstrings.ToCamelCase(name), protoObjStr)
}
func parseArray(name string, element bson.RawElement) (goTypeStr string, goObjectStr string, protoObjectStr string) {
t, o, p := getTypeFromMgo(name, element)
if o != "" {
return "[]" + t, o, p
}
return "[]" + t, "", ""
}
func toLowerFirst(str string) string {
if len(str) == 0 {
return str
}
return strings.ToLower(string(str[0])) + str[1:]
}
func embedTimeField(names []string, fields []*MgoField) []*MgoField {
isHaveCreatedAt, isHaveUpdatedAt := false, false
for _, name := range names {
if name == "created_at" || name == "createdAt" {
isHaveCreatedAt = true
}
if name == "updated_at" || name == "updatedAt" {
isHaveUpdatedAt = true
}
names = append(names, name)
}
var timeFields []*MgoField
if !isHaveCreatedAt {
timeFields = append(timeFields, &MgoField{
Name: "created_at",
Type: goTypeTime,
})
}
if !isHaveUpdatedAt {
timeFields = append(timeFields, &MgoField{
Name: "updated_at",
Type: goTypeTime,
})
}
if len(timeFields) == 0 {
return fields
}
return append(fields, timeFields...)
}
// ConvertToSQLByMgoFields convert to mysql table ddl
func ConvertToSQLByMgoFields(tableName string, fields []*MgoField) (string, map[string]string) {
isHaveID := false
fieldStr := ""
srcMongoTypeMap := make(map[string]string) // name:type
objectStrs := []string{}
protoObjectStrs := []string{}
for _, field := range fields {
switch field.Type {
case goTypeInterface, goTypeSliceInterface:
srcMongoTypeMap[field.Name] = xstrings.ToCamelCase(field.Name)
default:
srcMongoTypeMap[field.Name] = field.Type
}
if field.Name == oidName {
isHaveID = true
srcMongoTypeMap["id"] = field.Type
continue
}
fieldStr += fmt.Sprintf(" %s %s,\n", field.Name, convertMongoToMysqlType(field.Type))
if field.ObjectStr != "" {
objectStrs = append(objectStrs, field.ObjectStr)
protoObjectStrs = append(protoObjectStrs, field.ProtoObjectStr)
}
}
if isHaveID {
fieldStr = " id bigint unsigned primary key,\n" + fieldStr
}
fieldStr = strings.TrimSuffix(fieldStr, ",\n")
if len(objectStrs) > 0 {
srcMongoTypeMap[SubStructKey] = strings.Join(objectStrs, "\n") + "\n"
srcMongoTypeMap[ProtoSubStructKey] = strings.Join(protoObjectStrs, "\n") + "\n"
}
return fmt.Sprintf("CREATE TABLE %s (\n%s\n);", tableName, fieldStr), srcMongoTypeMap
}
// nolint
func convertMongoToMysqlType(goType string) string {
switch goType {
case goTypeInt:
return "int"
case goTypeInt64:
return "bigint"
case goTypeFloat64:
return "double"
case goTypeString:
return "varchar(255)"
case goTypeTime:
return "timestamp" //nolint
case goTypeBool:
return "tinyint(1)"
case goTypeOID, goTypeNil, goTypeBytes, goTypeInterface, goTypeSliceInterface, goTypeInts, goTypeStrings:
return "json"
}
return "json"
}
// nolint
func convertToProtoFieldType(name string, goType string) string {
switch goType {
case "int":
return "int32"
case "uint":
return "uint32" //nolint
case "time.Time":
return "int64"
case "float32":
return "float"
case "float64":
return "double"
case goTypeInts, "[]int64":
return "repeated int64"
case "[]int32":
return "repeated int32"
case "[]byte":
return "string"
case goTypeStrings:
return "repeated string"
}
if strings.Contains(goType, "[]") {
t := strings.TrimLeft(goType, "[]")
if strings.Contains(name, t) {
return "repeated " + t
}
}
return goType
}
// MgoFieldToGoStruct convert to go struct
func MgoFieldToGoStruct(name string, fs []*MgoField) string {
var str = ""
var objStr string
for _, f := range fs {
if f.Name == oidName {
str += " ID primitive.ObjectID `bson:\"_id\" json:\"id\"`\n"
continue
}
if f.Type == goTypeInterface || f.Type == goTypeSliceInterface {
f.Type = xstrings.ToCamelCase(f.Name)
}
str += fmt.Sprintf(" %s %s `bson:\"%s\" json:\"%s\"`\n", xstrings.ToCamelCase(f.Name), f.Type, f.Name, f.Name)
if f.ObjectStr != "" {
objStr += f.ObjectStr + "\n"
}
}
return fmt.Sprintf("type %s struct {\n%s}\n\n%s\n", xstrings.ToCamelCase(name), str, objStr)
}
func toSingular(word string) string {
if strings.HasSuffix(word, "es") {
if len(word) > 2 {
return word[:len(word)-2]
}
} else if strings.HasSuffix(word, "s") {
if len(word) > 1 {
return word[:len(word)-1]
}
}
return word
}
func nameToSingular(goTypeStr string, targetObjectStr string, markStr string) string {
name := strings.ReplaceAll(goTypeStr, "[]*", "")
prefixStr := markStr + " " + name
l := len(prefixStr)
if len(targetObjectStr) <= l {
return targetObjectStr
}
if prefixStr == targetObjectStr[:l] {
targetObjectStr = toSingular(prefixStr) + " " + targetObjectStr[l:]
return targetObjectStr
}
return targetObjectStr
}
func convertToSingular(goTypeStr string, objectStr string, protoObjectStr string) (tStr string, oStr string, pObjStr string) {
if !strings.Contains(goTypeStr, "[]*") || objectStr == "" {
return goTypeStr, objectStr, protoObjectStr
}
objectStr = nameToSingular(goTypeStr, objectStr, "type")
protoObjectStr = nameToSingular(goTypeStr, protoObjectStr, "message")
goTypeStr = toSingular(goTypeStr)
return goTypeStr, objectStr, protoObjectStr
}