@@ -37,6 +37,12 @@ type GoParser struct {
3737 fileSet * token.FileSet
3838}
3939
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.
4046func NewGolangParser () (* GoParser , error ) {
4147 fileSet := token .NewFileSet ()
4248 config := & packages.Config {
@@ -66,8 +72,18 @@ func (p *GoParser) IncludeCustomDeclaration(mappings map[string]TypeOverride) {
6672 }
6773}
6874
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 {
7187 for k , v := range mappings {
7288 // Make sure it parses
7389 _ , err := parseExpression (v )
@@ -114,6 +130,7 @@ func (p *GoParser) Include(directory string, generate bool) error {
114130}
115131
116132// ToTypescript translates the Go types into the intermediate typescript AST
133+ // The returned typescript object can be mutated before serializing.
117134func (p * GoParser ) ToTypescript () (* Typescript , error ) {
118135 typescript := & Typescript {
119136 typescriptNodes : make (map [string ]* typescriptNode ),
0 commit comments