-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
50 lines (42 loc) · 1.25 KB
/
main.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
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"time"
"github.com/coder/guts"
"github.com/coder/guts/config"
)
// SimpleType is a simple struct with a generic type
type SimpleType[T comparable] struct {
FieldString string
FieldInt int
FieldComparable T
FieldTime time.Time
}
func main() {
golang, _ := guts.NewGolangParser()
// Generate the typescript types for this package
_ = golang.IncludeGenerate("github.com/coder/guts/example/simple")
// Map time.Time to string
_ = golang.IncludeCustom(map[string]string{
"time.Time": "string",
})
// Convert the golang types to typescript AST
ts, _ := golang.ToTypescript()
// ApplyMutations allows adding in generation opinions to the typescript output.
// The basic generator has no opinions, so mutations are required to make the output
// more usable and idiomatic.
ts.ApplyMutations(
// Export all top level types
config.ExportTypes,
// Readonly changes all fields and types to being immutable.
// Useful if the types are only used for api responses, which should
// not be modified.
//config.ReadOnly,
)
// to see the AST tree
//ts.ForEach(func(key string, node bindings.Node) {
// walk.Walk(walk.PrintingVisitor(0), node.Node)
//})
output, _ := ts.Serialize()
fmt.Println(output)
}