1
1
package internal
2
2
3
3
import (
4
+ "fmt"
4
5
"runtime/debug"
6
+ "slices"
7
+ "strings"
5
8
6
9
"github.com/averak/protobq/internal/protobuf/protobq"
7
10
"google.golang.org/protobuf/compiler/protogen"
8
11
"google.golang.org/protobuf/proto"
9
12
)
10
13
11
- //goland:noinspection GoSnakeCaseUsage
12
14
var (
13
15
timeIdents = struct {
14
- Duration protogen.GoIdent
16
+ Minute protogen.GoIdent
15
17
}{
16
- Duration : protogen .GoImportPath ("time" ).Ident ("Duration" ),
17
- }
18
- protoIdents = struct {
19
- GetExtension protogen.GoIdent
20
- }{
21
- GetExtension : protogen .GoImportPath ("google.golang.org/protobuf/proto" ).Ident ("GetExtension" ),
22
- }
23
- internalIdents = struct {
24
- MaterializedView protogen.GoIdent
25
- E_MaterializedView protogen.GoIdent
26
- }{
27
- MaterializedView : protogen .GoImportPath ("github.com/averak/protobq/internal/protobuf/protobq" ).Ident ("MaterializedView" ),
28
- E_MaterializedView : protogen .GoImportPath ("github.com/averak/protobq/internal/protobuf/protobq" ).Ident ("E_MaterializedView" ),
18
+ Minute : protogen .GoImportPath ("time" ).Ident ("Minute" ),
29
19
}
30
20
protobqIdents = struct {
31
21
MaterializedView protogen.GoIdent
32
22
MaterializedViewOptions protogen.GoIdent
23
+ InsertDTO protogen.GoIdent
24
+ internal struct {
25
+ NewInsertDTOImpl protogen.GoIdent
26
+ BQField protogen.GoIdent
27
+ }
33
28
}{
34
29
MaterializedView : protogen .GoImportPath ("github.com/averak/protobq" ).Ident ("MaterializedView" ),
35
30
MaterializedViewOptions : protogen .GoImportPath ("github.com/averak/protobq" ).Ident ("MaterializedViewOptions" ),
31
+ InsertDTO : protogen .GoImportPath ("github.com/averak/protobq" ).Ident ("InsertDTO" ),
32
+ internal : struct {
33
+ NewInsertDTOImpl protogen.GoIdent
34
+ BQField protogen.GoIdent
35
+ }{
36
+ NewInsertDTOImpl : protogen .GoImportPath ("github.com/averak/protobq/internal" ).Ident ("NewInsertDTOImpl" ),
37
+ BQField : protogen .GoImportPath ("github.com/averak/protobq/internal" ).Ident ("BQField" ),
38
+ },
36
39
}
37
40
)
38
41
@@ -69,17 +72,38 @@ func (g CodeGenerator) Gen() error {
69
72
if ! g .isMaterializedViewSchema (msg ) {
70
73
continue
71
74
}
75
+ ext , _ := proto .GetExtension (msg .Desc .Options (), protobq .E_MaterializedView ).(* protobq.MaterializedView )
72
76
73
77
gf .P ("var _ " , protobqIdents .MaterializedView , " = (*" , msg .GoIdent .GoName , ")(nil)" )
74
78
gf .P ()
79
+
80
+ gf .P ("func (mv *" , msg .GoIdent .GoName , ") Name() string {" )
81
+ gf .P (" return \" " , msg .Desc .Name (), "\" " )
82
+ gf .P ("}" )
83
+ gf .P ()
84
+
75
85
gf .P ("func (mv *" , msg .GoIdent .GoName , ") Options() " , protobqIdents .MaterializedViewOptions , " {" )
76
- gf .P (" ext, _ := " , protoIdents .GetExtension , "(mv.ProtoReflect().Descriptor().Options(), " , internalIdents .E_MaterializedView , ").(*" , internalIdents .MaterializedView , ")" )
77
86
gf .P (" return " , protobqIdents .MaterializedViewOptions , "{" )
78
- gf .P (" EnableRefresh: ext.GetEnableRefresh()," )
79
- gf .P (" RefreshInterval: " , timeIdents . Duration , "( ext.GetRefreshIntervalMinutes()) * time .Minute," )
87
+ gf .P (" EnableRefresh: " , ext .GetEnableRefresh (), " ," )
88
+ gf .P (" RefreshInterval: " , ext .GetRefreshIntervalMinutes (), " * " , timeIdents .Minute , " ," )
80
89
gf .P (" }" )
81
90
gf .P ("}" )
82
91
gf .P ()
92
+
93
+ gf .P ("func (mv *" , msg .GoIdent .GoName , ") InsertDTO() " , protobqIdents .InsertDTO , " {" )
94
+ gf .P (" res := " , protobqIdents .internal .NewInsertDTOImpl , "(\" " , ext .GetBaseTable (), "\" , nil)" )
95
+ for _ , field := range msg .Fields {
96
+ g .generateAddField (gf , field , nil , "res" , "mv" )
97
+ }
98
+ gf .P (" return res" )
99
+ gf .P ("}" )
100
+ gf .P ()
101
+
102
+ //for _, field := range msg.Fields {
103
+ //fieldExt, _ := proto.GetExtension(field.Desc.Options(), protobq.E_MaterializedViewField).(*protobq.MaterializedViewField)
104
+ //if fieldExt != nil {
105
+ // gf.P(" res[\"", field.Desc.Name(), "\"] = mv.", field.GoName)
106
+ //}
83
107
}
84
108
}
85
109
return nil
@@ -100,9 +124,31 @@ func (g CodeGenerator) isMaterializedViewSchema(msg *protogen.Message) bool {
100
124
return false
101
125
}
102
126
103
- ext , ok := proto .GetExtension (opts , protobq .E_MaterializedView ).(* protobq.MaterializedView )
104
- if ! ok {
105
- return false
127
+ ext , _ := proto .GetExtension (opts , protobq .E_MaterializedView ).(* protobq.MaterializedView )
128
+ return ext != nil
129
+ }
130
+
131
+ func (g CodeGenerator ) generateAddField (gf * protogen.GeneratedFile , field * protogen.Field , parentPaths []string , result string , receiver string ) {
132
+ ext , _ := proto .GetExtension (field .Desc .Options (), protobq .E_MaterializedViewField ).(* protobq.MaterializedViewField )
133
+ if ext == nil {
134
+ ext = & protobq.MaterializedViewField {}
135
+ }
136
+
137
+ paths := parentPaths
138
+ if len (ext .GetOriginPath ()) > 0 {
139
+ paths = append (paths , ext .GetOriginPath ()... )
140
+ } else {
141
+ paths = append (paths , string (field .Desc .Name ()))
142
+ }
143
+
144
+ blacklist := []string {
145
+ "google.protobuf.Timestamp" ,
146
+ }
147
+ if field .Message != nil && ! slices .Contains (blacklist , string (field .Message .Desc .FullName ())) {
148
+ for _ , nestedField := range field .Message .Fields {
149
+ g .generateAddField (gf , nestedField , paths , result , receiver + ".Get" + field .GoName + "()" )
150
+ }
151
+ } else {
152
+ gf .P (result , ".AddField(" , protobqIdents .internal .BQField , "{[]string{" , fmt .Sprintf (`"%s"` , strings .Join (paths , `", "` )), "}, " , receiver , ".Get" , field .GoName , "()})" )
106
153
}
107
- return ext .GetIsMaterializedView ()
108
154
}
0 commit comments