-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstdtypes.go
37 lines (35 loc) · 1.11 KB
/
stdtypes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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()},
},
},
}
})
}