init template

This commit is contained in:
andrey1s
2022-10-02 23:31:19 +03:00
parent 3d5d51b2df
commit 621afcc59c
24 changed files with 969 additions and 0 deletions

29
loader/chain/loader.go Normal file
View File

@@ -0,0 +1,29 @@
package chain
import (
"context"
"errors"
"fmt"
"gitoa.ru/go-4devs/templating/loader"
"gitoa.ru/go-4devs/templating/render"
)
var _ loader.Loader = (Loaders)(nil)
type Loaders []loader.Loader
func (l Loaders) Load(ctx context.Context, template render.Reference) (loader.Source, error) {
for idx, load := range l {
sourse, err := load.Load(ctx, template)
if err == nil {
return sourse, nil
}
if !errors.Is(err, loader.ErrNotFound) {
return loader.Source{}, fmt.Errorf("chain[%d]:%w", idx, err)
}
}
return loader.Source{}, fmt.Errorf("chains(%d):%w", len(l), loader.ErrNotFound)
}

20
loader/empty.go Normal file
View File

@@ -0,0 +1,20 @@
package loader
import (
"context"
"fmt"
"gitoa.ru/go-4devs/templating/render"
)
var _empty = empty{}
type empty struct{}
func (empty) Load(context.Context, render.Reference) (Source, error) {
return Source{}, fmt.Errorf("empty: %w", ErrNotFound)
}
func Empty() Loader {
return _empty
}

34
loader/loader.go Normal file
View File

@@ -0,0 +1,34 @@
package loader
import (
"context"
"errors"
"gitoa.ru/go-4devs/templating/render"
)
var ErrNotFound = errors.New("not found")
type Loader interface {
Load(ctx context.Context, r render.Reference) (Source, error)
}
func NewSource(name, data string) Source {
return Source{
name: name,
data: data,
}
}
type Source struct {
name string
data string
}
func (s Source) Name() string {
return s.name
}
func (s Source) Data() string {
return s.data
}