|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path" |
| 9 | + "strings" |
| 10 | + "text/template" |
| 11 | +) |
| 12 | + |
| 13 | +// templateContext is the context passed to the template engine for generating tuple code and test files. |
| 14 | +type templateContext struct { |
| 15 | + Indexes []int |
| 16 | + Len int |
| 17 | + TypeName string |
| 18 | + TypeDecl string |
| 19 | + GenericTypesDecl string |
| 20 | + GenericTypesForward string |
| 21 | +} |
| 22 | + |
| 23 | +const minTupleLength = 1 |
| 24 | +const maxTupleLength = 9 |
| 25 | + |
| 26 | +var funcMap = template.FuncMap{ |
| 27 | + "quote": func(value interface{}) string { |
| 28 | + return fmt.Sprintf("%q", fmt.Sprint(value)) |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | +//go:embed tuple.tpl |
| 33 | +var codeTplContent string |
| 34 | + |
| 35 | +//go:embed tuple_test.tpl |
| 36 | +var testTplContent string |
| 37 | + |
| 38 | +// main generates the tuple code and test files by executing the template engine for the "tuple.tpl" and "tuple_test.tpl" files. |
| 39 | +func main() { |
| 40 | + outputDir := os.Args[1] |
| 41 | + |
| 42 | + codeTpl, err := template.New("tuple").Funcs(funcMap).Parse(codeTplContent) |
| 43 | + if err != nil { |
| 44 | + panic(err) |
| 45 | + } |
| 46 | + |
| 47 | + testTpl, err := template.New("tuple_test").Funcs(funcMap).Parse(testTplContent) |
| 48 | + if err != nil { |
| 49 | + panic(err) |
| 50 | + } |
| 51 | + |
| 52 | + for tupleLength := minTupleLength; tupleLength <= maxTupleLength; tupleLength++ { |
| 53 | + indexes := make([]int, tupleLength) |
| 54 | + for index := range indexes { |
| 55 | + indexes[index] = index + 1 |
| 56 | + } |
| 57 | + |
| 58 | + decl := genTypesDecl(indexes) |
| 59 | + forward := genTypesForward(indexes) |
| 60 | + context := templateContext{ |
| 61 | + Indexes: indexes, |
| 62 | + Len: tupleLength, |
| 63 | + TypeName: fmt.Sprintf("T%d[%s]", tupleLength, forward), |
| 64 | + TypeDecl: fmt.Sprintf("T%d[%s]", tupleLength, decl), |
| 65 | + GenericTypesDecl: decl, |
| 66 | + GenericTypesForward: forward, |
| 67 | + } |
| 68 | + |
| 69 | + filesToGenerate := []struct { |
| 70 | + fullPath string |
| 71 | + tpl *template.Template |
| 72 | + }{ |
| 73 | + { |
| 74 | + fullPath: path.Join(outputDir, fmt.Sprintf("tuple%d.go", tupleLength)), |
| 75 | + tpl: codeTpl, |
| 76 | + }, |
| 77 | + { |
| 78 | + fullPath: path.Join(outputDir, fmt.Sprintf("tuple%d_test.go", tupleLength)), |
| 79 | + tpl: testTpl, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, file := range filesToGenerate { |
| 84 | + fmt.Printf("Generating file %q...\n", file.fullPath) |
| 85 | + generateFile(context, file.fullPath, file.tpl) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// generateFile generates the file at outputFilePath according to the template tpl. |
| 91 | +// The template engine is given the context parameter as data (can be used as "." in the templates). |
| 92 | +func generateFile(context templateContext, outputFilePath string, tpl *template.Template) { |
| 93 | + file, err := os.OpenFile(outputFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(0600)) |
| 94 | + if err != nil { |
| 95 | + panic(err) |
| 96 | + } |
| 97 | + |
| 98 | + defer func() { |
| 99 | + if err := file.Close(); err != nil { |
| 100 | + panic(err) |
| 101 | + } |
| 102 | + }() |
| 103 | + |
| 104 | + err = tpl.Execute(file, context) |
| 105 | + if err != nil { |
| 106 | + panic(err) |
| 107 | + } |
| 108 | + |
| 109 | + cmd := exec.Command("gofmt", "-s", "-w", outputFilePath) |
| 110 | + if err := cmd.Run(); err != nil { |
| 111 | + panic(err) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// genTypesDecl generates a "TypeParamDecl" (https://tip.golang.org/ref/spec#Type_parameter_lists) expression, |
| 116 | +// used to declare generic types for a type or a function, according to the given element indexes. |
| 117 | +func genTypesDecl(indexes []int) string { |
| 118 | + sep := make([]string, len(indexes)) |
| 119 | + for index, typeIndex := range indexes { |
| 120 | + sep[index] = fmt.Sprintf("Ty%d any", typeIndex) |
| 121 | + } |
| 122 | + |
| 123 | + return strings.Join(sep, ", ") |
| 124 | +} |
| 125 | + |
| 126 | +// genTypesForward generates a "TypeParamList" (https://tip.golang.org/ref/spec#Type_parameter_lists) expression, |
| 127 | +// used to instantiate generic classes, according to the given element indexes. |
| 128 | +// Forward refers to forwarding already declared type parameters in order to instantiate the type. |
| 129 | +func genTypesForward(indexes []int) string { |
| 130 | + sep := make([]string, len(indexes)) |
| 131 | + for index, typeIndex := range indexes { |
| 132 | + sep[index] = fmt.Sprintf("Ty%d", typeIndex) |
| 133 | + } |
| 134 | + |
| 135 | + return strings.Join(sep, ", ") |
| 136 | +} |
0 commit comments