## Search

Page index: https://gasmod.dev/llms.txt. Add index.md to any page URL for its Markdown.

This site has a search endpoint: https://context7.com/api/v2/ask?siteKey=ask_b845b566cb63bd033e9c576d&query=<URL-encoded question>. It returns documentation that answers the question, with a source link for each part. No API key is needed. If nothing matches, it says so.

# 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.

The Markdown of https://gasmod.dev/reference/template/composite/ from the Gas documentation, whose index for LLMs is https://gasmod.dev/llms.txt

```go
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](#Store) implements gas.TemplateProvider. A common chain is a database
store over the templates embedded in the app.

## Usage

```go
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:

```go
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

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 `.html` over an fs store of `.html` and
`.txt`, `mail.txt` is 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

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

Register and RegisterFS go to the writable provider, and its error is
returned unchanged: template.ErrReadOnly when it is read-only.

## Lifecycle

A Store has none: it neither initializes nor closes its providers,
which belong to whoever built them.

## Concurrency

A Store holds only its providers, fixed by the constructor, and is safe
for concurrent use when they are.

## Errors

The sentinel this package adds, to be tested with [errors.Is](https://pkg.go.dev/errors#Is):

- [ErrNilProvider](#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:

```go
template: provider is nil: the writable provider
template: provider is nil: reader 2
```


## API reference

### Variables

<a id="ErrNilProvider"></a>

```go
var ErrNilProvider = errors.New(template.ErrorPrefix + "provider is nil")
```

ErrNilProvider indicates that the constructor was given a nil provider.

<a id="NewStore"></a>

#### func NewStore

```go
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.

<a id="Store"></a>

### type Store

```go
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.

<a id="Store.Get"></a>

#### func (*Store) Get

```go
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.

<a id="Store.List"></a>

#### func (*Store) List

```go
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.

<a id="Store.Register"></a>

#### func (*Store) Register

```go
func (s *Store) Register(ctx context.Context, name string, content []byte) error
```

Register registers the template with the writable provider and returns
its error unchanged.

<a id="Store.RegisterFS"></a>

#### func (*Store) RegisterFS

```go
func (s *Store) RegisterFS(ctx context.Context, fsys fs.FS) error
```

RegisterFS registers the files of fsys with the writable provider and
returns its error unchanged.
