-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add casttype support for scalar types
- Loading branch information
Showing
16 changed files
with
1,085 additions
and
95 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package patch | ||
|
||
import ( | ||
"go/ast" | ||
"go/types" | ||
"log" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/ast/astutil" | ||
) | ||
|
||
func (p *Patcher) patchTypeDef(id *ast.Ident, obj types.Object) { | ||
castType, ok := p.fieldCastType[obj] | ||
if !ok { | ||
return | ||
} | ||
|
||
parent := p.findParentNode(id) | ||
if pkg, name := packageAndName(castType); pkg != "" { | ||
f := p.fileOf(id) | ||
pkgImport := strings.Replace(strings.Replace(pkg, "/", "_", -1), ".", "_", -1) | ||
astutil.AddNamedImport(p.fset, f, pkgImport, pkg) | ||
castType = pkgImport + "." + name | ||
} | ||
// Cast Field definition | ||
if id.Obj != nil && id.Obj.Decl != nil { | ||
v, ok := id.Obj.Decl.(*ast.Field) | ||
if !ok { | ||
log.Printf("Warning: casttype declared for non-field object: %v `%s`", obj, castType) | ||
return | ||
} | ||
t, ok := v.Type.(*ast.Ident) | ||
if ok { | ||
t.Name = castType | ||
return | ||
} | ||
} | ||
switch obj.Type().(type) { | ||
// Cast Getter signature | ||
case *types.Signature: | ||
n, ok := parent.(*ast.FuncDecl) | ||
if !ok { | ||
log.Printf("Warning: unexpected type for getter: %v `%T`", obj, parent) | ||
break | ||
} | ||
if l := len(n.Type.Results.List); l != 1 { | ||
log.Printf("Warning: unexpected return count for getter: %v `%d`", obj, l) | ||
return | ||
} | ||
if ident, ok := n.Type.Results.List[0].Type.(*ast.Ident); ok { | ||
ident.Name = castType | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (p *Patcher) patchTypeUsage(id *ast.Ident, obj types.Object) { | ||
desiredType, ok := p.fieldCastType[obj] | ||
if !ok { | ||
return | ||
} | ||
var originalType string | ||
switch t := obj.Type().(type) { | ||
case *types.Basic: | ||
originalType = t.Name() | ||
case *types.Signature: | ||
if t.Results().Len() != 1 { | ||
return | ||
} | ||
originalType = t.Results().At(0).Type().String() | ||
} | ||
usageNode := p.findParentNode(id) | ||
pkgPath, pkgName := packageAndName(desiredType) | ||
pkgImport := strings.Replace(strings.Replace(pkgPath, "/", "_", -1), ".", "_", -1) | ||
if pkgPath != "" { | ||
desiredType = pkgImport + "." + pkgName | ||
} | ||
cast := func(as string, expr ast.Expr) ast.Expr { | ||
if pkgPath != "" && as == desiredType { | ||
f := p.fileOf(id) | ||
astutil.AddNamedImport(p.fset, f, pkgImport, pkgPath) | ||
} | ||
return &ast.CallExpr{ | ||
Fun: &ast.Ident{ | ||
Name: as, | ||
}, | ||
Args: []ast.Expr{expr}, | ||
} | ||
} | ||
parentNode := p.findParentNode(usageNode) | ||
|
||
switch usage := usageNode.(type) { | ||
case *ast.SelectorExpr: | ||
switch parentExpr := parentNode.(type) { | ||
case *ast.AssignStmt: | ||
if len(parentExpr.Lhs) != len(parentExpr.Rhs) { | ||
return | ||
} | ||
for i := range parentExpr.Lhs { | ||
if parentExpr.Lhs[i] == usage { | ||
parentExpr.Rhs[i] = cast(desiredType, parentExpr.Rhs[i]) | ||
return | ||
} | ||
} | ||
for i := range parentExpr.Rhs { | ||
if parentExpr.Rhs[i] == usage { | ||
parentExpr.Rhs[i] = cast(originalType, parentExpr.Rhs[i]) | ||
return | ||
} | ||
} | ||
case *ast.CallExpr: | ||
parent := p.findParentNode(parentExpr) | ||
assign, isAssign := parent.(*ast.AssignStmt) | ||
if parentExpr.Fun == usage && isAssign { | ||
for i := range assign.Rhs { | ||
if assign.Rhs[i] == parentExpr { | ||
assign.Rhs[i] = cast(originalType, assign.Rhs[i]) | ||
return | ||
} | ||
} | ||
} | ||
call, isCall := parent.(*ast.CallExpr) | ||
if isCall { | ||
for i := range call.Args { | ||
if call.Args[i] == parentExpr { | ||
call.Args[i] = cast(originalType, call.Args[i]) | ||
return | ||
} | ||
} | ||
} | ||
for i, v := range parentExpr.Args { | ||
if v == usage { | ||
parentExpr.Args[i] = cast(originalType, usage) | ||
return | ||
} | ||
} | ||
case *ast.BinaryExpr: | ||
if parentExpr.X == usage { | ||
parentExpr.X = cast(originalType, parentExpr.X) | ||
} | ||
if parentExpr.Y == usage { | ||
parentExpr.Y = cast(originalType, parentExpr.Y) | ||
} | ||
} | ||
case *ast.KeyValueExpr: | ||
if usage.Key == id { | ||
usage.Value = cast(desiredType, usage.Value) | ||
return | ||
} | ||
if usage.Value == id { | ||
usage.Value = cast(originalType, usage.Value) | ||
return | ||
} | ||
} | ||
} |
Oops, something went wrong.