mirror of
https://github.com/Bornholm/formidable.git
synced 2025-07-22 21:41:35 +02:00
feat: url based data loading system
This commit is contained in:
42
internal/data/format/json/decoder_handler.go
Normal file
42
internal/data/format/json/decoder_handler.go
Normal file
@ -0,0 +1,42 @@
|
||||
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{}
|
||||
}
|
78
internal/data/format/json/decoder_handler_test.go
Normal file
78
internal/data/format/json/decoder_handler_test.go
Normal file
@ -0,0 +1,78 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type parserHandlerTestCase struct {
|
||||
Path string
|
||||
ExpectMatch bool
|
||||
ExpectParseError bool
|
||||
}
|
||||
|
||||
var parserHandlerTestCases = []parserHandlerTestCase{
|
||||
{
|
||||
Path: "testdata/dummy.json",
|
||||
ExpectMatch: true,
|
||||
ExpectParseError: false,
|
||||
},
|
||||
{
|
||||
Path: "file://testdata/dummy_no_ext?format=json",
|
||||
ExpectMatch: true,
|
||||
ExpectParseError: false,
|
||||
},
|
||||
}
|
||||
|
||||
func TestDecoderHandler(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
handler := NewDecoderHandler()
|
||||
|
||||
for _, tc := range parserHandlerTestCases {
|
||||
func(tc parserHandlerTestCase) {
|
||||
t.Run(fmt.Sprintf("Parse '%s'", tc.Path), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
url, err := url.Parse(tc.Path)
|
||||
if err != nil {
|
||||
t.Fatal(errors.Wrapf(err, "could not parse url '%s'", tc.Path))
|
||||
}
|
||||
|
||||
if e, g := tc.ExpectMatch, handler.Match(url); e != g {
|
||||
t.Errorf("URL '%s': expected matching result '%v', got '%v'", url.String(), e, g)
|
||||
}
|
||||
|
||||
if !tc.ExpectMatch {
|
||||
return
|
||||
}
|
||||
|
||||
cleanedPath := filepath.Join(url.Host, url.Path)
|
||||
|
||||
file, err := os.Open(cleanedPath)
|
||||
if err != nil {
|
||||
t.Fatal(errors.Wrapf(err, "could not open file '%s'", cleanedPath))
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := file.Close(); err != nil {
|
||||
t.Error(errors.Wrapf(err, "could not close file '%s'", cleanedPath))
|
||||
}
|
||||
}()
|
||||
|
||||
if _, err := handler.Decode(url, file); err != nil && !tc.ExpectParseError {
|
||||
t.Fatal(errors.Wrapf(err, "could not parse file '%s'", tc.Path))
|
||||
}
|
||||
|
||||
if tc.ExpectParseError {
|
||||
t.Fatal(errors.Errorf("no error was returned as expected when opening url '%s'", url.String()))
|
||||
}
|
||||
})
|
||||
}(tc)
|
||||
}
|
||||
}
|
3
internal/data/format/json/testdata/dummy.json
vendored
Normal file
3
internal/data/format/json/testdata/dummy.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"foo": "bar"
|
||||
}
|
1
internal/data/format/json/testdata/dummy_no_ext
vendored
Normal file
1
internal/data/format/json/testdata/dummy_no_ext
vendored
Normal file
@ -0,0 +1 @@
|
||||
{}
|
Reference in New Issue
Block a user