Skip to content

Commit 9819fc0

Browse files
authored
Merge pull request #221 from gofiber/codex/2025-10-17-11-09-45
2 parents 6e8aba8 + 2a27f81 commit 9819fc0

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

cmd/internal/migrations/v3/parser_methods.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func MigrateParserMethods(cmd *cobra.Command, cwd string, _, _ *semver.Version) error {
1515
changed, err := internal.ChangeFileContent(cwd, func(content string) string {
1616
orig := content
17-
re := regexp.MustCompile(`\.(BodyParser|CookieParser|ParamsParser|QueryParser)\(`)
17+
re := regexp.MustCompile(`\.(AllParams|BodyParser|CookieParser|ParamsParser|QueryParser)\(`)
1818
matches := re.FindAllStringSubmatchIndex(content, -1)
1919
if len(matches) == 0 {
2020
return content
@@ -49,6 +49,8 @@ func MigrateParserMethods(cmd *cobra.Command, cwd string, _, _ *semver.Version)
4949
if isFiberCtx(orig, ident) {
5050
var repl string
5151
switch method {
52+
case "AllParams":
53+
repl = ".Bind().URI("
5254
case "BodyParser":
5355
repl = ".Bind().Body("
5456
case "CookieParser":

cmd/internal/migrations/v3/parser_methods_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func handler(c fiber.Ctx) error {
4040
assert.Contains(t, content, ".Bind().Cookie(&v)")
4141
assert.Contains(t, content, ".Bind().URI(&v)")
4242
assert.Contains(t, content, ".Bind().Query(&v)")
43+
assert.Contains(t, content, ".Bind().URI(&p)")
4344
assert.Contains(t, buf.String(), "Migrating parser methods")
4445
}
4546

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package v3
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
semver "github.com/Masterminds/semver/v3"
8+
"github.com/spf13/cobra"
9+
10+
"github.com/gofiber/cli/cmd/internal"
11+
)
12+
13+
func MigrateSendFileConfig(cmd *cobra.Command, cwd string, _, _ *semver.Version) error {
14+
changed, err := internal.ChangeFileContent(cwd, func(content string) string {
15+
return replaceCall(content, ".SendFile", func(call string, args []string) string {
16+
if len(args) != 2 {
17+
return call
18+
}
19+
20+
arg := strings.TrimSpace(args[1])
21+
if arg == "" {
22+
return call
23+
}
24+
if strings.HasPrefix(arg, "fiber.SendFile") || strings.HasPrefix(arg, "&fiber.SendFile") {
25+
return call
26+
}
27+
if strings.HasPrefix(arg, "SendFile{") || strings.HasPrefix(arg, "&SendFile{") {
28+
return call
29+
}
30+
31+
return fmt.Sprintf(".SendFile(%s, fiber.SendFile{Compress: %s})", args[0], args[1])
32+
})
33+
})
34+
if err != nil {
35+
return fmt.Errorf("failed to migrate SendFile calls: %w", err)
36+
}
37+
if !changed {
38+
return nil
39+
}
40+
41+
cmd.Println("Migrating SendFile compress flag")
42+
return nil
43+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package v3_test
2+
3+
import (
4+
"bytes"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
11+
"github.com/gofiber/cli/cmd/internal/migrations/v3"
12+
)
13+
14+
func Test_MigrateSendFileConfig(t *testing.T) {
15+
t.Parallel()
16+
17+
dir, err := os.MkdirTemp("", "msendfile")
18+
require.NoError(t, err)
19+
defer func() { require.NoError(t, os.RemoveAll(dir)) }()
20+
21+
file := writeTempFile(t, dir, `package main
22+
import "github.com/gofiber/fiber/v2"
23+
24+
func handler(c fiber.Ctx) error {
25+
if err := c.SendFile("./public/index.html", true); err != nil {
26+
return err
27+
}
28+
return c.SendFile("./public/file.txt", compressEnabled)
29+
}
30+
`)
31+
32+
var buf bytes.Buffer
33+
cmd := newCmd(&buf)
34+
require.NoError(t, v3.MigrateSendFileConfig(cmd, dir, nil, nil))
35+
36+
content := readFile(t, file)
37+
assert.Contains(t, content, `c.SendFile("./public/index.html", fiber.SendFile{Compress: true})`)
38+
assert.Contains(t, content, `c.SendFile("./public/file.txt", fiber.SendFile{Compress: compressEnabled})`)
39+
assert.Contains(t, buf.String(), "Migrating SendFile compress flag")
40+
}
41+
42+
func Test_MigrateSendFileConfig_Idempotent(t *testing.T) {
43+
t.Parallel()
44+
45+
dir, err := os.MkdirTemp("", "msendfile-idempotent")
46+
require.NoError(t, err)
47+
defer func() { require.NoError(t, os.RemoveAll(dir)) }()
48+
49+
file := writeTempFile(t, dir, `package main
50+
import "github.com/gofiber/fiber/v2"
51+
52+
func handler(c fiber.Ctx) error {
53+
return c.SendFile("./public/index.html", true)
54+
}
55+
`)
56+
57+
var buf bytes.Buffer
58+
cmd := newCmd(&buf)
59+
require.NoError(t, v3.MigrateSendFileConfig(cmd, dir, nil, nil))
60+
61+
migrated := readFile(t, file)
62+
assert.Contains(t, migrated, `c.SendFile("./public/index.html", fiber.SendFile{Compress: true})`)
63+
assert.Contains(t, buf.String(), "Migrating SendFile compress flag")
64+
65+
var second bytes.Buffer
66+
secondCmd := newCmd(&second)
67+
require.NoError(t, v3.MigrateSendFileConfig(secondCmd, dir, nil, nil))
68+
69+
assert.Equal(t, migrated, readFile(t, file))
70+
assert.Empty(t, second.String())
71+
}

0 commit comments

Comments
 (0)