mirror of
https://github.com/Bornholm/formidable.git
synced 2024-12-23 23:39:33 +01:00
43 lines
779 B
Go
43 lines
779 B
Go
|
package json
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"io"
|
||
|
"net/url"
|
||
|
"path"
|
||
|
"path/filepath"
|
||
|
|
||
|
"forge.cadoles.com/wpetit/formidable/internal/data/format"
|
||
|
"github.com/pkg/errors"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
ExtensionJSON = ".json"
|
||
|
FormatJSON = "json"
|
||
|
)
|
||
|
|
||
|
type DecoderHandler struct{}
|
||
|
|
||
|
func (d *DecoderHandler) Match(url *url.URL) bool {
|
||
|
ext := filepath.Ext(path.Join(url.Host, url.Path))
|
||
|
|
||
|
return ext == ExtensionJSON ||
|
||
|
format.MatchURLQueryFormat(url, FormatJSON)
|
||
|
}
|
||
|
|
||
|
func (d *DecoderHandler) Decode(url *url.URL, reader io.Reader) (interface{}, error) {
|
||
|
decoder := json.NewDecoder(reader)
|
||
|
|
||
|
var values interface{}
|
||
|
|
||
|
if err := decoder.Decode(&values); err != nil {
|
||
|
return nil, errors.WithStack(err)
|
||
|
}
|
||
|
|
||
|
return values, nil
|
||
|
}
|
||
|
|
||
|
func NewDecoderHandler() *DecoderHandler {
|
||
|
return &DecoderHandler{}
|
||
|
}
|