Skip to content

Commit e5544d3

Browse files
committed
pkg/asset: introduce AssetGenerator
Introduces the asset generator interface, which allows generating assets with a passed in context. Provides an adapter for assets that do not implement GenerateWithContext. The adapter simply wraps the asset and calls the original Generate (without context) function.
1 parent 66c8ea5 commit e5544d3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

pkg/asset/generator.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package asset
2+
3+
import (
4+
"context"
5+
)
6+
7+
// Generator is used to generate assets.
8+
type Generator interface {
9+
// Generate generates this asset given
10+
// the states of its parent assets.
11+
GenerateWithContext(context.Context, Parents) error
12+
}
13+
14+
// generatorAdapter wraps an asset to provide the
15+
// Generate with context function.
16+
type generatorAdapter struct {
17+
a Asset
18+
}
19+
20+
// NewDefaultGenerator creates a new adapter to generate
21+
// an asset with a context.
22+
func NewDefaultGenerator(a Asset) Generator {
23+
return &generatorAdapter{a: a}
24+
}
25+
26+
// Generate calls Generate on an asset, dropping the context
27+
// to maintain compatibility with assets that do not implement
28+
// generate with context.
29+
func (a *generatorAdapter) GenerateWithContext(_ context.Context, p Parents) error {
30+
return a.a.Generate(p)
31+
}

0 commit comments

Comments
 (0)