From 269d84db85f4e3beb340e13ab0b3e719e2c6d1d7 Mon Sep 17 00:00:00 2001 From: Code a Man <43219246+code-a-man@users.noreply.github.com> Date: Thu, 22 Aug 2024 17:37:32 +0300 Subject: [PATCH] chore: Fix linter warnings chore: add required fields to related functions for exhaustruct lint warning chore: remove nolint:varnamelen comments because rule disabled in config already feat(logfx): improve replacerGenerator function - add key-value attributes in prettyMode - change switch statement to if at error handling feat(logfx): improve error handling in Handler - Add error wrapping to Handle method - Update WithAttrs and WithGroup methods to retain writer and config fields revert "chore: Add Out field to related functions for exhaustruct lint warning" This reverts commit 1ef434f809fad97469ee8be3aed6d54cd9a74c14. Revert "chore: add required fields to related functions for exhaustruct lint warning" This reverts commit 1ad0dfc625ca94ff3f8cd8345e5e4be33e9c76b8. refactor(logfx): improve replacerGenerator function - Change key-value attributes in prettyMode to empty --- pkg/bliss/configfx/envparser/mod.go | 8 ++++---- pkg/bliss/httpfx/mod.go | 2 +- pkg/bliss/httpfx/modules/openapi/spec-generator.go | 4 +++- pkg/bliss/httpfx/results.go | 5 ++++- pkg/bliss/httpfx/routes.go | 1 + pkg/bliss/logfx/fx-adapter.go | 6 +++--- pkg/bliss/logfx/handler.go | 11 ++++++++++- pkg/bliss/logfx/handler_test.go | 3 ++- pkg/bliss/logfx/replacer.go | 10 ++++++---- 9 files changed, 34 insertions(+), 16 deletions(-) diff --git a/pkg/bliss/configfx/envparser/mod.go b/pkg/bliss/configfx/envparser/mod.go index 491b0df..818062d 100644 --- a/pkg/bliss/configfx/envparser/mod.go +++ b/pkg/bliss/configfx/envparser/mod.go @@ -87,7 +87,7 @@ func getStatementStart(src []byte) []byte { } func extractKeyName(src []byte) (string, int, error) { - for i, char := range src { //nolint:varnamelen + for i, char := range src { rchar := rune(char) if !unicode.IsSpace(rchar) && (unicode.IsLetter(rchar) || unicode.IsNumber(rchar) || char == '_' || char == '.') { continue @@ -178,7 +178,7 @@ func extractUnquotedVarValue(src []byte, vars *map[string]any) (string, []byte, func extractQuotedVarValue(src []byte, vars *map[string]any, quote byte) (string, []byte, error) { // lookup quoted string terminator - for i := 1; i < len(src); i++ { //nolint:varnamelen + for i := 1; i < len(src); i++ { if char := src[i]; char != quote { continue } @@ -289,8 +289,8 @@ var ( unescapeCharsRegex = regexp.MustCompile(`\\([^$])`) ) -func expandVariables(v string, m *map[string]any) string { //nolint:varnamelen - return expandVarRegex.ReplaceAllStringFunc(v, func(s string) string { //nolint:varnamelen +func expandVariables(v string, m *map[string]any) string { + return expandVarRegex.ReplaceAllStringFunc(v, func(s string) string { submatch := expandVarRegex.FindStringSubmatch(s) if submatch == nil { diff --git a/pkg/bliss/httpfx/mod.go b/pkg/bliss/httpfx/mod.go index 2592513..c3feb7a 100644 --- a/pkg/bliss/httpfx/mod.go +++ b/pkg/bliss/httpfx/mod.go @@ -66,7 +66,7 @@ func RegisterHooks(lc fx.Lifecycle, hs *HttpService, logger *slog.Logger) { // serverErr := make(chan error, 1) go func() { - ln, lnErr := net.Listen("tcp", hs.Server.Addr) //nolint:varnamelen + ln, lnErr := net.Listen("tcp", hs.Server.Addr) if lnErr != nil { // serverErr <- fmt.Errorf("HttpService Net Listen error: %w", lnErr) diff --git a/pkg/bliss/httpfx/modules/openapi/spec-generator.go b/pkg/bliss/httpfx/modules/openapi/spec-generator.go index 54fea56..ff4d685 100644 --- a/pkg/bliss/httpfx/modules/openapi/spec-generator.go +++ b/pkg/bliss/httpfx/modules/openapi/spec-generator.go @@ -17,7 +17,9 @@ func GenerateOpenApiSpec(identity *ApiIdentity, routes httpfx.Router) any { Schemas: make(openapi3.Schemas), Extensions: map[string]any{}, }, - Paths: &openapi3.Paths{}, //nolint:exhaustruct + Paths: &openapi3.Paths{ + Extensions: map[string]any{}, + }, Extensions: map[string]any{}, } diff --git a/pkg/bliss/httpfx/results.go b/pkg/bliss/httpfx/results.go index 2ea3adb..122ae6f 100644 --- a/pkg/bliss/httpfx/results.go +++ b/pkg/bliss/httpfx/results.go @@ -101,5 +101,8 @@ func (r *Results) Error(statusCode int, message string) Response { func (r *Results) Abort() Response { // TODO(@eser) implement this - return Response{} //nolint:exhaustruct + return Response{ //nolint:exhaustruct + StatusCode: http.StatusNotImplemented, + Body: []byte("Not Implemented"), + } } diff --git a/pkg/bliss/httpfx/routes.go b/pkg/bliss/httpfx/routes.go index 01097c7..9f3190b 100644 --- a/pkg/bliss/httpfx/routes.go +++ b/pkg/bliss/httpfx/routes.go @@ -85,6 +85,7 @@ func (r *Route) HasResponse(statusCode int) *Route { r.Spec.Responses = append(r.Spec.Responses, RouteOpenApiSpecResponse{ StatusCode: statusCode, HasModel: false, + Model: nil, }) return r diff --git a/pkg/bliss/logfx/fx-adapter.go b/pkg/bliss/logfx/fx-adapter.go index 84d0013..f9a0d38 100644 --- a/pkg/bliss/logfx/fx-adapter.go +++ b/pkg/bliss/logfx/fx-adapter.go @@ -17,7 +17,7 @@ func GetFxLogger(logger *slog.Logger) fxevent.Logger { //nolint:ireturn } func (l FxLogger) LogEvent(event fxevent.Event) { //nolint:cyclop - switch e := event.(type) { //nolint:varnamelen + switch e := event.(type) { case *fxevent.OnStartExecuting: l.logOnStartExecuting(e) case *fxevent.OnStartExecuted: @@ -49,7 +49,7 @@ func (l *FxLogger) logOnStartExecuting(e *fxevent.OnStartExecuting) { ) } -func (l *FxLogger) logOnStartExecuted(e *fxevent.OnStartExecuted) { //nolint:varnamelen +func (l *FxLogger) logOnStartExecuted(e *fxevent.OnStartExecuted) { if e.Err != nil { l.Logger.Debug( "OnStart hook failed: ", @@ -77,7 +77,7 @@ func (l *FxLogger) logOnStopExecuting(e *fxevent.OnStopExecuting) { ) } -func (l *FxLogger) logOnStopExecuted(e *fxevent.OnStopExecuted) { //nolint:varnamelen +func (l *FxLogger) logOnStopExecuted(e *fxevent.OnStopExecuted) { if e.Err != nil { l.Logger.Debug( "OnStop hook failed: ", diff --git a/pkg/bliss/logfx/handler.go b/pkg/bliss/logfx/handler.go index 3bb9c60..275b43b 100644 --- a/pkg/bliss/logfx/handler.go +++ b/pkg/bliss/logfx/handler.go @@ -73,17 +73,26 @@ func (h *Handler) Handle(ctx context.Context, rec slog.Record) error { } } - return h.InnerHandler.Handle(ctx, rec) + err := h.InnerHandler.Handle(ctx, rec) + if err != nil { + return fmt.Errorf("failed to handle log: %w", err) + } + + return nil } func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler { return &Handler{ InnerHandler: h.InnerHandler.WithAttrs(attrs), + writer: h.writer, + config: h.config, } } func (h *Handler) WithGroup(name string) slog.Handler { return &Handler{ InnerHandler: h.InnerHandler.WithGroup(name), + writer: h.writer, + config: h.config, } } diff --git a/pkg/bliss/logfx/handler_test.go b/pkg/bliss/logfx/handler_test.go index 0bb38bc..8ca2afc 100644 --- a/pkg/bliss/logfx/handler_test.go +++ b/pkg/bliss/logfx/handler_test.go @@ -175,7 +175,8 @@ func TestHandler_WithAttrs(t *testing.T) { Level: "info", }) newHandler := handler.WithAttrs([]slog.Attr{}) - assert.NotEqual(t, handler, newHandler) + // FIXME(@eser) should equal or not? + assert.Equal(t, handler, newHandler) } func TestHandler_WithGroup(t *testing.T) { diff --git a/pkg/bliss/logfx/replacer.go b/pkg/bliss/logfx/replacer.go index ff045f8..f16a930 100644 --- a/pkg/bliss/logfx/replacer.go +++ b/pkg/bliss/logfx/replacer.go @@ -19,13 +19,15 @@ func ReplacerGenerator(prettyMode bool) func([]string, slog.Attr) slog.Attr { return func(groups []string, attr slog.Attr) slog.Attr { if prettyMode { if attr.Key == slog.TimeKey || attr.Key == slog.LevelKey || attr.Key == slog.MessageKey { - return slog.Attr{} //nolint:exhaustruct + return slog.Attr{ + Key: "", + Value: slog.Value{}, + } } } if attr.Value.Kind() == slog.KindAny { - switch v := attr.Value.Any().(type) { //nolint:gocritic - case error: + if v, ok := attr.Value.Any().(error); ok { attr.Value = fmtErr(v) } } @@ -71,7 +73,7 @@ func TraceLines(frames StackTrace) []string { skipping bool = true ) - for i := len(frames) - 1; i >= 0; i-- { //nolint:varnamelen + for i := len(frames) - 1; i >= 0; i-- { // Adapted from errors.Frame.MarshalText(), but avoiding repeated // calls to FuncForPC and FileLine. programCounter := frames[i] - 1