-
Notifications
You must be signed in to change notification settings - Fork 21
Improve session release migration targeting #254
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package v3 | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "go/ast" | ||
| "go/parser" | ||
| "go/token" | ||
| "path" | ||
| ) | ||
|
|
||
| // parseGoFile parses Go source content into an AST. It returns the parsed file | ||
| // or an error if the content cannot be parsed. | ||
| func parseGoFile(content string) (*ast.File, error) { | ||
| fset := token.NewFileSet() | ||
| file, err := parser.ParseFile(fset, "", content, parser.ParseComments) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("parse Go file: %w", err) | ||
| } | ||
| return file, nil | ||
| } | ||
|
|
||
| // collectImportAliases finds all import aliases for the given import path within | ||
| // the provided file. The default alias derived from the path basename is also | ||
| // included when the import does not specify an explicit name. | ||
| func collectImportAliases(file *ast.File, importPath string) map[string]struct{} { | ||
| aliases := make(map[string]struct{}) | ||
|
|
||
| for _, imp := range file.Imports { | ||
| if imp.Path == nil || imp.Path.Value == "" { | ||
| continue | ||
| } | ||
|
|
||
| if imp.Path.Value != "\""+importPath+"\"" { | ||
| continue | ||
| } | ||
|
|
||
| if imp.Name != nil { | ||
| if imp.Name.Name == "_" || imp.Name.Name == "." { | ||
| continue | ||
| } | ||
|
|
||
| aliases[imp.Name.Name] = struct{}{} | ||
| continue | ||
| } | ||
|
|
||
| aliases[path.Base(importPath)] = struct{}{} | ||
| } | ||
|
|
||
| return aliases | ||
| } | ||
|
|
||
| // collectAssignedCallIdents walks assignment statements and collects identifier | ||
| // names that are assigned the result of a call expression matching the provided | ||
| // predicate. | ||
| func collectAssignedCallIdents(file *ast.File, predicate func(*ast.CallExpr) bool) map[string]struct{} { | ||
| matches := make(map[string]struct{}) | ||
|
|
||
| ast.Inspect(file, func(n ast.Node) bool { | ||
| assign, ok := n.(*ast.AssignStmt) | ||
| if !ok { | ||
| return true | ||
| } | ||
|
|
||
| if len(assign.Rhs) == 1 { | ||
| // Capture all identifiers from a single call returning multiple values. | ||
| if call, ok := assign.Rhs[0].(*ast.CallExpr); ok && predicate(call) { | ||
| for _, lhs := range assign.Lhs { | ||
| if ident, ok := lhs.(*ast.Ident); ok && ident.Name != "_" { | ||
| matches[ident.Name] = struct{}{} | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| // Map each call on the RHS to its corresponding identifier on the LHS. | ||
| for idx, rhs := range assign.Rhs { | ||
| call, ok := rhs.(*ast.CallExpr) | ||
| if !ok || !predicate(call) { | ||
| continue | ||
| } | ||
|
|
||
| if idx < len(assign.Lhs) { | ||
| if ident, ok := assign.Lhs[idx].(*ast.Ident); ok && ident.Name != "_" { | ||
| matches[ident.Name] = struct{}{} | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| }) | ||
|
|
||
| return matches | ||
| } | ||
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,101 @@ | ||
| package v3 | ||
|
|
||
| import ( | ||
| "go/ast" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func Test_parseGoFile_InvalidContent(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| _, err := parseGoFile("package main\n func") | ||
| assert.Error(t, err) | ||
| } | ||
|
|
||
| func Test_collectImportAliases(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := map[string]struct { //nolint:govet // fieldalignment warning is not relevant for test data shapes | ||
| content string | ||
| importPath string | ||
| expected map[string]struct{} | ||
| }{ | ||
| "default alias": { | ||
| importPath: "github.com/gofiber/fiber/v3/middleware/session", | ||
| content: "package main\nimport \"github.com/gofiber/fiber/v3/middleware/session\"\n", | ||
| expected: map[string]struct{}{"session": {}}, | ||
| }, | ||
| "explicit alias": { | ||
| importPath: "github.com/gofiber/fiber/v3/middleware/session", | ||
| content: "package main\nimport sess \"github.com/gofiber/fiber/v3/middleware/session\"\n", | ||
| expected: map[string]struct{}{"sess": {}}, | ||
| }, | ||
| "blank import ignored": { | ||
| importPath: "github.com/gofiber/fiber/v3/middleware/session", | ||
| content: "package main\nimport _ \"github.com/gofiber/fiber/v3/middleware/session\"\n", | ||
| expected: map[string]struct{}{}, | ||
| }, | ||
| "dot import ignored": { | ||
| importPath: "github.com/gofiber/fiber/v3/middleware/session", | ||
| content: "package main\nimport . \"github.com/gofiber/fiber/v3/middleware/session\"\n", | ||
| expected: map[string]struct{}{}, | ||
| }, | ||
| "unrelated import": { | ||
| importPath: "github.com/gofiber/fiber/v3/middleware/session", | ||
| content: "package main\nimport \"github.com/example/other\"\n", | ||
| expected: map[string]struct{}{}, | ||
| }, | ||
| } | ||
|
|
||
| for name, tt := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| file, err := parseGoFile(tt.content) | ||
| require.NoError(t, err) | ||
|
|
||
| aliases := collectImportAliases(file, tt.importPath) | ||
| assert.Equal(t, tt.expected, aliases) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_collectAssignedCallIdents(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| content := `package main | ||
|
|
||
| func target() (int, error) { return 0, nil } | ||
| func other() int { return 1 } | ||
|
|
||
| func main() { | ||
| primary, secondary := target() | ||
| single := target() | ||
| _, captured := target() | ||
| value, err := other() | ||
| first, second := other(), target() | ||
| field.Name = target() | ||
| } | ||
| ` | ||
|
|
||
| file, err := parseGoFile(content) | ||
| require.NoError(t, err) | ||
|
|
||
| matches := collectAssignedCallIdents(file, func(call *ast.CallExpr) bool { | ||
| if ident, ok := call.Fun.(*ast.Ident); ok { | ||
| return ident.Name == "target" | ||
| } | ||
| return false | ||
| }) | ||
|
|
||
| assert.Contains(t, matches, "primary") | ||
| assert.Contains(t, matches, "secondary") | ||
| assert.Contains(t, matches, "single") | ||
|
ReneWerner87 marked this conversation as resolved.
|
||
| assert.Contains(t, matches, "captured") | ||
| assert.Contains(t, matches, "second") | ||
| assert.NotContains(t, matches, "value") | ||
| assert.NotContains(t, matches, "first") | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.