Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ide
.idea

# binary
/bin
# development
.env
.DS_Store
Expand Down
18 changes: 9 additions & 9 deletions example/testapp/go.mod
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
module testapp

go 1.21.4
go 1.21.5

require (
github.com/go-sql-driver/mysql v1.7.1
github.com/google/uuid v1.4.0
github.com/google/uuid v1.5.0
github.com/gorilla/mux v1.8.1
github.com/hanagantig/gracy v0.0.0-20231003055507-4d3ebe0e0a0a
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.9
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.17.0
github.com/spf13/viper v1.18.1
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
466 changes: 18 additions & 448 deletions example/testapp/go.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions example/testapp/goro.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ app:
module: testapp
work_dir: ""

# Set one of supported loggers: [ "zap" , "slog" ]
logger: slog

storages:
- mysql
- mysqlx
Expand Down
15 changes: 10 additions & 5 deletions example/testapp/internal/app/app.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions example/testapp/internal/app/http.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions example/testapp/internal/app/logger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions example/testapp/internal/handler/http/api/v1/pong.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package v1

import (
"encoding/json"
"go.uber.org/zap"
"log/slog"
"net/http"
)

Expand All @@ -13,6 +13,6 @@ func (h Handler) Ping(w http.ResponseWriter, r *http.Request) {
res, err := h.uc.Pong(ctx)
err = json.NewEncoder(w).Encode(res)
if err != nil {
h.logger.Error("failed to encode json", zap.Error(err))
h.logger.Error("failed to encode json", slog.String("err", err.Error()))
}
}
23 changes: 15 additions & 8 deletions example/testapp/pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package logger

import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"context"
"log/slog"
"os"
)

type Logger interface {
Debug(string, ...zapcore.Field)
Info(string, ...zapcore.Field)
Error(string, ...zapcore.Field)
Fatal(string, ...zapcore.Field)
Debug(string, ...any)
DebugContext(context.Context, string, ...any)
Info(string, ...any)
InfoContext(context.Context, string, ...any)
Error(string, ...any)
ErrorContext(context.Context, string, ...any)
Warn(string, ...any)
WarnContext(context.Context, string, ...any)
}

func NewLogger() (*zap.Logger, error) {
return zap.NewProduction()
func NewLogger() *slog.Logger {
handler := slog.NewJSONHandler(os.Stdout, nil)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's good to add here some default fields, such as env=production, app=goro, version=0.0.1


return slog.New(handler)
}
2 changes: 2 additions & 0 deletions internal/config/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Config struct {
Storages Storages `yaml:"storages"`
Services Services `yaml:"services"`
Adapters Adapters `yaml:"adapters"`
Logger Logger `yaml:"logger"`
Chunks []Chunk
}

Expand All @@ -32,6 +33,7 @@ type Chunk struct {
Build string
Configs string
InitConfig string
PkgInterface string
InitHasErr bool
}

Expand Down
48 changes: 48 additions & 0 deletions internal/config/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package config

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no needs in all of this thing since we will keep only slog implementation in our code (check this comment )


var loggerPackges = map[Logger]string{
"zap": "\"go.uber.org/zap\"",
"slog": "\"log/slog\"",
}

var serverHTTPStartFailMessage = map[Logger]string{
"zap": "a.logger.Fatal(\n\"Fail to start %s http server:\",\nzap.String(\"app\", a.cfg.App.Name),\nzap.Error(err),\n)",
"slog": "a.logger.Error(\n\"Fail to start %s http server:\",\nslog.String(\"app\", a.cfg.App.Name),\nslog.String(\"err\", err.Error()),\n)",
}

var serverHTTPStopFailMessage = map[Logger]string{
"zap": "a.logger.Error(\"failed to gracefully shutdown server\", zap.Error(err))",
"slog": "a.logger.Error(\"failed to gracefully shutdown server\", slog.String(\"err\", err.Error()))",
}

var examplePongHttpFailMessage = map[Logger]string{
"zap": "h.logger.Error(\"failed to encode json\", zap.Error(err))",
"slog": "h.logger.Error(\"failed to encode json\", slog.String(\"err\", err.Error()))",
}

var supportedLoggers = map[Logger]struct{}{
"zap": {},
"slog": {},
}

type Logger string

func (l Logger) String() string {
return string(l)
}

func (l Logger) GetImportPackage() string {
return loggerPackges[l]
}

func (l Logger) GetFailStartHTTPServerMessage() string {
return serverHTTPStartFailMessage[l]
}

func (l Logger) GetFailStopHTTPServerMessage() string {
return serverHTTPStopFailMessage[l]
}

func (l Logger) GetExamplePongHTTPFailMessage() string {
return examplePongHttpFailMessage[l]
}
4 changes: 4 additions & 0 deletions internal/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,9 @@ func (c *Config) Validate() error {
}
}

if _, ok := supportedLoggers[c.Logger]; !ok {
return fmt.Errorf("unsupported logger '%v'", c.Logger.String())
}

return nil
}
4 changes: 4 additions & 0 deletions internal/generator/chunks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import (
"github.com/hanagantig/goro/internal/generator/chunks/mysqlchunk"
"github.com/hanagantig/goro/internal/generator/chunks/mysqlxchunk"
"github.com/hanagantig/goro/internal/generator/chunks/pgsqlxchunk"
"github.com/hanagantig/goro/internal/generator/chunks/slogchunk"
"github.com/hanagantig/goro/internal/generator/chunks/zapchunk"
)

var supportedChunks = map[string]config.Chunk{
"mysql": mysqlchunk.NewMySQLChunk(),
"mysqlx": mysqlxchunk.NewMySQLxChunk(),
"pgsqlx": pgsqlxchunk.NewPostgresChunk(),
"http": httpchunk.NewHttpChunk(),
"zap": zapchunk.NewZapLoggerChunk(),
"slog": slogchunk.NewSlogLoggerChunk(),
}
5 changes: 5 additions & 0 deletions internal/generator/chunks/slogchunk/build.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
func NewLogger() *slog.Logger {
handler := slog.NewJSONHandler(os.Stdout, nil)

return slog.New(handler)
}
33 changes: 33 additions & 0 deletions internal/generator/chunks/slogchunk/chunk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package slogchunk

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep only slog implementation in the generated code. Basically we will use slog everywhere and if somebody want to use different loggers - they will just implement their own handler for the slog


import (
_ "embed"

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

//go:embed build.tpl
var buildTmpl string

//go:embed pkginterface.tpl
var pkgInterface string

const name = "slog"
const initName = "l"

func NewSlogLoggerChunk() config.Chunk {
return config.Chunk{
Name: name,
Scope: "logger.slog",
ArgName: initName,
ReturnType: "*slog.Logger",
DefinitionImports: "\"context\"",
BuildImports: "\"log/slog\"\n\"os\"\n\"context\"",
InitFunc: "NewLogger",
Build: buildTmpl,
Configs: "logger configs",
InitConfig: "",
PkgInterface: pkgInterface,
InitHasErr: false,
}
}
11 changes: 11 additions & 0 deletions internal/generator/chunks/slogchunk/pkginterface.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type Logger interface {
Debug(string, ...any)
DebugContext(context.Context, string, ...any)
Info(string, ...any)
InfoContext(context.Context, string, ...any)
Error(string, ...any)
ErrorContext(context.Context, string, ...any)
Warn(string, ...any)
WarnContext(context.Context, string, ...any)
}

3 changes: 3 additions & 0 deletions internal/generator/chunks/zapchunk/build.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func NewLogger() (*zap.Logger, error) {
return zap.NewProduction()
}
33 changes: 33 additions & 0 deletions internal/generator/chunks/zapchunk/chunk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package zapchunk

import (
_ "embed"

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

//go:embed build.tpl
var buildTmpl string

//go:embed pkginteface.tpl
var pkgInterface string

const name = "zap"
const initName = "l"

func NewZapLoggerChunk() config.Chunk {
return config.Chunk{
Name: name,
Scope: "logger.zap",
ArgName: initName,
ReturnType: "*zap.Logger",
DefinitionImports: "\"go.uber.org/zap/zapcore\"",
BuildImports: "\"go.uber.org/zap/zapcore\"\n\"go.uber.org/zap\"",
InitFunc: "NewLogger",
Build: buildTmpl,
Configs: "logger configs",
InitConfig: "",
PkgInterface: pkgInterface,
InitHasErr: true,
}
}
7 changes: 7 additions & 0 deletions internal/generator/chunks/zapchunk/pkginteface.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type Logger interface {
Debug(string, ...zapcore.Field)
Info(string, ...zapcore.Field)
Error(string, ...zapcore.Field)
Fatal(string, ...zapcore.Field)
}

7 changes: 7 additions & 0 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ func (g *Generator) loadChunks() error {
g.config.Chunks = append(g.config.Chunks, ch)
}

ch, ok := supportedChunks[g.config.Logger.String()]
if !ok {
return fmt.Errorf("%w: %v", UnsupportedChunkErr, g.config.Logger.String())
}

g.config.Chunks = append(g.config.Chunks, ch)

return nil
}

Expand Down
Loading