Skip to content

Commit

Permalink
generalize template helper
Browse files Browse the repository at this point in the history
  • Loading branch information
uberesch committed Jul 31, 2017
1 parent 370d8dd commit fbac992
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions codegen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ import (
"github.com/uber/zanzibar/codegen/template_bundle"
)

// AssetProvider provides access to template assets
type AssetProvider interface {
// AssetNames returns a list of named assets available
AssetNames() []string
// Asset returns the bytes for a provided asset name
Asset(string) ([]byte, error)
}

type defaultAssetCollection struct{}

func (*defaultAssetCollection) AssetNames() []string {
return templates.AssetNames()
}

func (*defaultAssetCollection) Asset(assetName string) ([]byte, error) {
return templates.Asset(assetName)
}

var funcMap = tmpl.FuncMap{
"lower": strings.ToLower,
"title": strings.Title,
Expand Down Expand Up @@ -81,9 +99,17 @@ type Template struct {

// NewTemplate creates a bundle of templates.
func NewTemplate() (*Template, error) {
return NewTemplateFromAssetProvider(&defaultAssetCollection{})
}

// NewTemplateFromAssetProvider returns a template helper for the
// provided asset collection
func NewTemplateFromAssetProvider(
assetProvider AssetProvider,
) (*Template, error) {
t := tmpl.New("main").Funcs(funcMap)
for _, file := range templates.AssetNames() {
fileContent, err := templates.Asset(file)
for _, file := range assetProvider.AssetNames() {
fileContent, err := assetProvider.Asset(file)
if err != nil {
return nil, errors.Wrapf(
err,
Expand Down

0 comments on commit fbac992

Please sign in to comment.