Skip to content

Commit

Permalink
fix(fsi): Init does not output a schema.json
Browse files Browse the repository at this point in the history
Merge pull request #917 from qri-io/no-init-schema
  • Loading branch information
b5 authored Sep 10, 2019
2 parents f1f14d4 + 3ecdbbe commit b018fd4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 55 deletions.
8 changes: 3 additions & 5 deletions api/fsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ func TestNoHistory(t *testing.T) {
// Get mtimes for the component files
st, _ := os.Stat(filepath.Join(initDir, "meta.json"))
metaMtime := st.ModTime().Format(time.RFC3339)
st, _ = os.Stat(filepath.Join(initDir, "schema.json"))
schemaMtime := st.ModTime().Format(time.RFC3339)
st, _ = os.Stat(filepath.Join(initDir, "body.csv"))
bodyMtime := st.ModTime().Format(time.RFC3339)

Expand All @@ -123,7 +121,7 @@ func TestNoHistory(t *testing.T) {
}
// Handle temporary directory by replacing the temp part with a shorter string.
resultBody := strings.Replace(actualBody, initDir, initSubdir, -1)
expectBody = `{"data":{"peername":"peer","name":"test_ds","fsiPath":"fsi_init_dir","dataset":{"bodyPath":"fsi_init_dir/body.csv","meta":{"keywords":[],"qri":"md:0"},"name":"test_ds","peername":"peer","qri":"ds:0","structure":{"format":"csv","qri":"st:0","schema":{"items":{"items":[{"title":"name","type":"string"},{"title":"describe","type":"string"},{"title":"quantity","type":"integer"}],"type":"array"},"type":"array"}}},"published":false},"meta":{"code":200}}`
expectBody = `{"data":{"peername":"peer","name":"test_ds","fsiPath":"fsi_init_dir","dataset":{"bodyPath":"fsi_init_dir/body.csv","meta":{"keywords":[],"qri":"md:0"},"name":"test_ds","peername":"peer","qri":"ds:0","structure":{"format":"csv","qri":"st:0","schema":{"items":{"items":[{"title":"field_1","type":"string"},{"title":"field_2","type":"boolean"},{"title":"field_3","type":"integer"}],"type":"array"},"type":"array"}}},"published":false},"meta":{"code":200}}`
if diff := cmp.Diff(expectBody, resultBody); diff != "" {
t.Errorf("api response (-want +got):\n%s", diff)
}
Expand Down Expand Up @@ -167,8 +165,8 @@ func TestNoHistory(t *testing.T) {
}
// Handle temporary directory by replacing the temp part with a shorter string.
resultBody = strings.Replace(actualBody, initDir, initSubdir, -1)
templateBody := `{"data":[{"sourceFile":"fsi_init_dir/meta.json","component":"meta","type":"add","message":"","mtime":"%s"},{"sourceFile":"fsi_init_dir/schema.json","component":"schema","type":"add","message":"","mtime":"%s"},{"sourceFile":"fsi_init_dir/body.csv","component":"body","type":"add","message":"","mtime":"%s"}],"meta":{"code":200}}`
expectBody = fmt.Sprintf(templateBody, metaMtime, schemaMtime, bodyMtime)
templateBody := `{"data":[{"sourceFile":"fsi_init_dir/meta.json","component":"meta","type":"add","message":"","mtime":"%s"},{"sourceFile":"fsi_init_dir/body.csv","component":"body","type":"add","message":"","mtime":"%s"}],"meta":{"code":200}}`
expectBody = fmt.Sprintf(templateBody, metaMtime, bodyMtime)
if diff := cmp.Diff(expectBody, resultBody); diff != "" {
t.Errorf("api response (-want +got):\n%s", diff)
}
Expand Down
12 changes: 4 additions & 8 deletions cmd/fsi_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestInitStatusSave(t *testing.T) {

// Verify the directory contains the files that we expect.
dirContents := listDirectory(workDir)
expectContents := []string{".qri-ref", "body.csv", "meta.json", "schema.json"}
expectContents := []string{".qri-ref", "body.csv", "meta.json"}
if diff := cmp.Diff(dirContents, expectContents); diff != "" {
t.Errorf("directory contents (-want +got):\n%s", diff)
}
Expand All @@ -134,7 +134,6 @@ func TestInitStatusSave(t *testing.T) {
expect := `for linked dataset [test_peer/brand_new]
add: meta (source: meta.json)
add: schema (source: schema.json)
add: body (source: body.csv)
run ` + "`qri save`" + ` to commit this dataset
Expand Down Expand Up @@ -868,7 +867,7 @@ func TestRestoreWithNoHistory(t *testing.T) {

// Verify the directory contains the files that we expect.
dirContents := listDirectory(workDir)
expectContents := []string{".qri-ref", "body.csv", "schema.json"}
expectContents := []string{".qri-ref", "body.csv"}
if diff := cmp.Diff(dirContents, expectContents); diff != "" {
t.Errorf("directory contents (-want +got):\n%s", diff)
}
Expand All @@ -881,7 +880,6 @@ func TestRestoreWithNoHistory(t *testing.T) {
output := fr.GetCommandOutput()
expect := `for linked dataset [test_peer/new_folder]
add: schema (source: schema.json)
add: body (source: body.csv)
run ` + "`qri save`" + ` to commit this dataset
Expand Down Expand Up @@ -910,7 +908,7 @@ func TestInitWithSourceBodyPath(t *testing.T) {

// Verify the directory contains the files that we expect.
dirContents := listDirectory(workDir)
expectContents := []string{".qri-ref", "body.csv", "meta.json", "schema.json"}
expectContents := []string{".qri-ref", "body.csv", "meta.json"}
if diff := cmp.Diff(dirContents, expectContents); diff != "" {
t.Errorf("directory contents (-want +got):\n%s", diff)
}
Expand All @@ -924,7 +922,6 @@ func TestInitWithSourceBodyPath(t *testing.T) {
expect := `for linked dataset [test_peer/init_source]
add: meta (source: meta.json)
add: schema (source: schema.json)
add: body (source: body.csv)
run ` + "`qri save`" + ` to commit this dataset
Expand Down Expand Up @@ -964,7 +961,7 @@ func TestInitWithDirectory(t *testing.T) {

// Verify the directory contains the files that we expect.
dirContents := listDirectory(workDir)
expectContents := []string{".qri-ref", "body.csv", "meta.json", "schema.json"}
expectContents := []string{".qri-ref", "body.csv", "meta.json"}
if diff := cmp.Diff(dirContents, expectContents); diff != "" {
t.Errorf("directory contents (-want +got):\n%s", diff)
}
Expand All @@ -978,7 +975,6 @@ func TestInitWithDirectory(t *testing.T) {
expect := `for linked dataset [test_peer/init_dir]
add: meta (source: meta.json)
add: schema (source: schema.json)
add: body (source: body.csv)
run ` + "`qri save`" + ` to commit this dataset
Expand Down
39 changes: 0 additions & 39 deletions fsi/init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fsi

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -89,44 +88,6 @@ func (fsi *FSI) InitDataset(p InitParams) (name string, err error) {
return name, err
}

var (
schema map[string]interface{}
data []byte
)
if p.Format == "csv" {
schema = map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
"type": "array",
"items": []interface{}{
// First column
map[string]interface{}{
"type": "string",
"title": "name",
},
// Second column
map[string]interface{}{
"type": "string",
"title": "describe",
},
// Third column
map[string]interface{}{
"type": "integer",
"title": "quantity",
},
},
},
}
} else {
schema = map[string]interface{}{
"type": "object",
}
}
data, err = json.MarshalIndent(schema, "", " ")
if err := ioutil.WriteFile(filepath.Join(targetPath, "schema.json"), data, os.ModePerm); err != nil {
return name, err
}

var bodyBytes []byte
if p.SourceBodyPath == "" {
// Create a skeleton body file.
Expand Down
19 changes: 16 additions & 3 deletions fsi/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func ReadDir(dir string) (ds *dataset.Dataset, fileMap, problems map[string]File
}

bodyFilepath := ""
var bodyBaseSchema map[string]interface{}
if bodyFormat != "" {
bodyFilepath = filepath.Join(dir, fmt.Sprintf("body.%s", bodyFormat))
if err != nil {
Expand All @@ -107,6 +108,7 @@ func ReadDir(dir string) (ds *dataset.Dataset, fileMap, problems map[string]File
err = dsio.EachEntry(entries, func(int, dsio.Entry, error) error { return nil })
if err == nil {
bodyOkay = true
bodyBaseSchema = entries.Structure().Schema
}
}
}
Expand Down Expand Up @@ -206,9 +208,6 @@ func ReadDir(dir string) (ds *dataset.Dataset, fileMap, problems map[string]File
ds.Structure = &dataset.Structure{}
}
ds.Structure.Schema = *cmp.(*map[string]interface{})
if ds.Structure.Format == "" {
ds.Structure.Format = bodyFormat
}
case componentNameViz:
ds.Viz = cmp.(*dataset.Viz)
case componentNameTransform:
Expand All @@ -222,6 +221,20 @@ func ReadDir(dir string) (ds *dataset.Dataset, fileMap, problems map[string]File
}
}

// A very special hack for when the body implies a format, but there's no schema, and
// therefore no structure.
if bodyFormat != "" {
if ds.Structure == nil {
ds.Structure = &dataset.Structure{}
}
if ds.Structure.Format == "" {
ds.Structure.Format = bodyFormat
}
if len(ds.Structure.Schema) == 0 {
ds.Structure.Schema = bodyBaseSchema
}
}

if len(fileMap) == 0 {
err = ErrNoDatasetFiles
}
Expand Down

0 comments on commit b018fd4

Please sign in to comment.