gas/template
template/memory
import "github.com/gasmod/gas/template/memory"Package memory keeps templates in a map in memory, for development, tests and content an app builds at run time. Store implements gas.TemplateProvider with the contract of the template package.
store, err := memory.NewStore(memory.WithExtensions(".html", ".txt"))()if err != nil { return err}err = store.RegisterFS(ctx, emails) // an embed.FSAfter this the store holds every .html and .txt file of emails,
under its path in emails, and nothing else. In an app, register the
constructor:
gas.WithSingletonService[gas.TemplateProvider](memory.NewStore())A Store starts empty and holds what is registered until the process ends; it has no lifecycle. Names and extensions are as the template package describes, template.DefaultExtension unless WithExtensions gives others.
- Register keeps a copy of the content and Get returns a copy, so neither the caller’s slice nor the returned one shares memory with the store.
- RegisterFS reads every file first and stores them all under one hold of the lock, so a reader sees all of them or none.
- Register and RegisterFS refuse content over the store’s maximum size, template.DefaultMaxSize unless WithMaxSize gives another, with an error wrapping template.ErrTooLarge.
Concurrency
Section titled “Concurrency”A Store is safe for concurrent use. A read-write lock guards the map:
- Get and List hold it for reading.
- Register and RegisterFS hold it for writing.
- RegisterFS never holds it while it reads its files.
Errors
Section titled “Errors”The constructor returns an error wrapping template.ErrInvalidExtension or template.ErrInvalidMaxSize for a bad option.
The methods return errors wrapping template.ErrInvalidName, template.ErrTemplateNotFound and template.ErrTooLarge. RegisterFS also returns template.ErrNilFS, template.ErrRead and the context’s error. The texts are those the template package lists.
API reference
Section titled “API reference”func NewStore
Section titled “func NewStore”func NewStore(opts ...Option) func() (*Store, error)NewStore returns the constructor of an empty Store with opts applied, in the shape the gas container expects. The constructor returns an error wrapping template.ErrInvalidExtension or template.ErrInvalidMaxSize for a bad option.
type Option
Section titled “type Option”type Option func(*Store)Option configures a Store.
func WithExtensions
Section titled “func WithExtensions”func WithExtensions(exts ...string) OptionWithExtensions sets the extensions a template name must end in. The constructor checks them, as described in the template package documentation.
Default: template.DefaultExtension.
func WithMaxSize
Section titled “func WithMaxSize”func WithMaxSize(n int64) OptionWithMaxSize sets the most bytes a template may hold. The constructor checks it with template.CheckMaxSize.
Default: template.DefaultMaxSize.
type Store
Section titled “type Store”type Store struct { // contains filtered or unexported fields}Store keeps templates in memory, as described in the package documentation. It is safe for concurrent use.
func (*Store) Get
Section titled “func (*Store) Get”func (s *Store) Get(_ context.Context, name string) ([]byte, error)Get returns a copy of the content of the template name, an error wrapping template.ErrTemplateNotFound when there is none, or one wrapping template.ErrInvalidName for a name the store does not accept.
func (*Store) List
Section titled “func (*Store) List”func (s *Store) List(context.Context) ([]string, error)List returns the names of the templates in byte order.
func (*Store) Register
Section titled “func (*Store) Register”func (s *Store) Register(_ context.Context, name string, content []byte) errorRegister stores a copy of content as the template name, replacing one of that name. Nil content is stored as empty. A name the store does not accept is an error wrapping template.ErrInvalidName, and content over the store’s maximum size one wrapping template.ErrTooLarge.
func (*Store) RegisterFS
Section titled “func (*Store) RegisterFS”func (s *Store) RegisterFS(ctx context.Context, fsys fs.FS) errorRegisterFS reads the files of fsys with one of the store’s extensions and stores them all, or none when reading fails, as described in the template package documentation.