You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
831 B
39 lines
831 B
2 years ago
|
package engine
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
|
||
|
"gitoa.ru/go-4devs/mime"
|
||
|
"gitoa.ru/go-4devs/templating/render"
|
||
|
)
|
||
|
|
||
|
func NewEncode(name string, encode func(w io.Writer, data interface{}) error, ext ...mime.Ext) Encode {
|
||
|
return Encode{
|
||
|
name: name,
|
||
|
encode: encode,
|
||
|
formats: ext,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type Encode struct {
|
||
|
encode func(w io.Writer, data interface{}) error
|
||
|
name string
|
||
|
formats []mime.Ext
|
||
|
}
|
||
|
|
||
|
func (e Encode) Support(_ context.Context, reference render.Reference) bool {
|
||
|
return Support(reference, e.name, e.formats...)
|
||
|
}
|
||
|
|
||
|
func (e Encode) Load(context.Context, render.Reference) (render.Execute, error) {
|
||
|
return func(_ context.Context, wr io.Writer, data interface{}, _ render.Params) error {
|
||
|
if err := e.encode(wr, data); err != nil {
|
||
|
return fmt.Errorf("%s engine:%w", e.name, err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}, nil
|
||
|
}
|