|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + "fmt" |
| 6 | + "flag" |
| 7 | + "strings" |
| 8 | + "os" |
| 9 | + "io" |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +var template = ` |
| 14 | +package %s |
| 15 | +
|
| 16 | +// DO NOT EDIT - this file was generated with |
| 17 | +// %s |
| 18 | +// On %s |
| 19 | +
|
| 20 | +import ( |
| 21 | + "github.com/tbshill/csv" |
| 22 | + "sync" |
| 23 | + "os" |
| 24 | + "time" |
| 25 | + %s |
| 26 | +) |
| 27 | +
|
| 28 | +type %s struct { |
| 29 | + LoadedTimestamp time.Time |
| 30 | + Filename string |
| 31 | + Rownum int |
| 32 | + Errs []error |
| 33 | + Data %s |
| 34 | +} |
| 35 | +
|
| 36 | +func New%s(filename string, row int, data %s) *%s { |
| 37 | + return &%s { |
| 38 | + Filename: filename, |
| 39 | + Rownum: row, |
| 40 | + Data: data, |
| 41 | + Errs: []error{}, |
| 42 | + LoadedTimestamp: time.Now(), |
| 43 | + } |
| 44 | +} |
| 45 | +
|
| 46 | +func Load%s(filenames []string, parallelism int, dl, nl string) (chan *%s, chan error){ |
| 47 | + recordChan, errChan := make(chan *%s), make(chan error) |
| 48 | + go func(){ |
| 49 | + defer close(recordChan) |
| 50 | + defer close(errChan) |
| 51 | +
|
| 52 | + concurrencyLimit := make(chan struct{}, parallelism) |
| 53 | + defer close(concurrencyLimit) |
| 54 | +
|
| 55 | + var wg sync.WaitGroup |
| 56 | + wg.Add(len(filenames)) |
| 57 | +
|
| 58 | + for _, filename := range filenames { |
| 59 | + concurrencyLimit <- struct{}{} |
| 60 | + go func(wg *sync.WaitGroup, filename string) { |
| 61 | +
|
| 62 | + defer func(){ |
| 63 | + <-concurrencyLimit |
| 64 | + }() |
| 65 | +
|
| 66 | + defer wg.Done() |
| 67 | +
|
| 68 | + f, err := os.Open(filename) |
| 69 | + if err != nil { |
| 70 | + errChan <- err |
| 71 | + return |
| 72 | + } |
| 73 | +
|
| 74 | + defer func(){ |
| 75 | + if err := f.Close(); err != nil{ |
| 76 | + errChan <- err |
| 77 | + } |
| 78 | + }() |
| 79 | +
|
| 80 | + rowNum := 0 |
| 81 | + decoder := csv.NewDecoder(dl, nl, f) |
| 82 | + for decoder.Scan() { |
| 83 | + rowNum++ |
| 84 | + var data %s |
| 85 | + if err := decoder.Decode(&data); err != nil { |
| 86 | + errChan <- err |
| 87 | + return // Parser is probably confused so we should exit |
| 88 | + } |
| 89 | + recordChan <- New%s(filename, rowNum, data) |
| 90 | + } |
| 91 | +
|
| 92 | + }(&wg, filename) |
| 93 | + } |
| 94 | +
|
| 95 | + wg.Wait() |
| 96 | + }() |
| 97 | + return recordChan, errChan |
| 98 | +} |
| 99 | +` |
| 100 | + |
| 101 | +func generateCsvRecordName(dataType string) string { |
| 102 | + normalizedDatatypeName := dataType |
| 103 | + if idx := strings.LastIndexByte(dataType,'.'); idx >= 0 { |
| 104 | + normalizedDatatypeName = dataType[idx+1:] |
| 105 | + } |
| 106 | + return fmt.Sprintf("CsvRecord%s", normalizedDatatypeName) |
| 107 | +} |
| 108 | + |
| 109 | +func generateImports(imports []string) string { |
| 110 | + quoted := make([]string, len(imports)) |
| 111 | + for i, v := range imports{ |
| 112 | + quoted[i] = "\""+v+"\"" |
| 113 | + } |
| 114 | + return strings.Join(quoted,"\n\t") |
| 115 | +} |
| 116 | + |
| 117 | +func generateTemplate(out io.Writer, genCommand, pkg, dataType string, imports []string) { |
| 118 | + csvRecordName := generateCsvRecordName(dataType) |
| 119 | + importStr := generateImports(imports) |
| 120 | + fmt.Fprintf(out, template, |
| 121 | + pkg, |
| 122 | + genCommand, |
| 123 | + time.Now().String(), |
| 124 | + importStr, // 1 |
| 125 | + csvRecordName, // 2 |
| 126 | + dataType, // 3 |
| 127 | + csvRecordName, // 4 |
| 128 | + dataType, // 5 |
| 129 | + csvRecordName, // 6 |
| 130 | + csvRecordName, // 6 |
| 131 | + csvRecordName, // 6 |
| 132 | + csvRecordName, // 7 |
| 133 | + csvRecordName, |
| 134 | + dataType, |
| 135 | + csvRecordName) // 8 |
| 136 | +} |
| 137 | + |
| 138 | +var ( |
| 139 | + outfileName string |
| 140 | + pkg string |
| 141 | + dataType string |
| 142 | + importsRaw string |
| 143 | +) |
| 144 | + |
| 145 | +func main() { |
| 146 | + flag.StringVar(&outfileName, "out", "", "-out csvloader.go") |
| 147 | + flag.StringVar(&pkg, "pkg", "", "-pkg main") |
| 148 | + flag.StringVar(&dataType, "data", "", "-data schema.CareCloudLabExtractRecord") |
| 149 | + flag.StringVar(&importsRaw, "imports", "", "-imports \"github.com/tbshillcdr/gocdrm/schema,secondImport.com/...\"") |
| 150 | + flag.Parse() |
| 151 | + |
| 152 | + var out io.Writer |
| 153 | + if outfileName == "" { |
| 154 | + out = os.Stdout |
| 155 | + } else { |
| 156 | + var err error |
| 157 | + out, err = os.OpenFile(outfileName,os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0655) |
| 158 | + if err != nil { |
| 159 | + fmt.Fprintf(os.Stderr, "Failed to create outfile with reason:%v", err) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + if pkg == "" { |
| 164 | + fmt.Fprintf(os.Stderr, "-pkg is requried\n") |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + if dataType == "" { |
| 169 | + fmt.Fprintf(os.Stderr, "-data is requried\n") |
| 170 | + return |
| 171 | + } |
| 172 | + |
| 173 | + generateTemplate(out, strings.Join(os.Args, " "), pkg, dataType, strings.Split(importsRaw, ",")) |
| 174 | +} |
0 commit comments