()
| 21 | ) |
| 22 | |
| 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.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. |
| 60 | var updateGoldenFiles = flag.Bool("update", false, "Update golden files") |
nothing calls this directly
no test coverage detected
searching dependent graphs…