Skip to content

Conversation

@erancihan
Copy link
Collaborator

@erancihan erancihan commented Oct 8, 2025

Overview

Function signature of Wrap is too large and adding new flags further along the project will keep increasing it's size.
As such, function arguments to accommodate further config flags to be passed without a change in the function's signature.

Changes

  • moved Config struct to internal/options/config.go
  • updated wrap middleware signature to pass config pointer

Summary by cubic

Switched middleware wrappers to accept a config pointer and moved the Config struct to internal/options to shrink function signatures and make future flags easier to add. No change in behavior.

  • Refactors

    • Moved Config to internal/options/config.go and kept ShouldIgnorePath on it.
    • Updated Wrap and WrapWithProfiling to use *options.Config instead of booleans.
    • Updated Option type and defaultConfig to target internal/options.Config.
    • Adjusted wrap.go to pass config through to middleware.
    • Updated tests to use the new signature.
  • Migration

    • If you call internal middleware.Wrap or WrapWithProfiling directly, pass a *options.Config instead of logRequestBody/logResponseBody flags.

- moved Config struct to internal/options/config.go
- updated wrap middleware signature to pass config pointer
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Oct 8, 2025

Reviewer's Guide

Extracted configuration into a central options.Config and updated all Option builders, middleware signatures, invocation sites, and tests to work with a single config pointer rather than multiple flag arguments, simplifying the Wrap API and preparing for future flags without altering its signature.

Class diagram for refactored Config struct and Option type

classDiagram
    class options.Config {
        int MaxRequests
        string DashboardPath
        bool LogRequestBody
        bool LogResponseBody
        []string IgnorePaths
        bool EnableOpenTelemetry
        string ServiceName
        string ServiceVersion
        string OTelEndpoint
        store.StorageType StorageType
        string ConnectionString
        string TableName
        int RedisTTL
        sql.DB* ExistingDB
        bool EnableProfiling
        profiling.ProfileType ProfileType
        time.Duration ProfileThreshold
        int MaxProfileMetrics
        ShouldIgnorePath(path string) bool
    }
    class Option {
        <<function>>
        Option(*options.Config)
    }
    Option --> options.Config : modifies
Loading

Class diagram for updated middleware signatures

classDiagram
    class middleware {
        +Wrap(handler http.Handler, store store.Store, config *options.Config, pathMatcher PathMatcher) http.Handler
        +WrapWithProfiling(handler http.Handler, store store.Store, config *options.Config, pathMatcher PathMatcher, profiler *profiling.Profiler) http.Handler
    }
    options.Config <.. middleware : used as parameter
Loading

File-Level Changes

Change Details Files
Relocate Config struct and ignore-path logic to internal options package
  • Created new internal/options/config.go with Config definition and ShouldIgnorePath
  • Removed Config type, defaultConfig, and ignore logic from options.go
  • Updated defaultConfig to return *options.Config
options.go
internal/options/config.go
Switch Option functions to operate on *options.Config
  • Changed Option type to func(options.Config)
  • Updated all With functions to target options.Config fields
options.go
Refactor middleware.Wrap signatures to accept config pointer
  • Replaced logRequestBody/logResponseBody parameters with *options.Config
  • Updated Wrap and WrapWithProfiling to reference config.LogRequestBody/LogResponseBody and pathMatcher
internal/middleware/middleware.go
internal/middleware/profiling.go
Update wrap.go calls to pass full config
  • Switched middleware.Wrap and WrapWithProfiling calls to use config pointer instead of separate flags
wrap.go
Adapt middleware tests to new config usage
  • Imported internal/options and created config instances in tests
  • Rewired Wrap test to pass *options.Config versus individual booleans
internal/middleware/middleware_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `internal/options/config.go:59` </location>
<code_context>
+}
+
+// ShouldIgnorePath checks if a path should be ignored based on the configured patterns
+func (c *Config) ShouldIgnorePath(path string) bool {
+	// First check if it's the dashboard path which should always be ignored to prevent recursive logging
+	if path == c.DashboardPath || strings.HasPrefix(path, c.DashboardPath+"/") {
</code_context>

<issue_to_address>
**suggestion:** ShouldIgnorePath uses filepath.Match for pattern matching.

filepath.Match may not handle URL paths correctly. Consider using a matcher better suited for HTTP paths, such as regex or prefix matching.

Suggested implementation:

```golang
	for _, pattern := range c.IgnorePaths {
		// If pattern starts with '^' or contains special regex characters, treat as regex
		if strings.HasPrefix(pattern, "^") || strings.ContainsAny(pattern, ".*+?()[]{}|\\") {
			matched, err := regexp.MatchString(pattern, path)
			if err == nil && matched {
				return true
			}
		} else {
			// Otherwise, treat as simple prefix match
			if strings.HasPrefix(path, pattern) {
				return true
			}
		}

```

You will need to:
1. Import the "regexp" package at the top of the file: `import "regexp"`
2. Remove any import of "path/filepath" if it is only used for this matching.
3. Document in your config that ignore patterns can be either prefixes or regexes (starting with ^ or containing regex special characters).
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 6 files

Prompt for AI agents (all 1 issues)

Understand the root cause of the following 1 issues and fix them.


<file name="internal/middleware/middleware.go">

<violation number="1" location="internal/middleware/middleware.go:55">
Wrap now dereferences config without first guarding against nil, so a nil *options.Config will panic instead of using defaults. Please default or guard the config pointer before accessing its fields.</violation>
</file>


Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Ask questions if you need clarification on any suggestion

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant