Skip to content

Commit da515c6

Browse files
committed
Add support for both static template vars and csv mapped vars
1 parent f153de5 commit da515c6

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ input: test_data/products.csv
2020
templates: "test_data/templates/"
2121
output: output/
2222
variables:
23+
csvMapping: # CSV to Template variable mapping
2324
ProductName: 6 # CSV column no for product name
2425
ProductPrice: 14 # CSV column no for product price
2526
ProductImage: 21 # CSV column no for product image
26-
VideoFile: "video.avi" # Hard coded string value
27+
staticVariables: # Hard coded template variables
28+
VideoFile: "video.avi"
2729
```

nerc.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"os"
1414
"path"
1515
"path/filepath"
16-
"reflect"
1716
"strconv"
1817
"text/template"
1918
)
@@ -29,10 +28,11 @@ type TemplateVariable struct {
2928
}
3029

3130
type NercConf struct {
32-
Input string `yaml:"input"`
33-
Templates string `yaml:"templates"`
34-
Output string `yaml:"output"`
35-
Variables map[string]interface{} `yaml:"variables"`
31+
Input string `yaml:"input"`
32+
Templates string `yaml:"templates"`
33+
Output string `yaml:"output"`
34+
StaticVariables map[string]interface{} `yaml:"staticVariables"`
35+
CSVMapping map[string]int `yaml:"csvMapping"`
3636
}
3737

3838
func main() {
@@ -144,12 +144,11 @@ func csvToConfigs(r *csv.Reader, nercConf NercConf) {
144144

145145
func writeConf(row []string, template string, i int, nercConf NercConf) {
146146
templateVars := make(map[string]interface{})
147-
for k, v := range nercConf.Variables {
148-
if reflect.TypeOf(v).Kind() == reflect.Int {
149-
templateVars[k] = row[v.(int)]
150-
} else {
151-
templateVars[k] = v
152-
}
147+
for k, v := range nercConf.StaticVariables {
148+
templateVars[k] = v
149+
}
150+
for k, v := range nercConf.CSVMapping {
151+
templateVars[k] = row[v]
153152
}
154153
conf := ProcessFile(template, templateVars)
155154
outputFile := "sku_" + row[0] + "_version_" + strconv.Itoa(i) + ".json"

0 commit comments

Comments
 (0)