This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmongoschema.go
393 lines (352 loc) · 8.05 KB
/
mongoschema.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
392
393
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"go/format"
"os"
"regexp"
"strings"
"time"
"unicode"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var errEmptyURL = errors.New("mongoschema: no URL specified")
func main() {
var generator Generator
flag.StringVar(&generator.URL, "url", "", "mongo url for dial")
flag.StringVar(&generator.DB, "db", "", "database to use")
flag.StringVar(&generator.Collection, "collection", "", "collection to use")
flag.StringVar(&generator.Struct, "struct", "", "name of the struct")
flag.StringVar(&generator.Package, "package", "", "pkg for the generate code")
flag.BoolVar(&generator.Raw, "raw", false, "output pre-gofmt code")
flag.BoolVar(&generator.Comments, "comments", true, "output comments in code")
flag.UintVar(&generator.Limit, "limit", 0, "maximum number of documents to scan")
ignoredKeys := flag.String("ignored-keys", "", "comma separated list of key names to ignore")
flag.Parse()
generator.IgnoredKeys = strings.Split(*ignoredKeys, ",")
if err := generator.Generate(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
}
type Generator struct {
URL string
DB string
Collection string
Package string
Struct string
Raw bool
Comments bool
IgnoredKeys []string
Limit uint
}
func (s *Generator) connect() (*mgo.Session, *mgo.Collection, error) {
if s.URL == "" {
return nil, nil, errEmptyURL
}
session, err := mgo.Dial(s.URL)
if err != nil {
return nil, nil, err
}
session.EnsureSafe(&mgo.Safe{})
session.SetBatch(1000)
session.SetMode(mgo.Eventual, true)
return session, session.DB(s.DB).C(s.Collection), nil
}
func (s *Generator) Generate() error {
session, collection, err := s.connect()
if err != nil {
return err
}
root := StructType{}
iter := collection.Find(nil).Iter()
m := bson.M{}
var seen uint
for iter.Next(m) {
if s.Limit != 0 && seen == s.Limit {
break
}
root.Merge(NewType(m, s), s)
m = bson.M{}
seen++
}
if err := iter.Close(); err != nil {
return err
}
session.Close()
const srcFmt = "package %s\ntype %s %s"
src := fmt.Sprintf(srcFmt, s.Package, s.Struct, root.GoType(s))
if s.Raw {
fmt.Println(src)
}
formatted, err := format.Source([]byte(src))
if err != nil {
return err
}
fmt.Printf("%s\n", formatted)
return nil
}
type Type interface {
GoType(gen *Generator) string
Merge(t Type, gen *Generator) Type
}
type LiteralType struct {
Literal string
}
func (l LiteralType) GoType(gen *Generator) string {
return l.Literal
}
func (l LiteralType) Merge(t Type, gen *Generator) Type {
if l.GoType(gen) == t.GoType(gen) {
return l
}
return MixedType{l, t}
}
var NilType = LiteralType{Literal: "nil"}
type MixedType []Type
func (m MixedType) GoType(gen *Generator) string {
if !gen.Comments {
return "interface{}"
}
var b bytes.Buffer
fmt.Fprint(&b, "interface{} /* ")
for i, v := range m {
fmt.Fprint(&b, v.GoType(gen))
if i != len(m)-1 {
fmt.Fprint(&b, ",")
}
fmt.Fprint(&b, " ")
}
fmt.Fprint(&b, " */")
return b.String()
}
func (m MixedType) Merge(t Type, gen *Generator) Type {
for _, e := range m {
if e.GoType(gen) == t.GoType(gen) {
return m
}
}
return append(m, t)
}
type PrimitiveType uint
const (
PrimitiveBinary PrimitiveType = iota
PrimitiveBool
PrimitiveDouble
PrimitiveInt32
PrimitiveInt64
PrimitiveObjectId
PrimitiveString
PrimitiveTimestamp
)
func (p PrimitiveType) GoType(gen *Generator) string {
switch p {
case PrimitiveBinary:
return "bson.Binary"
case PrimitiveBool:
return "bool"
case PrimitiveDouble:
return "float64"
case PrimitiveInt32:
return "int32"
case PrimitiveInt64:
return "int64"
case PrimitiveString:
return "string"
case PrimitiveTimestamp:
return "time.Time"
case PrimitiveObjectId:
return "bson.ObjectId"
}
panic(fmt.Sprintf("unknown primitive: %d", uint(p)))
}
func (p PrimitiveType) Merge(t Type, gen *Generator) Type {
if p.GoType(gen) == t.GoType(gen) {
return p
}
return MixedType{p, t}
}
type SliceType struct {
Type
}
func (s SliceType) GoType(gen *Generator) string {
return fmt.Sprintf("[]%s", s.Type.GoType(gen))
}
func (s SliceType) Merge(t Type, gen *Generator) Type {
if s.GoType(gen) == t.GoType(gen) {
return s
}
// If the target type is a slice of structs, we merge into the first struct
// type in our own slice type.
if targetSliceType, ok := t.(SliceType); ok {
if targetSliceStructType, ok := targetSliceType.Type.(StructType); ok {
// We're a slice of structs.
if ownSliceStructType, ok := s.Type.(StructType); ok {
s.Type = ownSliceStructType.Merge(targetSliceStructType, gen)
return s
}
// We're a slice of mixed types, one of which may or may not be a struct.
if sliceMixedType, ok := s.Type.(MixedType); ok {
for i, v := range sliceMixedType {
if vStructType, ok := v.(StructType); ok {
sliceMixedType[i] = vStructType.Merge(targetSliceStructType, gen)
return s
}
}
return SliceType{Type: append(sliceMixedType, targetSliceStructType)}
}
}
}
return MixedType{s, t}
}
type StructType map[string]Type
func (s StructType) GoType(gen *Generator) string {
var buf bytes.Buffer
fmt.Fprintln(&buf, "struct {")
for k, v := range s {
if isValidFieldName(k) {
var vGoType string
if sscontains(gen.IgnoredKeys, k) {
vGoType = "IgnoredField"
} else {
vGoType = v.GoType(gen)
}
fmt.Fprintf(
&buf,
"%s %s `bson:\"%s,omitempty\"`\n",
makeFieldName(k),
vGoType,
k,
)
} else {
if gen.Comments {
fmt.Fprintf(&buf, "// skipping invalid field name %s\n", k)
}
}
}
fmt.Fprint(&buf, "}")
return buf.String()
}
func (s StructType) Merge(t Type, gen *Generator) Type {
if o, ok := t.(StructType); ok {
for k, v := range o {
if e, ok := s[k]; ok {
s[k] = e.Merge(v, gen)
} else {
s[k] = v
}
}
return s
}
return MixedType{s, t}
}
func NewType(v interface{}, gen *Generator) Type {
switch i := v.(type) {
default:
panic(fmt.Sprintf("cannot determine type for %v with go type %T", v, v))
case nil:
return NilType
case bson.ObjectId:
return PrimitiveObjectId
case bson.M:
return NewStructType(i, gen)
case []interface{}:
if len(i) == 0 {
return SliceType{Type: MixedType{}}
}
var s Type
for _, v := range i {
vt := NewType(v, gen)
if vt == NilType {
continue
}
if s == nil {
s = SliceType{Type: vt}
} else {
s.Merge(SliceType{Type: vt}, gen)
}
}
if s == nil {
return SliceType{Type: MixedType{}}
}
return s
case int, int64:
return PrimitiveInt64
case int32:
return PrimitiveInt32
case bool:
return PrimitiveBool
case string:
return PrimitiveString
case time.Time, bson.MongoTimestamp:
return PrimitiveTimestamp
case float32, float64:
return PrimitiveDouble
case bson.Binary:
return PrimitiveBinary
}
}
func NewStructType(m bson.M, gen *Generator) Type {
s := StructType{}
for k, v := range m {
t := NewType(v, gen)
if t == NilType {
continue
}
s[k] = t
}
return s
}
func isValidFieldName(n string) bool {
if n == "" {
return false
}
if strings.IndexAny(n, "!*") == -1 {
return true
}
return false
}
var (
dashUnderscoreReplacer = strings.NewReplacer("-", " ", "_", " ")
capsRe = regexp.MustCompile(`([A-Z])`)
spaceRe = regexp.MustCompile(`(\w+)`)
forcedUpperCase = map[string]bool{"id": true, "url": true, "api": true}
)
func split(str string) []string {
str = dashUnderscoreReplacer.Replace(str)
str = capsRe.ReplaceAllString(str, " $1")
return spaceRe.FindAllString(str, -1)
}
func makeFieldName(s string) string {
parts := split(s)
for i, part := range parts {
if forcedUpperCase[strings.ToLower(part)] {
parts[i] = strings.ToUpper(part)
} else {
parts[i] = strings.Title(part)
}
}
camel := strings.Join(parts, "")
runes := []rune(camel)
for i, c := range runes {
ok := unicode.IsLetter(c) || unicode.IsDigit(c)
if i == 0 {
ok = unicode.IsLetter(c)
}
if !ok {
runes[i] = '_'
}
}
return string(runes)
}
func sscontains(l []string, v string) bool {
for _, e := range l {
if e == v {
return true
}
}
return false
}