Skip to content

Commit

Permalink
chore: Fix linter warnings
Browse files Browse the repository at this point in the history
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 1ef434f.

Revert "chore: add required fields to related functions for exhaustruct lint warning"

This reverts commit 1ad0dfc.

refactor(logfx): improve replacerGenerator function

- Change key-value attributes in prettyMode to empty
  • Loading branch information
code-a-man committed Aug 22, 2024
1 parent 32c504c commit 269d84d
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 16 deletions.
8 changes: 4 additions & 4 deletions pkg/bliss/configfx/envparser/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/bliss/httpfx/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/bliss/httpfx/modules/openapi/spec-generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/bliss/httpfx/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
1 change: 1 addition & 0 deletions pkg/bliss/httpfx/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/bliss/logfx/fx-adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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: ",
Expand Down Expand Up @@ -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: ",
Expand Down
11 changes: 10 additions & 1 deletion pkg/bliss/logfx/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
3 changes: 2 additions & 1 deletion pkg/bliss/logfx/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 6 additions & 4 deletions pkg/bliss/logfx/replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 269d84d

Please sign in to comment.