-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmain.go
More file actions
62 lines (51 loc) · 1.63 KB
/
Copy pathmain.go
File metadata and controls
62 lines (51 loc) · 1.63 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
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
}
type SecondaryType struct {
FieldString string
}
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",
})
// Common standard mappings exist as an easy starting place.
golang.IncludeCustomDeclaration(config.StandardMappings())
// Exclude SecondaryType from output
_ = golang.ExcludeCustom("github.com/coder/guts/example/simple.SecondaryType")
// Optionally bring over the golang comments to the typescript output
golang.PreserveComments()
// 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)
}