-
Notifications
You must be signed in to change notification settings - Fork 18
feat(migrator): implement migration for params tag keys to use uri format #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package v3 | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "go/ast" | ||
| "go/format" | ||
| "go/parser" | ||
| "go/token" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| semver "github.com/Masterminds/semver/v3" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/gofiber/cli/cmd/internal" | ||
| ) | ||
|
|
||
| func MigrateParamsTagKeys(cmd *cobra.Command, cwd string, _, _ *semver.Version) error { | ||
| changed, err := internal.ChangeFileContent(cwd, rewriteParamsTagKeys) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to migrate params tag keys: %w", err) | ||
| } | ||
| if !changed { | ||
| return nil | ||
| } | ||
|
|
||
| cmd.Println("Migrating params tag keys") | ||
| return nil | ||
| } | ||
|
|
||
| func rewriteParamsTagKeys(content string) string { | ||
| fset := token.NewFileSet() | ||
| file, err := parser.ParseFile(fset, "", content, parser.ParseComments) | ||
| if err != nil { | ||
| return content | ||
| } | ||
|
|
||
| modified := false | ||
| ast.Inspect(file, func(n ast.Node) bool { | ||
| field, ok := n.(*ast.Field) | ||
| if !ok || field.Tag == nil { | ||
| return true | ||
| } | ||
|
|
||
| tagLiteral, err := strconv.Unquote(field.Tag.Value) | ||
| if err != nil || !strings.Contains(tagLiteral, `params:"`) { | ||
| return true | ||
| } | ||
|
|
||
| updatedTag := strings.ReplaceAll(tagLiteral, `params:"`, `uri:"`) | ||
| if updatedTag == tagLiteral { | ||
| return true | ||
| } | ||
|
|
||
| field.Tag.Value = "`" + updatedTag + "`" | ||
|
Comment on lines
+46
to
+56
|
||
| modified = true | ||
| return true | ||
| }) | ||
|
|
||
| if !modified { | ||
| return content | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| if err := format.Node(&buf, fset, file); err != nil { | ||
| return content | ||
| } | ||
|
|
||
| return buf.String() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package v3_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/gofiber/cli/cmd/internal/migrations/v3" | ||
| ) | ||
|
|
||
| func Test_MigrateParamsTagKeys_RewriteWithParserCalls(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir, err := os.MkdirTemp("", "mptagparser") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| file := writeTempFile(t, dir, `package main | ||
| import "github.com/gofiber/fiber/v2" | ||
|
|
||
| type params struct { | ||
| ID string `+"`params:\"id\" json:\"id\"`"+` | ||
| } | ||
|
|
||
| func handler(c fiber.Ctx) error { | ||
| var p params | ||
| if err := c.ParamsParser(&p); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
| `) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| require.NoError(t, v3.MigrateParamsTagKeys(cmd, dir, nil, nil)) | ||
|
|
||
| content := readFile(t, file) | ||
| assert.Contains(t, content, "c.ParamsParser(&p)") | ||
| assert.Contains(t, content, "`uri:\"id\" json:\"id\"`") | ||
| assert.NotContains(t, content, "`params:\"id\" json:\"id\"`") | ||
| assert.Contains(t, buf.String(), "Migrating params tag keys") | ||
| } | ||
|
|
||
| func Test_MigrateParamsTagKeys_RewriteForNonFiber(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir, err := os.MkdirTemp("", "mptagnonfiber") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| file := writeTempFile(t, dir, `package main | ||
| type params struct { | ||
| ID string `+"`params:\"id\"`"+` | ||
| } | ||
|
|
||
| type ctx struct{} | ||
| func (ctx) ParamsParser(any) {} | ||
| func handler(c ctx) { | ||
| var p params | ||
| c.ParamsParser(&p) | ||
| }`) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| require.NoError(t, v3.MigrateParamsTagKeys(cmd, dir, nil, nil)) | ||
|
|
||
| content := readFile(t, file) | ||
| assert.Contains(t, content, "c.ParamsParser(&p)") | ||
| assert.NotContains(t, content, "`params:\"id\"`") | ||
| assert.Contains(t, content, "`uri:\"id\"`") | ||
| assert.Contains(t, buf.String(), "Migrating params tag keys") | ||
| } | ||
|
|
||
| func Test_MigrateParamsTagKeys_RewriteWithoutParserCalls(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir, err := os.MkdirTemp("", "mptagonly") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| file := writeTempFile(t, dir, `package main | ||
| type params struct { | ||
| ID string `+"`params:\"id\" json:\"id\"`"+` | ||
| }`) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| require.NoError(t, v3.MigrateParamsTagKeys(cmd, dir, nil, nil)) | ||
|
|
||
| content := readFile(t, file) | ||
| assert.NotContains(t, content, "`params:\"id\" json:\"id\"`") | ||
| assert.Contains(t, content, "`uri:\"id\" json:\"id\"`") | ||
| assert.Contains(t, buf.String(), "Migrating params tag keys") | ||
| } | ||
|
|
||
| func Test_MigrateParamsTagKeys_NoChanges(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir, err := os.MkdirTemp("", "mptagnochange") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| file := writeTempFile(t, dir, `package main | ||
| type params struct { | ||
| ID string `+"`uri:\"id\" json:\"id\"`"+` | ||
| }`) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| require.NoError(t, v3.MigrateParamsTagKeys(cmd, dir, nil, nil)) | ||
|
|
||
| content := readFile(t, file) | ||
| assert.Contains(t, content, "`uri:\"id\" json:\"id\"`") | ||
| assert.NotContains(t, buf.String(), "Migrating params tag keys") | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The modernize target sets GOTOOLCHAIN=$(GOVERSION), but GOVERSION isn’t defined anywhere in this Makefile. This will typically expand to an empty value and won’t select the intended toolchain; use $(TOOLCHAIN) here or define GOVERSION consistently.