Skip to content

Commit d9cf060

Browse files
committed
add example golang test
1 parent 5b32fd4 commit d9cf060

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

convert.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
4046
func 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.
117134
func (p *GoParser) ToTypescript() (*Typescript, error) {
118135
typescript := &Typescript{
119136
typescriptNodes: make(map[string]*typescriptNode),

convert_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package guts_test
88

99
import (
1010
"flag"
11+
"fmt"
1112
"os"
1213
"path/filepath"
1314
"strings"
@@ -19,6 +20,42 @@ import (
1920
"github.com/coder/guts/config"
2021
)
2122

23+
func ExampleNewGolangParser() {
24+
// gen will convert the golang package to the typescript AST.
25+
// Configure it before calling ToTypescript().
26+
gen, _ := guts.NewGolangParser()
27+
28+
// Pass in the directory of the package you want to convert.
29+
// You can mark a package as 'false' to include it as a reference, but not
30+
// generate types for it.
31+
_ = gen.Include("github.com/coder/guts/testdata/generics", true)
32+
33+
// Default type mappings are useful, feel free to add your own
34+
gen.IncludeCustomDeclaration(config.StandardMappings())
35+
_ = gen.IncludeCustom(map[string]string{
36+
// To configure a custom type for a golang type, use the full package path.
37+
"github.com/coder/guts/testdata/generics.ExampleType": "string",
38+
// You can use golang type syntax to specify a type.
39+
"github.com/coder/guts/testdata/generics.AnotherExampleType": "map[string]*string",
40+
})
41+
42+
// ts is the typescript AST. It can be mutated before serializing.
43+
ts, _ := gen.ToTypescript()
44+
45+
ts.ApplyMutations(
46+
// Generates a constant which lists all enum values.
47+
config.EnumLists,
48+
// Adds 'readonly' to all interface fields.
49+
config.ReadOnly,
50+
// Adds 'export' to all top level types.
51+
config.ExportTypes,
52+
)
53+
54+
output, _ := ts.Serialize()
55+
// Output is the typescript file text
56+
fmt.Println(output)
57+
}
58+
2259
// updateGoldenFiles is a flag that can be set to update golden files.
2360
var updateGoldenFiles = flag.Bool("update", false, "Update golden files")
2461

0 commit comments

Comments
 (0)