in generator.go [530:569]
func writeTableGoFile(cldrVersion *int, tables []*tablesTmplDataItem) error {
data := tablesTmplData{
CLDRVersion: cldrVersion,
Tables: tables,
}
tablesTmpl := filepath.Join("templates", "tables.go.tmpl")
tmpl, err := template.New(path.Base(tablesTmpl)).
Funcs(template.FuncMap{"StringSliceValue": stringSliceValue}).
ParseFiles(tablesTmpl)
if err != nil {
return fmt.Errorf("failed to parse tables.go template: %w", err)
}
tablesOutPath := filepath.Join("tables.go")
tablesOutFile, err := os.OpenFile(tablesOutPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open file %s: %w", tablesOutPath, err)
}
defer tablesOutFile.Close()
tmplBuffer := bytes.Buffer{}
err = tmpl.Execute(&tmplBuffer, data)
if err != nil {
return fmt.Errorf("failed to execute tables.go template: %w", err)
}
formattedBuffer, err := format.Source(tmplBuffer.Bytes())
if err != nil {
return fmt.Errorf("failed to format tables.go template output: %w", err)
}
_, err = tablesOutFile.Write(formattedBuffer)
if err != nil {
return fmt.Errorf("failed to write file %s: %w", tablesOutPath, err)
}
return nil
}