gas/template
template/composite
import "github.com/gasmod/gas/template/composite"Package composite chains template providers: a writable provider that receives every write and is read first, and read-only fallbacks read after it in order.
Store implements gas.TemplateProvider. A common chain is a database store over the templates embedded in the app.
embedded, err := templatefs.NewStore(sub)()if err != nil { return err}store, err := composite.NewStore(overrides, embedded)()overrides is a writable gas.TemplateProvider, such as a db store. After this Get returns a template from overrides when it has one, and from embedded otherwise.
In an app the providers are services of their own, so build the chain in a constructor that takes them:
gas.WithSingletonService[gas.TemplateProvider]( func(d *dir.Store) (*composite.Store, error) { return composite.NewStore(d, embedded)() },)with the dir store registered as *dir.Store, so that the container
initializes and closes it.
Reading
Section titled “Reading”Get asks the writable provider, then each reader in order, and returns the first template found.
It goes on to the next provider when one returns an error wrapping template.ErrTemplateNotFound or template.ErrInvalidName. So providers of different extensions or name limits mix:
- With a memory store of
.htmlover an fs store of.htmland.txt,mail.txtis read from the fs store. - A name too long for a db store is read from the store under it.
Any other error, such as a closed store or a database that is down, is returned at once and unchanged, so a failure is never hidden by a fallback.
When no provider has the name, Get returns the last provider’s not-found error. When every provider refused the name, it returns the first provider’s invalid-name error. So List and Get agree: Get of a name List returns is never a not-found or an invalid-name error.
Listing
Section titled “Listing”List returns the names of every provider, once each, sorted in byte order. A provider that fails stops it, and its error is returned unchanged; there is no partial list.
Writing
Section titled “Writing”Register and RegisterFS go to the writable provider, and its error is returned unchanged: template.ErrReadOnly when it is read-only.
Lifecycle
Section titled “Lifecycle”A Store has none: it neither initializes nor closes its providers, which belong to whoever built them.
Concurrency
Section titled “Concurrency”A Store holds only its providers, fixed by the constructor, and is safe for concurrent use when they are.
Errors
Section titled “Errors”The sentinel this package adds, to be tested with errors.Is:
- ErrNilProvider: the constructor was given a nil provider, a nil interface or a typed nil pointer.
The methods return their providers’ errors unchanged.
The errors of this package, as returned:
template: provider is nil: the writable providertemplate: provider is nil: reader 2API reference
Section titled “API reference”Variables
Section titled “Variables”var ErrNilProvider = errors.New(template.ErrorPrefix + "provider is nil")ErrNilProvider indicates that the constructor was given a nil provider.
func NewStore
Section titled “func NewStore”func NewStore( writable gas.TemplateProvider, readers ...gas.TemplateProvider,) func() (*Store, error)NewStore returns the constructor of a Store over writable and readers, in the shape the gas container expects. The constructor returns an error wrapping ErrNilProvider that names the position of a nil provider.
type Store
Section titled “type Store”type Store struct { // contains filtered or unexported fields}Store chains a writable provider and read-only fallbacks, as described in the package documentation. It is safe for concurrent use when its providers are.
func (*Store) Get
Section titled “func (*Store) Get”func (s *Store) Get(ctx context.Context, name string) ([]byte, error)Get returns the template name from the first provider that has it: the writable provider, then each reader in order. It goes on to the next provider on an error wrapping template.ErrTemplateNotFound or template.ErrInvalidName, and returns any other error unchanged. When no provider has the name it returns the last not-found error, or, when every provider refused the name, the first provider’s error.
func (*Store) List
Section titled “func (*Store) List”func (s *Store) List(ctx context.Context) ([]string, error)List returns the names of every provider’s templates, once each, in byte order. The first provider that fails stops it, and its error is returned unchanged.
func (*Store) Register
Section titled “func (*Store) Register”func (s *Store) Register(ctx context.Context, name string, content []byte) errorRegister registers the template with the writable provider and returns its error unchanged.
func (*Store) RegisterFS
Section titled “func (*Store) RegisterFS”func (s *Store) RegisterFS(ctx context.Context, fsys fs.FS) errorRegisterFS registers the files of fsys with the writable provider and returns its error unchanged.