|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "runtime/debug" |
| 5 | + |
| 6 | + "github.com/averak/protobq/internal/protobuf/protobq" |
| 7 | + "google.golang.org/protobuf/compiler/protogen" |
| 8 | + "google.golang.org/protobuf/proto" |
| 9 | +) |
| 10 | + |
| 11 | +//goland:noinspection GoSnakeCaseUsage |
| 12 | +var ( |
| 13 | + timeIdents = struct { |
| 14 | + Duration protogen.GoIdent |
| 15 | + }{ |
| 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"), |
| 29 | + } |
| 30 | + protobqIdents = struct { |
| 31 | + MaterializedViewOptions protogen.GoIdent |
| 32 | + }{ |
| 33 | + MaterializedViewOptions: protogen.GoImportPath("github.com/averak/protobq").Ident("MaterializedViewOptions"), |
| 34 | + } |
| 35 | +) |
| 36 | + |
| 37 | +type CodeGenerator struct { |
| 38 | + plugin *protogen.Plugin |
| 39 | + file *protogen.File |
| 40 | +} |
| 41 | + |
| 42 | +func NewCodeGenerator(plugin *protogen.Plugin, file *protogen.File) *CodeGenerator { |
| 43 | + return &CodeGenerator{ |
| 44 | + plugin: plugin, |
| 45 | + file: file, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (g CodeGenerator) Gen() error { |
| 50 | + if !g.shouldGenerate() { |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + filename := g.file.GeneratedFilenamePrefix + ".protobq.go" |
| 55 | + gf := g.plugin.NewGeneratedFile(filename, g.file.GoImportPath) |
| 56 | + |
| 57 | + { // generate file header |
| 58 | + info, _ := debug.ReadBuildInfo() |
| 59 | + gf.P("// Code generated by ", info.Path, ". DO NOT EDIT.") |
| 60 | + gf.P("// source: ", g.file.Desc.Path()) |
| 61 | + gf.P() |
| 62 | + gf.P("package ", g.file.GoPackageName) |
| 63 | + gf.P() |
| 64 | + } |
| 65 | + { // generate materialized view schema |
| 66 | + for _, msg := range g.file.Messages { |
| 67 | + if !g.isMaterializedViewSchema(msg) { |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + gf.P("func (mv *", msg.GoIdent.GoName, ") Options() ", protobqIdents.MaterializedViewOptions, " {") |
| 72 | + gf.P(" ext, _ := ", protoIdents.GetExtension, "(mv.ProtoReflect().Descriptor().Options(), ", internalIdents.E_MaterializedView, ").(*", internalIdents.MaterializedView, ")") |
| 73 | + gf.P(" return ", protobqIdents.MaterializedViewOptions, "{") |
| 74 | + gf.P(" EnableRefresh: ext.GetEnableRefresh(),") |
| 75 | + gf.P(" RefreshInterval: ", timeIdents.Duration, "(ext.GetRefreshIntervalMinutes()) * time.Minute,") |
| 76 | + gf.P(" }") |
| 77 | + gf.P("}") |
| 78 | + gf.P() |
| 79 | + } |
| 80 | + } |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func (g CodeGenerator) shouldGenerate() bool { |
| 85 | + for _, msg := range g.file.Messages { |
| 86 | + if g.isMaterializedViewSchema(msg) { |
| 87 | + return true |
| 88 | + } |
| 89 | + } |
| 90 | + return false |
| 91 | +} |
| 92 | + |
| 93 | +func (g CodeGenerator) isMaterializedViewSchema(msg *protogen.Message) bool { |
| 94 | + opts := msg.Desc.Options() |
| 95 | + if opts == nil { |
| 96 | + return false |
| 97 | + } |
| 98 | + |
| 99 | + ext, ok := proto.GetExtension(opts, protobq.E_MaterializedView).(*protobq.MaterializedView) |
| 100 | + if !ok { |
| 101 | + return false |
| 102 | + } |
| 103 | + return ext.GetIsMaterializedView() |
| 104 | +} |
0 commit comments