-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
customtype: add ability to register any custom type
- Loading branch information
Showing
4 changed files
with
87 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package customtype | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"reflect" | ||
"sync" | ||
) | ||
|
||
var ( | ||
customTypesMux sync.Mutex | ||
customTypes = make(map[reflect.Type]func(any) ast.Expr) | ||
) | ||
|
||
// Register registers a type that for representation in a custom manner with | ||
// valast. If valast encounters a value or pointer to a value of this type, it | ||
// will use the given render func to generate the appropriate AST representation. | ||
// | ||
// This is useful if a type's fields are private, and can only be represented | ||
// through a constructor - see stdtypes.go for examples. | ||
// | ||
// This mechanism currently only works with struct types. | ||
func Register[T any](render func(value T) ast.Expr) { | ||
customTypesMux.Lock() | ||
var zero T | ||
t := reflect.TypeOf(zero) | ||
if _, exists := customTypes[t]; exists { | ||
panic(fmt.Sprintf("%T already registered", zero)) | ||
} | ||
customTypes[t] = func(value any) ast.Expr { return render(value.(T)) } | ||
customTypesMux.Unlock() | ||
} | ||
|
||
// Is indicates if the given reflect.Type has a custom AST representation | ||
// generator registered. | ||
func Is(rt reflect.Type) (func(any) ast.Expr, bool) { | ||
customTypesMux.Lock() | ||
defer customTypesMux.Unlock() | ||
|
||
t, ok := customTypes[rt] | ||
return t, ok | ||
} |
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,39 @@ | ||
package valast | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/token" | ||
"time" | ||
|
||
"github.com/hexops/valast/customtype" | ||
) | ||
|
||
// Register custom reprsentations of common structs from stdlib that only | ||
// contain unexported fields. | ||
func init() { | ||
// For time.Time, returns the AST expression equivalent of: | ||
// | ||
// time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) | ||
customtype.Register(func(t time.Time) ast.Expr { | ||
return &ast.CallExpr{ | ||
Fun: &ast.SelectorExpr{ | ||
X: &ast.Ident{Name: "time"}, | ||
Sel: &ast.Ident{Name: "Date"}, | ||
}, | ||
Args: []ast.Expr{ | ||
&ast.BasicLit{Kind: token.INT, Value: fmt.Sprintf("%d", t.Year())}, | ||
&ast.BasicLit{Kind: token.INT, Value: fmt.Sprintf("%d", t.Month())}, | ||
&ast.BasicLit{Kind: token.INT, Value: fmt.Sprintf("%d", t.Day())}, | ||
&ast.BasicLit{Kind: token.INT, Value: fmt.Sprintf("%d", t.Hour())}, | ||
&ast.BasicLit{Kind: token.INT, Value: fmt.Sprintf("%d", t.Minute())}, | ||
&ast.BasicLit{Kind: token.INT, Value: fmt.Sprintf("%d", t.Second())}, | ||
&ast.BasicLit{Kind: token.INT, Value: fmt.Sprintf("%d", t.Nanosecond())}, | ||
&ast.SelectorExpr{ | ||
X: &ast.Ident{Name: "time"}, | ||
Sel: &ast.Ident{Name: t.Location().String()}, | ||
}, | ||
}, | ||
} | ||
}) | ||
} |
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