Skip to content

Commit

Permalink
Merge pull request #67 from miracl/embed-schema
Browse files Browse the repository at this point in the history
fixes #47 Conflate should define its own schema for the includes section
  • Loading branch information
andy-miracl authored Apr 18, 2018
2 parents c54d702 + 81c29fb commit 6fea23c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
29 changes: 21 additions & 8 deletions filedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (

type filedata struct {
url pkgurl.URL
bytes []byte
data []byte
obj map[string]interface{}
includes []string
}

var emptyFiledata = filedata{}

type filedatas []filedata

// UnmarshallerFunc defines the type of function used for unmarshalling data
Expand All @@ -36,18 +38,25 @@ var Unmarshallers = UnmarshallerMap{
"": {JSONUnmarshal, YAMLUnmarshal, TOMLUnmarshal},
}

func newFiledata(bytes []byte, url pkgurl.URL) (filedata, error) {
fd := filedata{bytes: bytes, url: url}
func newFiledata(data []byte, url pkgurl.URL) (filedata, error) {
fd := filedata{data: data, url: url}
err := fd.unmarshal()
if err != nil {
return fd, fd.wrapError(err)
return emptyFiledata, err
}
err = fd.validate()
if err != nil {
return emptyFiledata, err
}
err = fd.extractIncludes()
return fd, fd.wrapError(err)
if err != nil {
return emptyFiledata, err
}
return fd, nil
}

func newExpandedFiledata(bytes []byte, url pkgurl.URL) (filedata, error) {
return newFiledata(recursiveExpand(bytes), url)
func newExpandedFiledata(data []byte, url pkgurl.URL) (filedata, error) {
return newFiledata(recursiveExpand(data), url)
}

func (fd *filedata) wrapError(err error) error {
Expand All @@ -57,6 +66,10 @@ func (fd *filedata) wrapError(err error) error {
return wrapError(err, "Error processing %v", fd.url.String())
}

func (fd *filedata) validate() error {
return fd.wrapError(validate(fd.obj, getSchema()))
}

func (fd *filedata) unmarshal() error {
ext := strings.ToLower(filepath.Ext(fd.url.Path))
unmarshallers, ok := Unmarshallers[ext]
Expand All @@ -65,7 +78,7 @@ func (fd *filedata) unmarshal() error {
}
err := makeError("Could not unmarshal data")
for _, unmarshal := range unmarshallers {
uerr := unmarshal(fd.bytes, &fd.obj)
uerr := unmarshal(fd.data, &fd.obj)
if uerr == nil {
return nil
}
Expand Down
18 changes: 13 additions & 5 deletions filedata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,9 @@ func TestFiledata_BlankIncludes(t *testing.T) {
}

func TestFiledata_NullIncludes(t *testing.T) {
fd, err := testLoader.wrapFiledata([]byte(`{"includes":null, "x": 1}`))
assert.Nil(t, err)
assert.Nil(t, fd.obj[Includes])
assert.Equal(t, fd.obj, map[string]interface{}{"x": 1.0})
_, err := testLoader.wrapFiledata([]byte(`{"includes":null}`))
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "not valid against the schema")
}

func TestFiledata_Includes(t *testing.T) {
Expand All @@ -165,12 +164,21 @@ func TestFiledata_Includes(t *testing.T) {
assert.Equal(t, fd.obj, map[string]interface{}{"x": 1.0})
}

func TestFiledata_IncludesError(t *testing.T) {
func TestFiledata_ExtractError(t *testing.T) {
old := getSchema
getSchema = func() map[string]interface{} { return map[string]interface{}{} }
defer func() { getSchema = old }()
_, err := testLoader.wrapFiledata([]byte(`{"includes": "not array"}`))
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Could not extract includes")
}

func TestFiledata_IncludesError(t *testing.T) {
_, err := testLoader.wrapFiledata([]byte(`{"includes": "not array"}`))
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "not valid against the schema")
}

func TestFiledata_Expand(t *testing.T) {
w := os.Getenv("W")
x := os.Getenv("X")
Expand Down
23 changes: 23 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ import (

var metaSchema interface{}

var getSchema = getDefaultSchema

func getDefaultSchema() map[string]interface{} {
return map[string]interface{}{
"anyOf": []interface{}{
map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
Includes: map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
"type": "string",
},
},
},
},
map[string]interface{}{
"type": "null",
},
},
}
}

func validateSchema(schema interface{}) error {
if metaSchema == nil {
err := JSONUnmarshal(metaSchemaData, &metaSchema)
Expand Down

0 comments on commit 6fea23c

Please sign in to comment.