-
Notifications
You must be signed in to change notification settings - Fork 2
/
dao.go
1139 lines (1054 loc) · 37.1 KB
/
dao.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// MIT License
//
// Copyright (c) 2020 Lack
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package plugin
import (
"errors"
"fmt"
"go/build"
"go/parser"
"go/token"
"io/fs"
"path/filepath"
"sort"
"strings"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"github.com/vine-io/vine/cmd/generator"
"github.com/vine-io/vine/lib/dao/schema"
)
var TagString = "gen"
const (
// message tags
// dao generate flag
_dao = "dao"
_table = "table"
_runtime = "runtime"
// field tags
// inline
_inline = "inline"
// dao primary key
_pk = "pk"
)
type Tag struct {
Key string
Value string
}
// dao is an implementation of the Go protocol buffer compiler's
// plugin architecture. It generates bindings for dao support.
type dao struct {
generator.PluginImports
gen *generator.Generator
schemas []*Schema
regTables map[string]string
aliasTypes map[string]string
aliasFields map[string]*Field
sourcePkg string
ctxPkg generator.Single
fmtPkg generator.Single
timePkg generator.Single
stringPkg generator.Single
errPkg generator.Single
DriverPkg generator.Single
jsonPkg generator.Single
daoPkg generator.Single
clausePkg generator.Single
runtimePkg generator.Single
}
func New() *dao {
return &dao{
schemas: []*Schema{},
regTables: map[string]string{},
aliasTypes: map[string]string{},
aliasFields: map[string]*Field{},
}
}
// Name returns the name of this plugin, "dao".
func (g *dao) Name() string {
return "dao"
}
// Init initializes the plugin.
func (g *dao) Init(gen *generator.Generator) {
g.gen = gen
g.PluginImports = generator.NewPluginImports(g.gen)
}
// Given a type name defined in a .proto, return its object.
// Also record that we're using it, to guarantee the associated import.
func (g *dao) objectNamed(name string) generator.Object {
g.gen.RecordTypeUse(name)
return g.gen.ObjectNamed(name)
}
// Given a type name defined in a .proto, return its name as we will print it.
func (g *dao) typeName(str string) string {
return g.gen.TypeName(g.objectNamed(str))
}
// P forwards to g.gen.P.
func (g *dao) P(args ...interface{}) { g.gen.P(args...) }
// Generate generates code for the services in the given file.
func (g *dao) Generate(file *generator.FileDescriptor) {
if len(file.Comments()) == 0 {
return
}
g.ctxPkg = g.NewImport("context", "context")
g.fmtPkg = g.NewImport("fmt", "fmt")
g.timePkg = g.NewImport("time", "time")
g.stringPkg = g.NewImport("strings", "strings")
g.DriverPkg = g.NewImport("database/sql/driver", "driver")
g.jsonPkg = g.NewImport("github.com/json-iterator/go", "json")
g.errPkg = g.NewImport("errors", "errors")
g.daoPkg = g.NewImport("github.com/vine-io/vine/lib/dao", "dao")
g.clausePkg = g.NewImport("github.com/vine-io/vine/lib/dao/clause", "clause")
g.runtimePkg = g.NewImport("github.com/vine-io/vine/util/runtime", "runtime")
if g.gen.OutPut.Load {
g.sourcePkg = string(g.gen.AddImport(generator.GoImportPath(g.gen.OutPut.SourcePkgPath)))
}
for _, msg := range file.Messages() {
g.wrapSchemas(file, msg)
}
g.generateRegTables(file)
aFields := make([]*Field, 0)
for _, value := range g.aliasFields {
if value.File.GetName() != file.GetName() {
continue
}
aFields = append(aFields, value)
}
sort.Slice(aFields, func(i, j int) bool { return aFields[i].Num < aFields[j].Num })
for _, value := range aFields {
if file.GetOptions().GetGoPackage() != "" && g.gen.OutPut.Out != "" {
f := strings.TrimSuffix(filepath.Base(file.GetName()), ".proto") + ".pb.dao.go"
if g.isContains(filepath.Join(build.Default.GOPATH, "src", g.gen.OutPut.Out), f, "type", value.Alias) {
continue
}
}
g.generateAliasField(file, value)
}
for _, item := range g.schemas {
if item.Desc.Proto.File().GetName() != file.GetName() {
continue
}
g.generateSchema(file, item)
}
}
func (g *dao) wrapSchemas(file *generator.FileDescriptor, msg *generator.MessageDescriptor) {
if msg.Proto.Options != nil && msg.Proto.Options.GetMapEntry() {
return
}
tags := g.extractTags(msg.Comments)
if _, ok := tags[_dao]; !ok {
return
}
table := toTableName(msg.Proto.GetName())
if v, ok := tags[_table]; ok {
table = v.Value
}
s := &Schema{
Name: msg.Proto.GetName() + "S",
Desc: msg,
MFields: map[string]*Field{},
Table: table,
}
if _, ok := tags[_runtime]; ok {
g.regTables[msg.Proto.GetName()] = s.Name
s.Deep = true
}
n := 0
g.buildFields(file, msg, s, &n)
if s.PK == nil {
g.gen.Fail(fmt.Sprintf(`Message:%s missing primary key`, msg.Proto.GetName()))
}
s.Fields = make([]*Field, 0)
for _, item := range s.MFields {
s.Fields = append(s.Fields, item)
}
sort.Slice(s.Fields, func(i, j int) bool { return s.Fields[i].Num < s.Fields[j].Num })
g.schemas = append(g.schemas, s)
}
func (g *dao) buildFields(file *generator.FileDescriptor, m *generator.MessageDescriptor, s *Schema, n *int) {
for _, item := range m.Fields {
fTags := g.extractTags(item.Comments)
_, isInline := fTags[_inline]
if isInline && item.Proto.GetType() == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
subMsg := g.gen.ExtractMessage(item.Proto.GetTypeName())
g.buildFields(file, subMsg, s, n)
continue
}
field := &Field{
Name: generator.CamelCase(item.Proto.GetName()),
Tags: []*FieldTag{},
Desc: item,
File: file,
Num: *n,
}
if item.Proto.IsRepeated() {
alias := generator.CamelCaseSlice([]string{m.Proto.GetName(), item.Proto.GetName()})
if strings.HasSuffix(item.Proto.GetTypeName(), "Entry") {
field.Type = _map
for _, nest := range m.Proto.GetNestedType() {
if strings.HasSuffix(item.Proto.GetTypeName(), nest.GetName()) {
field.Map = &MapFields{}
field.Map.Key, field.Map.Value = nest.GetMapFields()
break
}
}
} else {
field.Type = _slice
field.Slice = field.Desc.Proto
field.IsRepeated = true
}
field.Alias = alias
field.Tags = append(field.Tags, g.buildJSONTags(item), g.buildDaoTags(item, false))
g.aliasFields[alias] = field
} else {
typ, tags, err := g.buildFieldTypeAndTags(item)
if err != nil {
continue
}
field.Type = typ
field.Tags = tags
if item.Proto.GetType() == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
alias := generator.CamelCaseSlice([]string{m.Proto.GetName(), item.Proto.GetName()})
field.Alias = alias
g.aliasFields[alias] = field
}
_, pkExists := fTags[_pk]
if pkExists && s.PK == nil && (strings.ToLower(field.Name) == "id" || strings.ToLower(field.Name) == "uuid") {
s.PK = field
for _, tag := range field.Tags {
if tag.Key == "dao" {
tag.Values = append(tag.Values, "primaryKey")
}
}
}
}
*n += 1
s.MFields[field.Name] = field
}
}
func (g *dao) generateRegTables(_ *generator.FileDescriptor) {
if len(g.regTables) == 0 {
return
}
g.P(`func init() {`)
for k, _ := range g.regTables {
name := g.wrapPkg(k)
pkg := g.runtimePkg.Use()
g.P(fmt.Sprintf(`sets.RegistrySchema(new(%s).GVK(), func(in %s.Object) %s.Schema {`, name, pkg, pkg))
g.P(fmt.Sprintf(`return From%s(in.(*%s))`, k, name))
g.P(`})`)
}
g.P(`}`)
}
func (g *dao) generateAliasField(file *generator.FileDescriptor, field *Field) {
alias := field.Alias
if field.IsRepeated {
// slice, array type
typ, err := g.buildFieldGoType(file, field.Desc.Proto)
if err != nil {
return
}
g.P(fmt.Sprintf(`// %s the alias of []%s`, alias, typ))
g.P(fmt.Sprintf(`type %s []%s`, alias, typ))
}
if field.Type == _map {
if field.Map == nil {
return
}
key, value := field.Map.Key, field.Map.Value
keyString, _ := g.buildFieldGoType(file, key)
valueString, _ := g.buildFieldGoType(file, value)
g.P(fmt.Sprintf(`// %s the alias of map[%s]%s`, alias, keyString, valueString))
g.P(fmt.Sprintf(`type %s map[%s]%s`, alias, keyString, valueString))
}
if field.Type == _point {
var typ string
subMsg := g.extractMessage(field.Desc.Proto.GetTypeName())
if subMsg.Proto.File() == file {
typ = g.wrapPkg(subMsg.Proto.GetName())
} else {
obj := g.gen.ObjectNamed(field.Desc.Proto.GetTypeName())
v, ok := g.gen.ImportMap[obj.GoImportPath().String()]
if !ok {
v = string(g.gen.AddImport(obj.GoImportPath()))
}
typ = fmt.Sprintf("%s.%s", v, subMsg.Proto.GetName())
}
g.P(fmt.Sprintf(`// %s the alias of %s`, alias, typ))
g.P(fmt.Sprintf("type %s %s", alias, typ))
}
g.P()
g.P("// Value return json value, implement driver.Valuer interface")
switch field.Type {
case _slice:
g.P(fmt.Sprintf(`func (m %s) Value() (%s.Value, error) {`, alias, g.DriverPkg.Use()))
g.P("if len(m) == 0 {")
g.P("return nil, nil")
g.P("}")
case _map:
g.P(fmt.Sprintf(`func (m %s) Value() (%s.Value, error) {`, alias, g.DriverPkg.Use()))
g.P("if m == nil {")
g.P("return nil, nil")
g.P("}")
default:
g.P(fmt.Sprintf(`func (m *%s) Value() (%s.Value, error) {`, alias, g.DriverPkg.Use()))
g.P("if m == nil {")
g.P("return nil, nil")
g.P("}")
}
g.P(fmt.Sprintf(`b, err := %s.Marshal(m)`, g.jsonPkg.Use()))
g.P(`return string(b), err`)
g.P("}")
g.P()
g.P("// Scan scan value into Jsonb, implements sql.Scanner interface")
g.P(fmt.Sprintf(`func (m *%s) Scan(value interface{}) error {`, alias))
g.P("var bytes []byte")
g.P("switch v := value.(type) {")
g.P("case []byte:")
g.P("bytes = v")
g.P("case string:")
g.P("bytes = []byte(v)")
g.P("default:")
g.P(fmt.Sprintf(`return %s.New(%s.Sprint("Failed to unmarshal JSONB value:", value))`, g.errPkg.Use(), g.fmtPkg.Use()))
g.P("}")
g.P()
g.P(fmt.Sprintf(`return %s.Unmarshal(bytes, &m)`, g.jsonPkg.Use()))
g.P("}")
g.P()
g.P(fmt.Sprintf(`func (m *%s) DaoDataType() string {`, alias))
g.P(fmt.Sprintf(`return %s.DefaultDialect.JSONDataType()`, g.daoPkg.Use()))
g.P("}")
g.P()
}
func (g *dao) generateSchema(file *generator.FileDescriptor, schema *Schema) {
g.generateSchemaFields(file, schema)
g.generateSchemaIOMethods(file, schema)
g.generateSchemaUtilMethods(file, schema)
g.generateSchemaCURDMethods(file, schema)
}
func (g *dao) generateSchemaFields(file *generator.FileDescriptor, schema *Schema) {
g.P(fmt.Sprintf(`// %s the Schema for %s`, schema.Name, schema.Desc.Proto.GetName()))
g.P("type ", schema.Name, " struct {")
g.P(fmt.Sprintf(`tx *dao.DB %s`, toQuoted(`json:"-" dao:"-"`)))
g.P(fmt.Sprintf(`exprs []%s.Expression %s`, g.clausePkg.Use(), toQuoted(`json:"-" dao:"-"`)))
g.P()
for _, field := range schema.Fields {
switch field.Type {
case _point:
g.P(fmt.Sprintf(`%s *%s %s`, field.Name, field.Alias, MargeTags(field.Tags...)))
case _slice, _map:
g.P(fmt.Sprintf(`%s %s %s`, field.Name, field.Alias, MargeTags(field.Tags...)))
default:
g.P(fmt.Sprintf(`%s %s %s`, field.Name, field.Type, MargeTags(field.Tags...)))
}
}
g.P(fmt.Sprintf(`DeletionTimestamp int64 %s`, toQuoted(`json:"deletionTimestamp,omitempty" dao:"column:deletion_timestamp"`)))
g.P("}")
g.P()
}
func (g *dao) generateSchemaIOMethods(file *generator.FileDescriptor, schema *Schema) {
sname := schema.Name
pname := schema.Desc.Proto.GetName()
g.P(fmt.Sprintf(`func Registry%s() error {`, pname))
g.P(fmt.Sprintf(`return dao.DefaultDialect.Migrator().AutoMigrate(&%s{})`, sname))
g.P("}")
g.P()
g.P(fmt.Sprintf(`func %sBuilder() *%s {`, sname, sname))
g.P(fmt.Sprintf(`return &%s{tx: dao.DefaultDialect.NewTx()}`, sname))
g.P("}")
g.P()
for _, field := range schema.Fields {
switch field.Type {
case _float32, _float64, _int32, _int64, _uint32, _uint64, _string, _bool:
g.P(fmt.Sprintf(`func (m *%s) Set%s(in %s) *%s {`, sname, field.Name, field.Type, sname))
g.P(fmt.Sprintf(`m.%s = in`, field.Name))
g.P(`return m`)
g.P("}")
g.P()
case _map:
key, value := field.Map.Key, field.Map.Value
keyString, _ := g.buildFieldGoType(file, key)
valueString, _ := g.buildFieldGoType(file, value)
g.P(fmt.Sprintf(`func (m *%s) Set%s(in map[%s]%s) *%s {`, sname, field.Name, keyString, valueString, sname))
g.P(fmt.Sprintf(`m.%s = in`, field.Name))
g.P(`return m`)
g.P("}")
g.P()
g.P(fmt.Sprintf(`func (m *%s) Put%s(k %s, v %s) *%s {`, sname, field.Name, keyString, valueString, sname))
g.P(fmt.Sprintf(`if len(m.%s) == 0 {`, field.Name))
g.P(fmt.Sprintf(`m.%s = map[%s]%s{}`, field.Name, keyString, valueString))
g.P(`}`)
g.P(fmt.Sprintf(`m.%s[k] = v`, field.Name))
g.P(`return m`)
g.P("}")
g.P()
g.P(fmt.Sprintf(`func (m *%s) Remove%s(k %s) *%s {`, sname, field.Name, keyString, sname))
g.P(fmt.Sprintf(`if len(m.%s) == 0 {`, field.Name))
g.P(`return m`)
g.P(`}`)
g.P(fmt.Sprintf(`delete(m.%s, k)`, field.Name))
g.P(`return m`)
g.P("}")
g.P()
case _slice:
typ, _ := g.buildFieldGoType(file, field.Slice)
g.P(fmt.Sprintf(`func (m *%s) Set%s(in []%s) *%s {`, sname, field.Name, typ, sname))
g.P(fmt.Sprintf(`m.%s = in`, field.Name))
g.P(`return m`)
g.P("}")
g.P()
g.P(fmt.Sprintf(`func (m *%s) Add%s(in %s) *%s {`, sname, field.Name, typ, sname))
g.P(fmt.Sprintf(`if len(m.%s) == 0 {`, field.Name))
g.P(fmt.Sprintf(`m.%s = []%s{}`, field.Name, typ))
g.P(`}`)
g.P(fmt.Sprintf(`m.%s = append(m.%s, in)`, field.Name, field.Name))
g.P(`return m`)
g.P("}")
g.P()
g.P(fmt.Sprintf(`func (m *%s) Filter%s(fn func(item %s) bool) *%s {`, sname, field.Name, typ, sname))
g.P(fmt.Sprintf(`out := make([]%s, 0)`, typ))
g.P(fmt.Sprintf(`for _, item := range m.%s {`, field.Name))
g.P(`if !fn(item) {`)
g.P(`out = append(out, item)`)
g.P(`}`)
g.P(`}`)
g.P(fmt.Sprintf(`m.%s = out`, field.Name))
g.P(`return m`)
g.P(`}`)
g.P()
g.P(fmt.Sprintf(`func (m *%s) Remove%s(index int) *%s {`, sname, field.Name, sname))
g.P(fmt.Sprintf(`if index < 0 || index >= len(m.%s) {`, field.Name))
g.P(`return m`)
g.P(`}`)
g.P(fmt.Sprintf(`if index == len(m.%s)-1 {`, field.Name))
g.P(fmt.Sprintf(`m.%s = m.%s[:index-1]`, field.Name, field.Name))
g.P(`} else {`)
g.P(fmt.Sprintf(`m.%s = append(m.%s[0:index], m.%s[index+1:]...)`, field.Name, field.Name, field.Name))
g.P(`}`)
g.P(`return m`)
g.P("}")
g.P()
case _point:
var typ string
subMsg := g.extractMessage(field.Desc.Proto.GetTypeName())
if subMsg.Proto.File() == file {
typ = g.wrapPkg(subMsg.Proto.GetName())
} else {
obj := g.gen.ObjectNamed(field.Desc.Proto.GetTypeName())
v, ok := g.gen.ImportMap[obj.GoImportPath().String()]
if !ok {
v = string(g.gen.AddImport(obj.GoImportPath()))
}
typ = fmt.Sprintf(`%s.%s`, v, subMsg.Proto.GetName())
}
g.P(fmt.Sprintf(`func (m *%s) Set%s(in *%s) *%s {`, sname, field.Name, typ, sname))
g.P(fmt.Sprintf(`m.%s = (*%s)(in)`, field.Name, field.Alias))
g.P(`return m`)
g.P("}")
g.P()
}
}
g.P(fmt.Sprintf(`func From%s(in *%s) *%s {`, pname, g.wrapPkg(pname), sname))
g.P(fmt.Sprintf(`out := new(%s)`, sname))
g.P(`out.tx = dao.DefaultDialect.NewTx()`)
for _, field := range schema.Fields {
switch field.Type {
case _float32, _float64, _int32, _int64, _uint32, _uint64:
g.P(fmt.Sprintf(`if in.%s != 0 {`, field.Name))
if field.Desc.Proto.GetType() == descriptor.FieldDescriptorProto_TYPE_ENUM {
g.P(fmt.Sprintf(`out.%s = int32(in.%s)`, field.Name, field.Name))
} else {
g.P(fmt.Sprintf(`out.%s = in.%s`, field.Name, field.Name))
}
g.P("}")
case _bool:
continue
case _map:
g.P(fmt.Sprintf(`if in.%s != nil {`, field.Name))
g.P(fmt.Sprintf(`out.%s = in.%s`, field.Name, field.Name))
g.P("}")
case _string:
if field.Desc.Proto.GetType() == descriptor.FieldDescriptorProto_TYPE_BYTES {
g.P(fmt.Sprintf(`if in.%s != nil {`, field.Name))
g.P(fmt.Sprintf(`out.%s = string(in.%s)`, field.Name, field.Name))
} else {
g.P(fmt.Sprintf(`if in.%s != "" {`, field.Name))
g.P(fmt.Sprintf(`out.%s = in.%s`, field.Name, field.Name))
}
g.P("}")
case _slice:
g.P(fmt.Sprintf(`if len(in.%s) != 0 {`, field.Name))
g.P(fmt.Sprintf(`out.%s = in.%s`, field.Name, field.Name))
g.P("}")
case _point:
g.P(fmt.Sprintf(`if in.%s != nil {`, field.Name))
g.P(fmt.Sprintf(`out.%s = (*%s)(in.%s)`, field.Name, field.Alias, field.Name))
g.P("}")
}
}
g.P("return out")
g.P("}")
g.P()
g.P(fmt.Sprintf(`func (m *%s) To%s() *%s {`, sname, pname, g.wrapPkg(pname)))
g.P(fmt.Sprintf(`out := new(%s)`, g.wrapPkg(pname)))
for _, field := range schema.Fields {
switch field.Type {
case _point:
var typPkg string
obj := g.objectNamed(field.Desc.Proto.GetTypeName())
submsg := g.extractMessage(field.Desc.Proto.GetTypeName())
typPkg = submsg.Proto.GetName()
if obj.File() != file {
typPkg = string(g.gen.AddImport(obj.GoImportPath())) + "." + submsg.Proto.GetName()
} else {
typPkg = g.wrapPkg(submsg.Proto.GetName())
}
g.P(fmt.Sprintf("out.%s = (*%s)(m.%s)", field.Name, typPkg, field.Name))
case _string:
if field.Desc.Proto.GetType() == descriptor.FieldDescriptorProto_TYPE_BYTES {
g.P(fmt.Sprintf(`out.%s = []byte(m.%s)`, field.Name, field.Name))
} else {
g.P(fmt.Sprintf(`out.%s = m.%s`, field.Name, field.Name))
}
default:
if field.Desc.Proto.IsEnum() {
enum := g.gen.ExtractEnum(field.Desc.Proto.GetTypeName())
g.P(fmt.Sprintf(`out.%s = %s(m.%s)`, field.Name, g.wrapPkg(enum.GetName()), field.Name))
} else {
g.P(fmt.Sprintf(`out.%s = m.%s`, field.Name, field.Name))
}
}
}
g.P("return out")
g.P("}")
g.P()
}
func (g *dao) generateSchemaUtilMethods(file *generator.FileDescriptor, schema *Schema) {
g.P("func (", schema.Name, ") TableName() string {")
g.P(`return "`, schema.Table, `"`)
g.P("}")
g.P()
g.P("func (m ", schema.Name, ") PrimaryKey() (string, interface{}, bool) {")
if schema.PK.Type == _string {
g.P(fmt.Sprintf(`return "%s", m.%s, m.%s == ""`, toColumnName(schema.PK.Name), schema.PK.Name, schema.PK.Name))
} else {
g.P(fmt.Sprintf(`return "%s", m.%s, m.%s == 0`, toColumnName(schema.PK.Name), schema.PK.Name, schema.PK.Name))
}
g.P("}")
g.P()
}
func (g *dao) generateSchemaCURDMethods(file *generator.FileDescriptor, schema *Schema) {
source, target := schema.Name, schema.Desc.Proto.GetName()
if schema.Deep {
g.P(fmt.Sprintf(`func (m *%s) FindPage(ctx %s.Context, page, size int32) ([]%s.Object, int64, error) {`, source, g.ctxPkg.Use(), g.runtimePkg.Use()))
} else {
g.P(fmt.Sprintf(`func (m *%s) FindPage(ctx %s.Context, page, size int) ([]*%s, int64, error) {`, source, g.ctxPkg.Use(), g.wrapPkg(target)))
}
g.P(`pk, _, _ := m.PrimaryKey()`)
g.P()
g.P(`m.exprs = append(m.exprs,`)
g.P(fmt.Sprintf(`%s.OrderBy{Columns: []%s.OrderByColumn{{Column: %s.Column{Table: m.TableName(), Name: pk}, Desc: true}}},`, g.clausePkg.Use(), g.clausePkg.Use(), g.clausePkg.Use()))
g.P(fmt.Sprintf(`%s.Cond().Build("deletion_timestamp", 0),`, g.clausePkg.Use()))
g.P(`)`)
g.P()
g.P(`total, err := m.Count(ctx)`)
g.P(`if err != nil {`)
g.P(`return nil, 0, err`)
g.P("}")
g.P()
g.P(fmt.Sprintf(`m.exprs = append(m.exprs, %s.Limit{Offset: int((page - 1) * size), Limit: int(size)})`, g.clausePkg.Use()))
g.P(`data, err := m.findAll(ctx)`)
g.P(`if err != nil {`)
g.P(`return nil, 0, err`)
g.P("}")
g.P()
g.P(`return data, total, nil`)
g.P("}")
g.P()
if schema.Deep {
g.P(fmt.Sprintf(`func (m *%s) FindAll(ctx %s.Context) ([]%s.Object, error) {`, source, g.ctxPkg.Use(), g.runtimePkg.Use()))
} else {
g.P(fmt.Sprintf(`func (m *%s) FindAll(ctx %s.Context) ([]*%s, error) {`, source, g.ctxPkg.Use(), g.wrapPkg(target)))
}
g.P(fmt.Sprintf(`m.exprs = append(m.exprs, %s.Cond().Build("deletion_timestamp", 0))`, g.clausePkg.Use()))
g.P(`return m.findAll(ctx)`)
g.P("}")
g.P()
if schema.Deep {
g.P(fmt.Sprintf(`func (m *%s) FindPureAll(ctx %s.Context) ([]%s.Object, error) {`, source, g.ctxPkg.Use(), g.runtimePkg.Use()))
} else {
g.P(fmt.Sprintf(`func (m *%s) FindPureAll(ctx %s.Context) ([]*%s, error) {`, source, g.ctxPkg.Use(), g.wrapPkg(target)))
}
g.P(`return m.findAll(ctx)`)
g.P("}")
g.P()
if schema.Deep {
g.P(fmt.Sprintf(`func (m *%s) findAll(ctx %s.Context) ([]%s.Object, error) {`, source, g.ctxPkg.Use(), g.runtimePkg.Use()))
} else {
g.P(fmt.Sprintf(`func (m *%s) findAll(ctx %s.Context) ([]*%s, error) {`, source, g.ctxPkg.Use(), g.wrapPkg(target)))
}
g.P(fmt.Sprintf(`dest := make([]*%s, 0)`, source))
g.P(fmt.Sprintf(`tx := m.tx.Session(&%s.Session{}).Table(m.TableName()).WithContext(ctx)`, g.daoPkg.Use()))
g.P()
g.P(`clauses := append(m.extractClauses(tx), m.exprs...)`)
g.P(`if err := tx.Clauses(clauses...).Find(&dest).Error; err != nil {`)
g.P("return nil, err")
g.P("}")
g.P()
if schema.Deep {
g.P(fmt.Sprintf(`outs := make([]%s.Object, len(dest))`, g.runtimePkg.Use()))
} else {
g.P(fmt.Sprintf(`outs := make([]*%s, len(dest))`, g.wrapPkg(target)))
}
g.P(`for i := range dest {`)
g.P(fmt.Sprintf(`outs[i] = dest[i].To%s()`, target))
g.P("}")
g.P()
g.P(fmt.Sprintf(`return outs, nil`))
g.P("}")
g.P()
g.P(fmt.Sprintf(`func (m *%s) Count(ctx %s.Context) (total int64, err error) {`, source, g.ctxPkg.Use()))
g.P(fmt.Sprintf(`tx := m.tx.Session(&%s.Session{}).Table(m.TableName()).WithContext(ctx)`, g.daoPkg.Use()))
g.P()
g.P(fmt.Sprintf(`clauses := append(m.extractClauses(tx), %s.Cond().Build("deletion_timestamp", 0))`, g.clausePkg.Use()))
g.P(`clauses = append(clauses, m.exprs...)`)
g.P()
g.P(`err = tx.Clauses(clauses...).Count(&total).Error`)
g.P(`return`)
g.P("}")
g.P()
if schema.Deep {
g.P(fmt.Sprintf(`func (m *%s) FindOne(ctx %s.Context) (%s.Object, error) {`, source, g.ctxPkg.Use(), g.runtimePkg.Use()))
} else {
g.P(fmt.Sprintf(`func (m *%s) FindOne(ctx %s.Context) (*%s, error) {`, source, g.ctxPkg.Use(), g.wrapPkg(target)))
}
g.P(fmt.Sprintf(`tx := m.tx.Session(&%s.Session{}).Table(m.TableName()).WithContext(ctx)`, g.daoPkg.Use()))
g.P()
g.P(fmt.Sprintf(`clauses := append(m.extractClauses(tx), %s.Cond().Build("deletion_timestamp", 0))`, g.clausePkg.Use()))
g.P(`clauses = append(clauses, m.exprs...)`)
g.P()
g.P(`if err := tx.Clauses(clauses...).First(&m).Error; err != nil {`)
g.P("return nil, err")
g.P("}")
g.P()
g.P(fmt.Sprintf(`return m.To%s(), nil`, target))
g.P("}")
g.P()
if schema.Deep {
g.P(fmt.Sprintf(`func (m *%s) FindPureOne(ctx %s.Context) (%s.Object, error) {`, source, g.ctxPkg.Use(), g.runtimePkg.Use()))
} else {
g.P(fmt.Sprintf(`func (m *%s) FindPureOne(ctx %s.Context) (*%s, error) {`, source, g.ctxPkg.Use(), g.wrapPkg(target)))
}
g.P(fmt.Sprintf(`tx := m.tx.Session(&%s.Session{}).Table(m.TableName()).WithContext(ctx)`, g.daoPkg.Use()))
g.P()
g.P(`if err := tx.Clauses(m.exprs...).First(&m).Error; err != nil {`)
g.P("return nil, err")
g.P("}")
g.P()
g.P(fmt.Sprintf(`return m.To%s(), nil`, target))
g.P("}")
g.P()
if schema.Deep {
g.P(fmt.Sprintf(`func (m *%s) Cond(exprs ...%s.Expression) %s.Schema {`, source, g.clausePkg.Use(), g.runtimePkg.Use()))
} else {
g.P(fmt.Sprintf(`func (m *%s) Cond(exprs ...%s.Expression) *%s {`, source, g.clausePkg.Use(), source))
}
g.P(`m.exprs = append(m.exprs, exprs...)`)
g.P(`return m`)
g.P(`}`)
g.P()
g.P(fmt.Sprintf(`func (m *%s) extractClauses(tx *%s.DB) []%s.Expression {`, source, g.daoPkg.Use(), g.clausePkg.Use()))
g.P(`exprs := make([]clause.Expression, 0)`)
for _, field := range schema.Fields {
column := toColumnName(field.Name)
switch field.Type {
case _map:
g.P(fmt.Sprintf(`if m.%s != nil {`, field.Name))
g.P(fmt.Sprintf(`for k, v := range m.%s {`, field.Name))
switch field.Map.Key.GetType() {
case descriptor.FieldDescriptorProto_TYPE_STRING:
g.P(fmt.Sprintf(`exprs = append(exprs, %s.DefaultDialect.JSONBuild("%s").Tx(tx).Op(%s.JSONEq, v, k))`, g.daoPkg.Use(), column, g.daoPkg.Use()))
case descriptor.FieldDescriptorProto_TYPE_INT32, descriptor.FieldDescriptorProto_TYPE_INT64:
g.P(fmt.Sprintf(`exprs = append(exprs, %s.DefaultDialect.JSONBuild("%s").Tx(tx).Op(%s.JSONEq, v, %s))`, g.daoPkg.Use(), column, g.daoPkg.Use(), `fmt.Sprintf("%d", k)`))
}
g.P("}")
g.P("}")
case _point:
g.P(fmt.Sprintf(`if m.%s != nil {`, field.Name))
g.P(fmt.Sprintf(`for k, v := range dao.FieldPatch(m.%s) {`, field.Name))
g.P(`if v == nil {`)
g.P(fmt.Sprintf(`exprs = append(exprs, %s.DefaultDialect.JSONBuild("%s").Tx(tx).Op(%s.JSONHasKey, "", %s.Split(k, ".")...))`, g.daoPkg.Use(), column, g.daoPkg.Use(), g.stringPkg.Use()))
g.P(`} else {`)
g.P(fmt.Sprintf(`exprs = append(exprs, %s.DefaultDialect.JSONBuild("%s").Tx(tx).Op(%s.JSONEq, v, %s.Split(k, ".")...))`, g.daoPkg.Use(), column, g.daoPkg.Use(), g.stringPkg.Use()))
g.P("}")
g.P("}")
g.P("}")
case _slice:
g.P(fmt.Sprintf(`if len(m.%s) != 0 {`, field.Name))
g.P(fmt.Sprintf(`for _, item := range m.%s {`, field.Name))
switch field.Slice.GetType() {
case descriptor.FieldDescriptorProto_TYPE_STRING:
g.P(fmt.Sprintf(`exprs = append(exprs, %s.DefaultDialect.JSONBuild("%s").Tx(tx).Contains(%s.JSONEq, item))`, g.daoPkg.Use(), column, g.daoPkg.Use()))
case descriptor.FieldDescriptorProto_TYPE_UINT32,
descriptor.FieldDescriptorProto_TYPE_UINT64,
descriptor.FieldDescriptorProto_TYPE_INT32,
descriptor.FieldDescriptorProto_TYPE_INT64,
descriptor.FieldDescriptorProto_TYPE_SFIXED32,
descriptor.FieldDescriptorProto_TYPE_SFIXED64,
descriptor.FieldDescriptorProto_TYPE_FIXED32,
descriptor.FieldDescriptorProto_TYPE_FIXED64:
g.P(fmt.Sprintf(`exprs = append(exprs, %s.DefaultDialect.JSONBuild("%s").Tx(tx).Contains(%s.JSONEq, item))`, g.daoPkg.Use(), column, g.daoPkg.Use()))
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
g.P(`for k, v := range dao.FieldPatch(item) {`)
g.P(`if v != nil {`)
g.P(fmt.Sprintf(`exprs = append(exprs, %s.DefaultDialect.JSONBuild("%s").Tx(tx).Contains(%s.JSONEq, v, %s.Split(k, ".")...))`, g.daoPkg.Use(), column, g.daoPkg.Use(), g.stringPkg.Use()))
g.P(`}`)
g.P("}")
}
g.P("}")
g.P("}")
case _string:
g.P(fmt.Sprintf(`if m.%s != "" {`, field.Name))
g.P(fmt.Sprintf(`exprs = append(exprs, %s.Cond().Build("%s", m.%s))`, g.clausePkg.Use(), column, field.Name))
g.P("}")
case _bool:
continue
default:
g.P(fmt.Sprintf(`if m.%s != 0 {`, field.Name))
g.P(fmt.Sprintf(`exprs = append(exprs, %s.Cond().Build("%s", m.%s))`, g.clausePkg.Use(), column, field.Name))
g.P("}")
}
}
g.P()
g.P(`return exprs`)
g.P("}")
g.P()
if schema.Deep {
g.P(fmt.Sprintf(`func (m *%s) Create(ctx %s.Context) (%s.Object, error) {`, source, g.ctxPkg.Use(), g.runtimePkg.Use()))
} else {
g.P(fmt.Sprintf(`func (m *%s) Create(ctx %s.Context) (*%s, error) {`, source, g.ctxPkg.Use(), g.wrapPkg(target)))
}
g.P(fmt.Sprintf(`tx := m.tx.Session(&%s.Session{}).Table(m.TableName()).WithContext(ctx)`, g.daoPkg.Use()))
g.P()
g.P(`if err := tx.Create(m).Error; err != nil {`)
g.P("return nil, err")
g.P("}")
g.P()
g.P(fmt.Sprintf(`return m.To%s(), nil`, target))
g.P("}")
g.P()
g.P(fmt.Sprintf(`func (m *%s) BatchUpdates(ctx %s.Context) error {`, source, g.ctxPkg.Use()))
g.P(`if len(m.exprs) == 0 {`)
g.P(fmt.Sprintf(`return %s.New("missing conditions")`, g.errPkg.Use()))
g.P("}")
g.P()
g.P(fmt.Sprintf(`tx := m.tx.Session(&%s.Session{}).Table(m.TableName()).WithContext(ctx)`, g.daoPkg.Use()))
g.P()
g.P(`values := make(map[string]interface{}, 0)`)
for _, field := range schema.Fields {
column := toColumnName(field.Name)
if field == schema.PK {
continue
}
switch field.Type {
case _point, _map:
g.P(fmt.Sprintf(`if m.%s != nil {`, field.Name))
g.P(fmt.Sprintf(`values["%s"] = m.%s`, column, field.Name))
g.P("}")
case _slice:
g.P(fmt.Sprintf(`if len(m.%s) != 0 {`, field.Name))
g.P(fmt.Sprintf(`values["%s"] = m.%s`, column, field.Name))
g.P("}")
case _string:
g.P(fmt.Sprintf(`if m.%s != "" {`, field.Name))
g.P(fmt.Sprintf(`values["%s"] = m.%s`, column, field.Name))
g.P("}")
case _bool:
continue
default:
g.P(fmt.Sprintf(`if m.%s != 0 {`, field.Name))
g.P(fmt.Sprintf(`values["%s"] = m.%s`, column, field.Name))
g.P("}")
}
}
g.P()
g.P(`return tx.Clauses(m.exprs...).Updates(values).Error`)
g.P("}")
g.P()
if schema.Deep {
g.P(fmt.Sprintf(`func (m *%s) Updates(ctx %s.Context) (%s.Object, error) {`, source, g.ctxPkg.Use(), g.runtimePkg.Use()))
} else {
g.P(fmt.Sprintf(`func (m *%s) Updates(ctx %s.Context) (*%s, error) {`, source, g.ctxPkg.Use(), g.wrapPkg(target)))
}
g.P(`pk, pkv, isNil := m.PrimaryKey()`)
g.P(`if isNil {`)
g.P(fmt.Sprintf(`return nil, %s.New("missing primary key")`, g.errPkg.Use()))
g.P("}")
g.P()
g.P(fmt.Sprintf(`tx := m.tx.Session(&%s.Session{}).Table(m.TableName()).WithContext(ctx).Where(pk+" = ?", pkv)`, g.daoPkg.Use()))
g.P()
g.P(`values := make(map[string]interface{}, 0)`)
for _, field := range schema.Fields {
column := toColumnName(field.Name)
if field == schema.PK {
continue
}
switch field.Type {
case _point, _map:
g.P(fmt.Sprintf(`if m.%s != nil {`, field.Name))
g.P(fmt.Sprintf(`values["%s"] = m.%s`, column, field.Name))
g.P("}")
case _slice:
g.P(fmt.Sprintf(`if len(m.%s) != 0 {`, field.Name))
g.P(fmt.Sprintf(`values["%s"] = m.%s`, column, field.Name))
g.P("}")
case _string:
g.P(fmt.Sprintf(`if m.%s != "" {`, field.Name))
g.P(fmt.Sprintf(`values["%s"] = m.%s`, column, field.Name))
g.P("}")
case _bool:
continue
default:
g.P(fmt.Sprintf(`if m.%s != 0 {`, field.Name))
g.P(fmt.Sprintf(`values["%s"] = m.%s`, column, field.Name))
g.P("}")
}
}
g.P()
g.P(`if err := tx.Updates(values).Error; err != nil {`)
g.P("return nil, err")
g.P("}")
g.P()
g.P(fmt.Sprintf(`err := m.tx.Session(&%s.Session{}).Table(m.TableName()).WithContext(ctx).Where(pk+" = ?", pkv).First(m).Error`, g.daoPkg.Use()))
g.P("if err != nil {")
g.P(`return nil, err`)
g.P("}")
g.P(fmt.Sprintf(`return m.To%s(), nil`, target))
g.P("}")
g.P()
g.P(fmt.Sprintf(`func (m *%s) BatchDelete(ctx %s.Context, soft bool) error {`, source, g.ctxPkg.Use()))
g.P(`if len(m.exprs) == 0 {`)
g.P(fmt.Sprintf(`return %s.New("missing conditions")`, g.errPkg.Use()))
g.P("}")
g.P()
g.P(`tx := m.tx.Session(&dao.Session{}).Table(m.TableName()).WithContext(ctx)`)
g.P()
g.P(`if soft {`)
g.P(fmt.Sprintf(`return tx.Clauses(m.exprs...).Updates(map[string]interface{}{"deletion_timestamp": %s.Now().UnixNano()}).Error`, g.timePkg.Use()))
g.P(`}`)
g.P(fmt.Sprintf(`return tx.Clauses(m.exprs...).Delete(&%s{}).Error`, source))
g.P("}")
g.P()
g.P(fmt.Sprintf(`func (m *%s) Delete(ctx %s.Context, soft bool) error {`, source, g.ctxPkg.Use()))
g.P(`pk, pkv, isNil := m.PrimaryKey()`)
g.P(`if isNil {`)
g.P(fmt.Sprintf(`return %s.New("missing primary key")`, g.errPkg.Use()))
g.P("}")
g.P()
g.P(fmt.Sprintf(`tx := m.tx.Session(&%s.Session{}).Table(m.TableName()).WithContext(ctx)`, g.daoPkg.Use()))
g.P()
g.P(`if soft {`)
g.P(fmt.Sprintf(`return tx.Where(pk+" = ?", pkv).Updates(map[string]interface{}{"deletion_timestamp": %s.Now().UnixNano()}).Error`, g.timePkg.Use()))
g.P(`}`)
g.P(fmt.Sprintf(`return tx.Where(pk+" = ?", pkv).Delete(&%s{}).Error`, source))
g.P("}")
g.P()
g.P(fmt.Sprintf(`func (m *%s) Tx(ctx %s.Context) *dao.DB {`, source, g.ctxPkg.Use()))
g.P(fmt.Sprintf(`return m.tx.Session(&%s.Session{}).Table(m.TableName()).WithContext(ctx).Clauses(m.exprs...)`, g.daoPkg.Use()))
g.P("}")
g.P()
}
func (g *dao) buildFieldTypeAndTags(field *generator.FieldDescriptor) (fieldType, []*FieldTag, error) {
var (
typ fieldType
incre bool
tags = make([]*FieldTag, 0)
)
tags = append(tags, g.buildJSONTags(field))
switch field.Proto.GetType() {
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
typ, incre = _float64, false
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
typ, incre = _float32, false
case descriptor.FieldDescriptorProto_TYPE_SFIXED32,
descriptor.FieldDescriptorProto_TYPE_INT32,
descriptor.FieldDescriptorProto_TYPE_ENUM:
typ, incre = _int32, true
case descriptor.FieldDescriptorProto_TYPE_SFIXED64,
descriptor.FieldDescriptorProto_TYPE_INT64:
typ, incre = _int64, true
case descriptor.FieldDescriptorProto_TYPE_FIXED32,
descriptor.FieldDescriptorProto_TYPE_UINT32:
typ, incre = _uint32, true
case descriptor.FieldDescriptorProto_TYPE_FIXED64,
descriptor.FieldDescriptorProto_TYPE_UINT64:
typ, incre = _uint64, true
case descriptor.FieldDescriptorProto_TYPE_STRING,
descriptor.FieldDescriptorProto_TYPE_BYTES:
typ, incre = _string, false
case descriptor.FieldDescriptorProto_TYPE_BOOL:
typ, incre = _bool, false
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
typ, incre = _point, false
default:
return "", nil, errors.New("invalid field type")
}
tags = append(tags, g.buildDaoTags(field, incre))
return typ, tags, nil
}
func (g *dao) buildJSONTags(field *generator.FieldDescriptor) *FieldTag {
fTag := &FieldTag{Key: "json", Values: []string{}, Seq: ","}
fTag.Values = append(fTag.Values, field.Proto.GetJsonName(), "omitempty")
return fTag
}
func (g *dao) buildDaoTags(field *generator.FieldDescriptor, autoIncr bool) *FieldTag {
fTag := &FieldTag{Key: "dao", Values: []string{}, Seq: ";"}
fTag.Values = append(fTag.Values, fmt.Sprintf(`column:%s`, toColumnName(field.Proto.GetName())))
for _, tag := range g.extractTags(field.Comments) {
// TODO: parse dao tags
switch tag.Key {
case _pk: