feat: url based data loading system

This commit is contained in:
2022-05-05 16:22:52 +02:00
parent 2a7dc481b1
commit e05722cc2f
25 changed files with 774 additions and 33 deletions

View 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{}
}

View 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)
}
}

View File

@ -0,0 +1,3 @@
{
"foo": "bar"
}

View File

@ -0,0 +1 @@
{}