Skip to content

Commit

Permalink
refactor(config): add method GetChunksByScope
Browse files Browse the repository at this point in the history
* add new functional in renreder
* edit templates (app, container, database)
* edit field "scope" in chunks
  • Loading branch information
alekseykondratev91 committed Nov 30, 2023
1 parent 15c57c0 commit bde7fef
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 25 deletions.
11 changes: 11 additions & 0 deletions internal/config/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion internal/generator/chunks/mysqlchunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mysqlchunk

import (
_ "embed"

"github.com/hanagantig/goro/internal/config"
)

Expand All @@ -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\"",
Expand Down
3 changes: 2 additions & 1 deletion internal/generator/chunks/mysqlxchunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mysqlxchunk

import (
_ "embed"

"github.com/hanagantig/goro/internal/config"
)

Expand All @@ -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\"",
Expand Down
3 changes: 2 additions & 1 deletion internal/generator/chunks/pgsqlxchunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pgsqlxchunk

import (
_ "embed"

"github.com/hanagantig/goro/internal/config"
)

Expand All @@ -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\"",
Expand Down
26 changes: 17 additions & 9 deletions internal/generator/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions internal/generator/templates/app/internal/app/app.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -29,7 +29,7 @@ type App struct {
//hc health.Checker
//hcOnce *sync.Once

{{ renderDefinition "storage" .Chunks }}
{{ renderDefinition "storage" . }}

logger Logger
}
Expand All @@ -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
}
Expand All @@ -72,4 +72,4 @@ func GetGlobalApp() (*App, error) {
}

return a, nil
}
}
12 changes: 6 additions & 6 deletions internal/generator/templates/app/internal/app/container.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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{}),
}
}
Expand All @@ -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}}
}
Expand All @@ -66,4 +66,4 @@ func (c *Container) GetUseCase() *usecase.UseCase {
return {{$val.GetPkgName}}.{{ $val.GetConstructorName }} (c.get{{toCamelCase $val.Storage.String}}())
}
{{end}}
{{end}}
{{end}}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package app
import (
"{{ .App.Module }}/internal/config"
"fmt"
{{ renderImports "storage" "build" .Chunks }}
{{ renderImports "storage.database" "build" . }}
)

{{ renderBuild "storage" .Chunks }}
{{ renderBuild "storage.database" . }}

0 comments on commit bde7fef

Please sign in to comment.