Skip to content

Commit

Permalink
feat: add iterator function to template execution
Browse files Browse the repository at this point in the history
Co-authored-by: Eko Simanjuntak <[email protected]>
Co-authored-by: Rizky Putra <[email protected]>
  • Loading branch information
3 people committed Jan 12, 2022
1 parent 824ffc8 commit 7e87d09
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
23 changes: 23 additions & 0 deletions pkg/tmpl/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,26 @@ func (result *scanResult) Execute(data interface{}) (mapResult map[string]string

return
}
func (result *scanResult) addInternalFunction() {
result.template.Funcs(template.FuncMap{
"Iterate": func(count *uint) []uint {
var counter uint
var items []uint
for counter = 0; counter < (*count); counter++ {
items = append(items, counter)
}
return items
},
})
}

func NewScanResult(rootPath string, template *template.Template, templateMap FileMap, option *ScanOption) ScanResult {
result := &scanResult{
rootPath: rootPath,
template: template,
templateMap: templateMap,
option: option,
}
result.addInternalFunction()
return result
}
12 changes: 2 additions & 10 deletions pkg/tmpl/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ func TemplateScanOption(scanPath string, option *ScanOption) (ScanResult, error)
rootTemplate = rootTemplate.Delims(option.StartDelim, option.EndDelim)
}

result := &scanResult{
rootPath: scanPath,
template: rootTemplate,
option: option,
}

// start directory walk
templateMap := FileMap{}
err := filepath.Walk(scanPath, func(path string, info os.FileInfo, err error) error {
Expand All @@ -124,7 +118,7 @@ func TemplateScanOption(scanPath string, option *ScanOption) (ScanResult, error)
}

// initiate new template with path as name
innerTemplate := result.template.New(relativePath).Funcs(sprig.TxtFuncMap())
innerTemplate := rootTemplate.New(relativePath).Funcs(sprig.TxtFuncMap())

// parse template
_, err = innerTemplate.Parse(string(byteArray))
Expand All @@ -138,9 +132,7 @@ func TemplateScanOption(scanPath string, option *ScanOption) (ScanResult, error)

return nil
})

result.templateMap = templateMap

result := NewScanResult(scanPath, rootTemplate, templateMap, option)
return result, err
}

Expand Down

0 comments on commit 7e87d09

Please sign in to comment.