-
-
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 (#27)
* customtype: add ability to register any custom type * make customtype internal, add valast.RegisterType * make reflect.Array support custom type (#1) --------- Co-authored-by: Joe Chen <jc@unknwon.io>
- Loading branch information
Showing
5 changed files
with
98 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,35 @@ | ||
package customtype | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"reflect" | ||
"sync" | ||
) | ||
|
||
var ( | ||
customTypesMux sync.Mutex | ||
customTypes = make(map[reflect.Type]func(any) ast.Expr) | ||
) | ||
|
||
// See the top-level valast.RegisterType docstring for details. | ||
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,37 @@ | ||
package valast | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/token" | ||
"time" | ||
) | ||
|
||
// 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) | ||
RegisterType(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
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