diff --git a/api/fsi_test.go b/api/fsi_test.go index 65c40c12c..24b329e6b 100644 --- a/api/fsi_test.go +++ b/api/fsi_test.go @@ -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) @@ -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) } @@ -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) } diff --git a/cmd/fsi_integration_test.go b/cmd/fsi_integration_test.go index 073b5e741..c3d581cf6 100644 --- a/cmd/fsi_integration_test.go +++ b/cmd/fsi_integration_test.go @@ -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) } @@ -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 @@ -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) } @@ -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 @@ -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) } @@ -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 @@ -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) } @@ -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 diff --git a/fsi/init.go b/fsi/init.go index 86d8f2959..fa7a0c226 100644 --- a/fsi/init.go +++ b/fsi/init.go @@ -1,7 +1,6 @@ package fsi import ( - "encoding/json" "fmt" "io/ioutil" "os" @@ -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. diff --git a/fsi/mapping.go b/fsi/mapping.go index 4f76cab4c..0c7996933 100644 --- a/fsi/mapping.go +++ b/fsi/mapping.go @@ -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 { @@ -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 } } } @@ -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: @@ -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 }