-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Improve sortables detection (#1151)
Co-authored-by: chavacava <salvador.cavadini@gmail.com>
- Loading branch information
1 parent
72b91f0
commit cb74ccb
Showing
3 changed files
with
120 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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,60 @@ | ||
package astutils | ||
|
||
import "go/ast" | ||
|
||
// FuncSignatureIs returns true if the given func decl satisfies a signature characterized | ||
// by the given name, parameters types and return types; false otherwise. | ||
// | ||
// Example: to check if a function declaration has the signature Foo(int, string) (bool,error) | ||
// call to FuncSignatureIs(funcDecl,"Foo",[]string{"int","string"},[]string{"bool","error"}) | ||
func FuncSignatureIs(funcDecl *ast.FuncDecl, wantName string, wantParametersTypes, wantResultsTypes []string) bool { | ||
if wantName != funcDecl.Name.String() { | ||
return false // func name doesn't match expected one | ||
} | ||
|
||
funcParametersTypes := getTypeNames(funcDecl.Type.Params) | ||
if len(wantParametersTypes) != len(funcParametersTypes) { | ||
return false // func has not the expected number of parameters | ||
} | ||
|
||
funcResultsTypes := getTypeNames(funcDecl.Type.Results) | ||
if len(wantResultsTypes) != len(funcResultsTypes) { | ||
return false // func has not the expected number of return values | ||
} | ||
|
||
for i, wantType := range wantParametersTypes { | ||
if wantType != funcParametersTypes[i] { | ||
return false // type of a func's parameter does not match the type of the corresponding expected parameter | ||
} | ||
} | ||
|
||
for i, wantType := range wantResultsTypes { | ||
if wantType != funcResultsTypes[i] { | ||
return false // type of a func's return value does not match the type of the corresponding expected return value | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func getTypeNames(fields *ast.FieldList) []string { | ||
result := []string{} | ||
|
||
if fields == nil { | ||
return result | ||
} | ||
|
||
for _, field := range fields.List { | ||
typeName := field.Type.(*ast.Ident).Name | ||
if field.Names == nil { // unnamed field | ||
result = append(result, typeName) | ||
continue | ||
} | ||
|
||
for range field.Names { // add one type name for each field name | ||
result = append(result, typeName) | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains 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 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