feat: url based multi-format loaders/decoders

This commit is contained in:
2022-05-09 14:23:01 +02:00
parent 1353755683
commit 5383ed7ced
17 changed files with 375 additions and 4 deletions

38
internal/data/encoder.go Normal file
View File

@ -0,0 +1,38 @@
package data
import (
"io"
"net/url"
"github.com/pkg/errors"
)
type EncoderHandler interface {
URLMatcher
Encode(url *url.URL, data interface{}) (io.Reader, error)
}
type Encoder struct {
handlers []EncoderHandler
}
func (e *Encoder) Encode(url *url.URL, data interface{}) (io.Reader, error) {
for _, h := range e.handlers {
if !h.Match(url) {
continue
}
reader, err := h.Encode(url, data)
if err != nil {
return nil, errors.WithStack(err)
}
return reader, nil
}
return nil, errors.Wrapf(ErrHandlerNotFound, "could not find matching handler for url '%s'", url.String())
}
func NewEncoder(handlers ...EncoderHandler) *Encoder {
return &Encoder{handlers}
}