@@ -37,6 +37,12 @@ type GoParser struct {
37
37
fileSet * token.FileSet
38
38
}
39
39
40
+ // NewGolangParser returns a new GoParser object.
41
+ // This object is responsible for converting Go types into the intermediate
42
+ // typescript AST representation.
43
+ // All configuration of the GoParser should be done before calling
44
+ // 'ToTypescript'.
45
+ // For usage, see 'ExampleGeneration' in convert_test.go.
40
46
func NewGolangParser () (* GoParser , error ) {
41
47
fileSet := token .NewFileSet ()
42
48
config := & packages.Config {
@@ -66,8 +72,18 @@ func (p *GoParser) IncludeCustomDeclaration(mappings map[string]TypeOverride) {
66
72
}
67
73
}
68
74
69
- // IncludeCustom only works for basic literal types and non-parameterized reference types.
70
- func (p * GoParser ) IncludeCustom (mappings map [string ]string ) error {
75
+ type GolangType = string
76
+
77
+ // IncludeCustom takes in a remapping of golang types.
78
+ // Both the key and value of the map should be valid golang types.
79
+ // The key is the type to override, and the value is the new type.
80
+ // Typescript will be generated with the new type.
81
+ //
82
+ // Only named types can be overridden.
83
+ // Examples:
84
+ // "github.com/your/repo/pkg.ExampleType": "string"
85
+ // "time.Time": "string"
86
+ func (p * GoParser ) IncludeCustom (mappings map [GolangType ]GolangType ) error {
71
87
for k , v := range mappings {
72
88
// Make sure it parses
73
89
_ , err := parseExpression (v )
@@ -114,6 +130,7 @@ func (p *GoParser) Include(directory string, generate bool) error {
114
130
}
115
131
116
132
// ToTypescript translates the Go types into the intermediate typescript AST
133
+ // The returned typescript object can be mutated before serializing.
117
134
func (p * GoParser ) ToTypescript () (* Typescript , error ) {
118
135
typescript := & Typescript {
119
136
typescriptNodes : make (map [string ]* typescriptNode ),
0 commit comments