github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/cmd/protoc-gen-gorony/repo/gen.go (about) 1 package repo 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/ronaksoft/rony/cmd/protoc-gen-gorony/repo/cql" 8 "github.com/ronaksoft/rony/cmd/protoc-gen-gorony/repo/store" 9 "github.com/ronaksoft/rony/internal/codegen" 10 "google.golang.org/protobuf/compiler/protogen" 11 ) 12 13 /* 14 Creation Time: 2021 - Jul - 16 15 Created by: (ehsan) 16 Maintainers: 17 1. Ehsan N. Moosa (E2) 18 Auditor: Ehsan N. Moosa (E2) 19 Copyright Ronak Software Group 2020 20 */ 21 22 const ( 23 localRepoPrefix = "Local" 24 GlobalRepoPrefix = "Global" 25 ) 26 27 type Generator struct { 28 p *protogen.Plugin 29 f *protogen.File 30 g *protogen.GeneratedFile 31 initFuncBlock *strings.Builder 32 } 33 34 func New(p *protogen.Plugin, f *protogen.File, g *protogen.GeneratedFile) *Generator { 35 return &Generator{ 36 p: p, 37 f: f, 38 g: g, 39 initFuncBlock: &strings.Builder{}, 40 } 41 } 42 43 func (g *Generator) Generate() { 44 g.g.QualifiedGoIdent(protogen.GoIdent{GoName: "", GoImportPath: "bytes"}) 45 g.g.P("var _ = bytes.MinRead") 46 47 var cqlGen *protogen.GeneratedFile 48 for _, m := range g.f.Messages { 49 arg := codegen.GetMessageArg(m).With(g.f) 50 if !arg.IsAggregate && !arg.IsSingleton { 51 continue 52 } 53 validLocalRepo := true 54 validGlobalRepo := true 55 56 switch arg.LocalRepo { 57 case "store": 58 store.Generate(store.New(g.f, g.g, localRepoPrefix), arg) 59 case "cql": 60 if cqlGen == nil { 61 cqlGen = g.p.NewGeneratedFile(fmt.Sprintf("%s.cql", g.f.GeneratedFilenamePrefix), g.f.GoImportPath) 62 } 63 cql.GenerateCQL(cql.New(g.f, cqlGen, localRepoPrefix), arg) 64 cql.GenerateGo(cql.New(g.f, g.g, localRepoPrefix), arg) 65 default: 66 validLocalRepo = false 67 } 68 69 switch arg.GlobalRepo { 70 case "cql": 71 if cqlGen == nil { 72 cqlGen = g.p.NewGeneratedFile(fmt.Sprintf("%s.cql", g.f.GeneratedFilenamePrefix), g.f.GoImportPath) 73 } 74 cql.GenerateCQL(cql.New(g.f, cqlGen, GlobalRepoPrefix), arg) 75 cql.GenerateGo(cql.New(g.f, g.g, GlobalRepoPrefix), arg) 76 77 default: 78 validGlobalRepo = false 79 } 80 if validLocalRepo { 81 if arg.IsAggregate { 82 g.g.QualifiedGoIdent(protogen.GoIdent{GoName: "", GoImportPath: "github.com/ronaksoft/rony/di"}) 83 g.appendToInit(fmt.Sprintf("di.MustProvide(New%sLocalRepo)\n", arg.Name())) 84 } 85 if arg.IsSingleton { 86 g.g.QualifiedGoIdent(protogen.GoIdent{GoName: "", GoImportPath: "github.com/ronaksoft/rony/di"}) 87 g.appendToInit(fmt.Sprintf("di.MustProvide(New%sLocalSingleton)\n", arg.Name())) 88 } 89 } 90 if validGlobalRepo { 91 if arg.IsAggregate { 92 g.g.QualifiedGoIdent(protogen.GoIdent{GoName: "", GoImportPath: "github.com/ronaksoft/rony/di"}) 93 g.appendToInit(fmt.Sprintf("di.MustProvide(New%sGlobalRepo)\n", arg.Name())) 94 } 95 } 96 } 97 98 if g.initFuncBlock.Len() > 0 { 99 g.g.P("// register provider constructors for dependency injection") 100 g.g.P("func init() {") 101 g.g.P(g.initFuncBlock.String()) 102 g.g.P("}") 103 } 104 } 105 106 func (g *Generator) appendToInit(x string) { 107 g.initFuncBlock.WriteString(x) 108 g.initFuncBlock.WriteRune('\n') 109 }