-
Notifications
You must be signed in to change notification settings - Fork 21
feat: migrate logger generics #186
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package v3 | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
|
|
||
| semver "github.com/Masterminds/semver/v3" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/gofiber/cli/cmd/internal" | ||
| ) | ||
|
|
||
| func MigrateLoggerGenerics(cmd *cobra.Command, cwd string, _, _ *semver.Version) error { | ||
| reAllLogger := regexp.MustCompile(`(\w+)\.AllLogger([^\w\[]|$)`) | ||
| reConfigurableLogger := regexp.MustCompile(`(\w+)\.ConfigurableLogger([^\w\[]|$)`) | ||
| reDefaultLogger := regexp.MustCompile(`(\w+)\.DefaultLogger\(\)`) | ||
| reSetLogger := regexp.MustCompile(`(\w+)\.SetLogger\(`) | ||
| reLoggerToWriter := regexp.MustCompile(`(\w+)\.LoggerToWriter\(`) | ||
|
|
||
| changed, err := internal.ChangeFileContent(cwd, func(content string) string { | ||
| content = reAllLogger.ReplaceAllString(content, `$1.AllLogger[any]$2`) | ||
| content = reConfigurableLogger.ReplaceAllString(content, `$1.ConfigurableLogger[any]$2`) | ||
| content = reDefaultLogger.ReplaceAllString(content, `$1.DefaultLogger[any]()`) | ||
| content = reSetLogger.ReplaceAllString(content, `$1.SetLogger[any](`) | ||
| content = reLoggerToWriter.ReplaceAllString(content, `$1.LoggerToWriter[any](`) | ||
| return content | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to migrate logger generics: %w", err) | ||
| } | ||
| if !changed { | ||
| return nil | ||
| } | ||
|
|
||
| cmd.Println("Migrating logger generics") | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| 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_MigrateLoggerGenerics(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir, err := os.MkdirTemp("", "mloggenericstest") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| file := writeTempFile(t, dir, `package main | ||
| import ( | ||
| fiberlog "github.com/gofiber/fiber/v2/log" | ||
| ) | ||
| var _ fiberlog.AllLogger = (*customLogger)(nil) | ||
| var _ fiberlog.ConfigurableLogger = (*customLogger)(nil) | ||
| func main() { | ||
| logger := fiberlog.DefaultLogger() | ||
| fiberlog.SetLogger(logger) | ||
| _ = fiberlog.LoggerToWriter(logger, fiberlog.LevelInfo) | ||
| } | ||
| `) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| require.NoError(t, v3.MigrateLoggerGenerics(cmd, dir, nil, nil)) | ||
|
|
||
| content := readFile(t, file) | ||
| assert.NotContains(t, content, "AllLogger =") | ||
| assert.NotContains(t, content, "ConfigurableLogger =") | ||
| assert.Contains(t, content, "AllLogger[any]") | ||
| assert.Contains(t, content, "ConfigurableLogger[any]") | ||
| assert.Contains(t, content, "DefaultLogger[any]()") | ||
| assert.Contains(t, content, "SetLogger[any](") | ||
| assert.Contains(t, content, "LoggerToWriter[any](") | ||
| assert.Contains(t, buf.String(), "Migrating logger generics") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For performance and to follow Go best practices, regular expressions should be compiled only once. You can define them as package-level variables using
regexp.MustCompile. This avoids recompiling them every timeMigrateLoggerGenericsis called. While the performance impact is likely negligible in this command-line tool context, this change improves code structure and adheres to common Go idioms.