-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(renderer): lazy loading of templates (#1062)
Fixes #1061 Signed-off-by: Xavier Coulon <[email protected]>
- Loading branch information
Showing
46 changed files
with
1,427 additions
and
828 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"log" | ||
"text/template" | ||
"unicode" | ||
) | ||
|
||
const ( | ||
sgmlRenderer = `package sgml | ||
import ( | ||
"sync" | ||
text "text/template" | ||
) | ||
type sgmlRenderer struct { | ||
templates Templates | ||
functions text.FuncMap | ||
{{ range $i, $tmpl := . }} | ||
{{ once $tmpl}} sync.Once | ||
{{ tmpl $tmpl}} *text.Template | ||
{{ end }} | ||
} | ||
{{ range $i, $tmpl := . }} | ||
func (r *sgmlRenderer) {{ func $tmpl }} (*text.Template, error) { | ||
var err error | ||
r.{{ once $tmpl }}.Do(func() { | ||
r.{{ tmpl $tmpl }}, err = r.newTemplate("{{ $tmpl }}", r.templates.{{ $tmpl }}, err) | ||
}) | ||
return r.{{ tmpl $tmpl }}, err | ||
} | ||
{{ end }} | ||
` | ||
) | ||
|
||
func main() { | ||
// read the content of pkg/renderer/sgml/templates.go | ||
fs := token.NewFileSet() | ||
f, err := parser.ParseFile(fs, "pkg/renderer/sgml/templates.go", nil, parser.AllErrors) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
v := &visitor{ | ||
fields: []string{}, | ||
} | ||
ast.Walk(v, f) | ||
tmpl, err := template.New("templates").Funcs(template.FuncMap{ | ||
"once": func(s string) string { | ||
return string(unicode.ToLower(rune(s[0]))) + s[1:] + "Once" | ||
}, | ||
"func": func(s string) string { | ||
return string(unicode.ToLower(rune(s[0]))) + s[1:] + "()" | ||
}, | ||
"tmpl": func(s string) string { | ||
return string(unicode.ToLower(rune(s[0]))) + s[1:] + "Tmpl" | ||
}, | ||
}).Parse(sgmlRenderer) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// fmt.Printf("templates: %v\n", v.fields) | ||
result := &bytes.Buffer{} | ||
if err := tmpl.Execute(result, v.fields); err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("%s\n", result.String()) | ||
} | ||
|
||
type visitor struct { | ||
fields []string | ||
} | ||
|
||
func (v *visitor) Visit(n ast.Node) ast.Visitor { | ||
if n == nil { | ||
return nil | ||
} | ||
if f, ok := n.(*ast.Field); ok { | ||
v.fields = append(v.fields, f.Names[0].Name) | ||
} | ||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.