github.com/xaionaro-go/rand@v0.0.0-20191005105903-aba1befc54a5/internal/autogen/write_files.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "sort" 7 "strings" 8 ) 9 10 func methodToValues(method *Method) map[string]interface{} { 11 return map[string]interface{}{ 12 `MethodName`: method.Name, 13 `InitCode`: method.InitCode, 14 `GetValueCode`: method.GetValueCode, 15 `ResultVariable`: method.ResultVariable, 16 `FinishCode`: method.FinishCode, 17 `ResultSize`: method.ResultSize, 18 `AdditionalInfo`: method.AdditionalInfo, 19 } 20 } 21 22 func WriteFiles(templates *Templates, methods Methods) error { 23 fMap := map[string]*os.File{} 24 getFileFunc := func(fileName string, packageName string, imports []string) *os.File { 25 if f := fMap[fileName]; f != nil { 26 return f 27 } 28 29 f, err := os.Create(fileName) 30 if err != nil { 31 panic(err) 32 } 33 34 sort.Strings(imports) 35 36 var stdImports, extImports []string 37 for _, _import := range imports { 38 importWords := strings.Split(_import, `/`) 39 if len(strings.Split(importWords[0], `.`)) > 1 { 40 extImports = append(extImports, _import) 41 } else { 42 stdImports = append(stdImports, _import) 43 } 44 } 45 var importsBlock string 46 if len(stdImports) > 0 { 47 importsBlock += "\t\"" + strings.Join(stdImports, "\"\n\t\"") + "\"\n" 48 } 49 if len(extImports) > 0 { 50 if len(importsBlock) > 0 { 51 importsBlock += "\n" 52 } 53 importsBlock += "\t\"" + strings.Join(extImports, "\"\n\t\"") + "\"\n" 54 } 55 _, _ = fmt.Fprintf(f, "package "+packageName+"\n\nimport (\n"+importsBlock+")\n\n") 56 _, _ = fmt.Fprintln(f, `// This file was automatically generated by github.com/xaionaro-go/rand/internal/autogen`) 57 58 fMap[fileName] = f 59 return f 60 } 61 62 defer func() { 63 for _, f := range fMap { 64 _ = f.Close() 65 } 66 } () 67 68 for _, method := range methods { 69 values := methodToValues(method) 70 71 for _, enableReseed := range []bool{false, true} { 72 values[`EnableReseed`] = enableReseed 73 74 { 75 for _, isXORRead := range []bool{false, true} { 76 values[`IsXORRead`] = isXORRead 77 78 if err := templates.Read.Execute( 79 getFileFunc(`read_autogenerated.go`, `mathrand`, []string{`unsafe`}), 80 values, 81 ); err != nil { 82 return err 83 } 84 85 if err := templates.TestRead.Execute( 86 getFileFunc(`read_autogenerated_test.go`, `mathrand_test`, []string{`testing`, `github.com/xaionaro-go/rand/mathrand`, `github.com/stretchr/testify/assert`}), 87 values, 88 ); err != nil { 89 return err 90 } 91 } 92 93 delete(values, `IsXORRead`) 94 } 95 } 96 97 { 98 var testFileName string 99 switch method.ResultSize { 100 case 4: 101 testFileName = `uint32_autogenerated_test.go` 102 case 8: 103 testFileName = `uint64_autogenerated_test.go` 104 default: 105 panic(fmt.Sprint(`unknown ResultSize`, method.ResultSize)) 106 } 107 if err := templates.TestMethod.Execute( 108 getFileFunc(testFileName, `mathrand_test`, []string{`testing`, `github.com/xaionaro-go/rand/mathrand`}), 109 values, 110 ); err != nil { 111 return err 112 } 113 } 114 } 115 116 return nil 117 }