Skip to content
Merged
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
23 changes: 16 additions & 7 deletions cmd/internal/migrations/v3/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,23 @@ func MigrateGenericHelpers(cmd *cobra.Command, cwd string, _, _ *semver.Version)

// MigrateContextMethods updates context related methods to the new names
func MigrateContextMethods(cmd *cobra.Command, cwd string, _, _ *semver.Version) error {
replacer := strings.NewReplacer(
".Context()", ".RequestCtx()",
".UserContext()", ".Context()",
".SetUserContext(", ".SetContext(", // TODO: check if this is correct
)

err := internal.ChangeFileContent(cwd, func(content string) string {
return replacer.Replace(content)
// UserContext() removed - Ctx implements context.Context
reUserCtx := regexp.MustCompile(`(\w+)\.UserContext\(\)`)
content = reUserCtx.ReplaceAllString(content, `$1`)
Comment on lines +103 to +104
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

It's more efficient to compile the regular expression outside the ChangeFileContent closure, as the closure is executed for each file. Compiling it once at the function level improves performance.


// SetUserContext removed - comment out the call
reSetUserCtx := regexp.MustCompile(`(?m)^(\s*)(.*\.SetUserContext\([^\n]*\).*)$`)
content = reSetUserCtx.ReplaceAllString(content, `$1// TODO: SetUserContext was removed, please migrate manually: $2`)

// old Context() returned fasthttp.RequestCtx
reReqCtx := regexp.MustCompile(`(\w+)\.Context\(\)`)
content = reReqCtx.ReplaceAllString(content, `$1.RequestCtx()`)

// remaining Context() usages return context.Context -> remove call
content = strings.ReplaceAll(content, ".Context()", "")
Comment on lines +114 to +115
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The strings.ReplaceAll call is broad and might introduce subtle bugs. The reReqCtx regex on line 111 only handles simple identifiers. For complex receivers, this line will incorrectly strip .Context(), potentially breaking user code. Consider removing this fallback to avoid silent bugs.


return content
})
if err != nil {
return fmt.Errorf("failed to migrate context methods: %w", err)
Expand Down
30 changes: 28 additions & 2 deletions cmd/internal/migrations/v3/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,34 @@ func handler(c fiber.Ctx) error {

content := readFile(t, file)
assert.Contains(t, content, ".RequestCtx()")
assert.Contains(t, content, ".Context()")
assert.Contains(t, content, ".SetContext(")
assert.NotContains(t, content, ".Context()")
assert.Contains(t, content, `// TODO: SetUserContext was removed, please migrate manually: c.SetUserContext(ctx)`)
assert.Contains(t, content, "uc := c")
assert.Contains(t, buf.String(), "Migrating context methods")
}

func Test_MigrateContextMethods_SetUserContextAssignment(t *testing.T) {
t.Parallel()

dir, err := os.MkdirTemp("", "mcmtest2")
require.NoError(t, err)
defer func() { require.NoError(t, os.RemoveAll(dir)) }()

file := writeTempFile(t, dir, `package main
import "github.com/gofiber/fiber/v2"
func handler(ctx fiber.Ctx) error {
res := ctx.SetUserContext(ctx.Context())
_ = res
return nil
}`)

var buf bytes.Buffer
cmd := newCmd(&buf)
require.NoError(t, v3.MigrateContextMethods(cmd, dir, nil, nil))

content := readFile(t, file)
assert.Contains(t, content, `// TODO: SetUserContext was removed, please migrate manually: res := ctx.SetUserContext(ctx.RequestCtx())`)
assert.NotContains(t, content, `.UserContext()`)
assert.Contains(t, buf.String(), "Migrating context methods")
}

Expand Down
Loading