MCPcopy Index your code
hub / github.com/coder/guts / ExampleNewGolangParser

Function ExampleNewGolangParser

convert_test.go:23–57  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

21)
22
23func 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.IncludeGenerate("github.com/coder/guts/testdata/generics")
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
59// updateGoldenFiles is a flag that can be set to update golden files.
60var updateGoldenFiles = flag.Bool("update", false, "Update golden files")

Callers

nothing calls this directly

Calls 8

NewGolangParserFunction · 0.92
StandardMappingsFunction · 0.92
IncludeGenerateMethod · 0.80
IncludeCustomMethod · 0.80
ToTypescriptMethod · 0.80
ApplyMutationsMethod · 0.80
SerializeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…