From bde7feff1990957635d9e65694e2d2f71a7cb12f Mon Sep 17 00:00:00 2001 From: alexkondratev91 Date: Thu, 30 Nov 2023 16:24:15 +0300 Subject: [PATCH] refactor(config): add method GetChunksByScope * add new functional in renreder * edit templates (app, container, database) * edit field "scope" in chunks --- internal/config/data.go | 11 ++++++++ internal/generator/chunks/mysqlchunk/chunk.go | 3 ++- .../generator/chunks/mysqlxchunk/chunk.go | 3 ++- .../generator/chunks/pgsqlxchunk/chunk.go | 3 ++- internal/generator/renderer.go | 26 ++++++++++++------- .../templates/app/internal/app/app.go.tmpl | 10 +++---- .../app/internal/app/container.go.tmpl | 12 ++++----- .../app/internal/app/database.go.tmpl | 4 +-- 8 files changed, 47 insertions(+), 25 deletions(-) diff --git a/internal/config/data.go b/internal/config/data.go index 7f8641e..d0851c8 100644 --- a/internal/config/data.go +++ b/internal/config/data.go @@ -131,3 +131,14 @@ func (c *Config) askWorkDir() (string, error) { // TODO: validate path }).Run() } + +func (c *Config) GetChunksByScope(scope string) []Chunk { + chunks := make([]Chunk, 0, len(c.Chunks)) + for _, ch := range c.Chunks { + if strings.HasPrefix(ch.Scope, scope) { + chunks = append(chunks, ch) + } + } + + return chunks +} diff --git a/internal/generator/chunks/mysqlchunk/chunk.go b/internal/generator/chunks/mysqlchunk/chunk.go index f9f3c13..6f3bedf 100644 --- a/internal/generator/chunks/mysqlchunk/chunk.go +++ b/internal/generator/chunks/mysqlchunk/chunk.go @@ -2,6 +2,7 @@ package mysqlchunk import ( _ "embed" + "github.com/hanagantig/goro/internal/config" ) @@ -16,7 +17,7 @@ const initHasErr = true func NewMySQLChunk() config.Chunk { return config.Chunk{ Name: name, - Scope: "storage", + Scope: "storage.database.mysql", ArgName: "mysqlConnect", ReturnType: "*sql.DB", DefinitionImports: "\"database/sql\"", diff --git a/internal/generator/chunks/mysqlxchunk/chunk.go b/internal/generator/chunks/mysqlxchunk/chunk.go index a18bc41..e96fd72 100644 --- a/internal/generator/chunks/mysqlxchunk/chunk.go +++ b/internal/generator/chunks/mysqlxchunk/chunk.go @@ -2,6 +2,7 @@ package mysqlxchunk import ( _ "embed" + "github.com/hanagantig/goro/internal/config" ) @@ -14,7 +15,7 @@ const initName = "mysqlxConn" func NewMySQLxChunk() config.Chunk { return config.Chunk{ Name: name, - Scope: "storage", + Scope: "storage.database.mysqlx", ArgName: initName, ReturnType: "*sqlx.DB", DefinitionImports: "\"github.com/jmoiron/sqlx\"", diff --git a/internal/generator/chunks/pgsqlxchunk/chunk.go b/internal/generator/chunks/pgsqlxchunk/chunk.go index 93d68c1..029710b 100644 --- a/internal/generator/chunks/pgsqlxchunk/chunk.go +++ b/internal/generator/chunks/pgsqlxchunk/chunk.go @@ -2,6 +2,7 @@ package pgsqlxchunk import ( _ "embed" + "github.com/hanagantig/goro/internal/config" ) @@ -14,7 +15,7 @@ const initName = "pgSqlxConn" func NewPostgresChunk() config.Chunk { return config.Chunk{ Name: name, - Scope: "storage", + Scope: "storage.database.pgsqlx", ArgName: initName, ReturnType: "*sqlx.DB", DefinitionImports: "\"github.com/jmoiron/sqlx\"", diff --git a/internal/generator/renderer.go b/internal/generator/renderer.go index 6948305..f140a94 100644 --- a/internal/generator/renderer.go +++ b/internal/generator/renderer.go @@ -2,10 +2,12 @@ package generator import ( "fmt" - entity "github.com/hanagantig/goro/internal/config" - "github.com/iancoleman/strcase" "strings" "text/template" + + "github.com/iancoleman/strcase" + + entity "github.com/hanagantig/goro/internal/config" ) var FuncMap = template.FuncMap{ @@ -21,7 +23,8 @@ var FuncMap = template.FuncMap{ "contains": strings.Contains, } -func RenderImports(scope, stage string, chunks []entity.Chunk) string { +func RenderImports(scope, stage string, cfg entity.Config) string { + chunks := cfg.GetChunksByScope(scope) res := strings.Builder{} for _, ch := range chunks { switch stage { @@ -37,7 +40,8 @@ func RenderImports(scope, stage string, chunks []entity.Chunk) string { return res.String() } -func RenderDefinitions(scope string, chunks []entity.Chunk) string { +func RenderDefinitions(scope string, cfg entity.Config) string { + chunks := cfg.GetChunksByScope(scope) res := strings.Builder{} for _, ch := range chunks { fmt.Fprintf(&res, "%v %v\n", ch.Name, ch.ReturnType) @@ -46,7 +50,8 @@ func RenderDefinitions(scope string, chunks []entity.Chunk) string { return res.String() } -func RenderInitializationsWithError(scope, prefix string, chunks []entity.Chunk) string { +func RenderInitializationsWithError(scope, prefix string, cfg entity.Config) string { + chunks := cfg.GetChunksByScope(scope) res := strings.Builder{} for _, ch := range chunks { fmt.Fprintf(&res, "%v,%v := %v.%v(cfg.MainDB)\n", ch.ArgName, "err", prefix, ch.InitFunc) @@ -57,11 +62,12 @@ func RenderInitializationsWithError(scope, prefix string, chunks []entity.Chunk) return res.String() } -func RenderDependency(scope, prefix string, chunks []entity.Chunk) string { +func RenderDependency(scope, prefix string, cfg entity.Config) string { return "// render dependencies code" } -func RenderBuild(scope string, chunks []entity.Chunk) string { +func RenderBuild(scope string, cfg entity.Config) string { + chunks := cfg.GetChunksByScope(scope) res := strings.Builder{} for _, ch := range chunks { fmt.Fprintf(&res, "%v\n\n", ch.Build) @@ -70,7 +76,8 @@ func RenderBuild(scope string, chunks []entity.Chunk) string { return res.String() } -func RenderArgs(scope string, chunks []entity.Chunk) string { +func RenderArgs(scope string, cfg entity.Config) string { + chunks := cfg.GetChunksByScope(scope) res := strings.Builder{} for _, ch := range chunks { fmt.Fprintf(&res, "%v %v,", ch.ArgName, ch.ReturnType) @@ -79,7 +86,8 @@ func RenderArgs(scope string, chunks []entity.Chunk) string { return res.String() } -func RenderStructPopulation(scope string, chunks []entity.Chunk) string { +func RenderStructPopulation(scope string, cfg entity.Config) string { + chunks := cfg.GetChunksByScope(scope) res := strings.Builder{} for _, ch := range chunks { fmt.Fprintf(&res, "%v: %v,\n", ch.Name, ch.ArgName) diff --git a/internal/generator/templates/app/internal/app/app.go.tmpl b/internal/generator/templates/app/internal/app/app.go.tmpl index 6a8402f..eff975c 100644 --- a/internal/generator/templates/app/internal/app/app.go.tmpl +++ b/internal/generator/templates/app/internal/app/app.go.tmpl @@ -8,7 +8,7 @@ package app import ( "sync" "errors" - {{ renderImports "storage" "definition" .Chunks }} + {{ renderImports "storage" "definition" . }} "{{ .App.Module }}/internal/config" "go.uber.org/zap/zapcore" ) @@ -29,7 +29,7 @@ type App struct { //hc health.Checker //hcOnce *sync.Once - {{ renderDefinition "storage" .Chunks }} + {{ renderDefinition "storage" . }} logger Logger } @@ -54,10 +54,10 @@ func NewApp(configPath string) (*App, error) { //goro:init healthChecker //app.initHealthChecker() - {{ renderInitializationsWithError "storage" "app" .Chunks }} + {{ renderInitializationsWithError "storage" "app" . }} //goro:init dependencies - app.c = NewContainer({{range .Chunks}}{{if eq .Scope `storage`}}app.{{.Name}},{{end}}{{end}}) + app.c = NewContainer({{range .Chunks}}{{if contains .Scope "storage"}}app.{{.Name}},{{end}}{{end}}) return app, nil } @@ -72,4 +72,4 @@ func GetGlobalApp() (*App, error) { } return a, nil -} \ No newline at end of file +} diff --git a/internal/generator/templates/app/internal/app/container.go.tmpl b/internal/generator/templates/app/internal/app/container.go.tmpl index 750722d..c3ae354 100644 --- a/internal/generator/templates/app/internal/app/container.go.tmpl +++ b/internal/generator/templates/app/internal/app/container.go.tmpl @@ -6,7 +6,7 @@ package app // Editing this file might prove futile when you re-run the goro commands import ( - {{ renderImports "storage" "definition" .Chunks }} + {{ renderImports "storage" "definition" . }} "{{ $.App.Module }}/internal/usecase" @@ -24,15 +24,15 @@ import ( ) type Container struct { - {{ renderDefinition "storage" .Chunks }} + {{ renderDefinition "storage" . }} deps map[string]interface{} } -func NewContainer({{ renderArgs "storage" .Chunks }}) *Container { +func NewContainer({{ renderArgs "storage" . }}) *Container { return &Container{ - {{ renderStructPopulation "storage" .Chunks }} + {{ renderStructPopulation "storage" . }} deps: make(map[string]interface{}), } } @@ -43,7 +43,7 @@ func (c *Container) GetUseCase() *usecase.UseCase { } {{range .Chunks}} - {{if eq .Scope `storage`}} + {{if contains .Scope "storage"}} func (c *Container) get{{toCamelCase .Name}}() {{.ReturnType}} { return c.{{.Name}} } @@ -66,4 +66,4 @@ func (c *Container) GetUseCase() *usecase.UseCase { return {{$val.GetPkgName}}.{{ $val.GetConstructorName }} (c.get{{toCamelCase $val.Storage.String}}()) } {{end}} -{{end}} \ No newline at end of file +{{end}} diff --git a/internal/generator/templates/app/internal/app/database.go.tmpl b/internal/generator/templates/app/internal/app/database.go.tmpl index 9146dba..b883067 100644 --- a/internal/generator/templates/app/internal/app/database.go.tmpl +++ b/internal/generator/templates/app/internal/app/database.go.tmpl @@ -8,7 +8,7 @@ package app import ( "{{ .App.Module }}/internal/config" "fmt" - {{ renderImports "storage" "build" .Chunks }} + {{ renderImports "storage.database" "build" . }} ) -{{ renderBuild "storage" .Chunks }} \ No newline at end of file +{{ renderBuild "storage.database" . }}