mirror of
https://github.com/Bornholm/formidable.git
synced 2025-08-01 18:21:35 +02:00
feat: url based data loading system
This commit is contained in:
40
internal/data/decoder.go
Normal file
40
internal/data/decoder.go
Normal file
@ -0,0 +1,40 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/url"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const FormatQueryParam = "format"
|
||||
|
||||
type DecoderHandler interface {
|
||||
URLMatcher
|
||||
Decode(url *url.URL, reader io.Reader) (interface{}, error)
|
||||
}
|
||||
|
||||
type Decoder struct {
|
||||
handlers []DecoderHandler
|
||||
}
|
||||
|
||||
func (d *Decoder) Decode(url *url.URL, reader io.ReadCloser) (interface{}, error) {
|
||||
for _, h := range d.handlers {
|
||||
if !h.Match(url) {
|
||||
continue
|
||||
}
|
||||
|
||||
data, err := h.Decode(url, reader)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
return nil, errors.Wrapf(ErrHandlerNotFound, "could not find matching handler for url '%s'", url.String())
|
||||
}
|
||||
|
||||
func NewDecoder(handlers ...DecoderHandler) *Decoder {
|
||||
return &Decoder{handlers}
|
||||
}
|
Reference in New Issue
Block a user