Skip to content

Commit

Permalink
fix(fsi): Can get body even if no structure exists. Infer it.
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Oct 18, 2019
1 parent 1218e89 commit 4cf8132
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
2 changes: 1 addition & 1 deletion api/fsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func TestNoHistory(t *testing.T) {
}
// Handle temporary directory by replacing the temp part with a shorter string.
resultBody := strings.Replace(gotBodyString, 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","formatConfig":{"lazyQuotes":true},"qri":"st:0","schema":{"items":{"items":[{"title":"field_1","type":"boolean"},{"title":"field_2","type":"boolean"},{"title":"field_3","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","formatConfig":{"lazyQuotes":true},"qri":"st:0","schema":{"items":{"items":[{"title":"field_1","type":"string"},{"title":"field_2","type":"string"},{"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
36 changes: 36 additions & 0 deletions cmd/fsi_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,42 @@ run ` + "`qri save`" + ` to commit this dataset
}
}

// Test that we can get the body even if structure has been deleted.
func TestGetBodyWithoutStructure(t *testing.T) {
fr := NewFSITestRunner(t, "qri_test_get_body_without_structure")
defer fr.Delete()

workDir := fr.CreateAndChdirToWorkDir("body_only")

// Init as a linked directory.
if err := fr.ExecCommand("qri init --name body_only --format csv"); err != nil {
t.Fatal(err)
}

// Verify the directory contains the files that we expect.
dirContents := listDirectory(workDir)
expectContents := []string{".qri-ref", "body.csv", "meta.json", "structure.json"}
if diff := cmp.Diff(expectContents, dirContents); diff != "" {
t.Errorf("directory contents (-want +got):\n%s", diff)
}

// Remove the structure.
if err := os.Remove(filepath.Join(workDir, "structure.json")); err != nil {
t.Fatal(err)
}

// Get the body, even though there's no structure. One will be inferred.
if err := fr.ExecCommand("qri get body"); err != nil {
t.Fatalf(err.Error())
}

output := fr.GetCommandOutput()
expectBody := "for linked dataset [test_peer/body_only]\n\none,two,3\nfour,five,6\n\n"
if diff := cmp.Diff(expectBody, output); diff != "" {
t.Errorf("directory contents (-want +got):\n%s", diff)
}
}

// Test that checkout, used on a simple dataset with a body.json and no meta, creates a
// working directory with a clean status.
func TestCheckoutSimpleStatus(t *testing.T) {
Expand Down
26 changes: 18 additions & 8 deletions fsi/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,27 @@ func GetBody(dirPath string, format dataset.DataFormat, fcfg dataset.FormatConfi
}
defer f.Close()

var structure *dataset.Structure

var schema map[string]interface{}
stComponent := components.Base().GetSubcomponent("structure")
stComponent.LoadAndFill(nil)
structure, ok := stComponent.(*component.StructureComponent)
if !ok {
return nil, fmt.Errorf("could not get structure")
if stComponent != nil {
stComponent.LoadAndFill(nil)
comp, ok := stComponent.(*component.StructureComponent)
if !ok {
return nil, fmt.Errorf("could not get structure")
}
structure = comp.Value
schema = structure.Schema
}
stValue := structure.Value
schema := stValue.Schema

if schema == nil {
bodyFormat := bodyComponent.Base().Format
// If there was no structure, define one using the body's file extension.
if structure == nil {
structure = &dataset.Structure{}
structure.Format = bodyFormat
}
// Create schema by detecting it from the body.
// TODO(dlong): This should move into `dsio` package.
entries, err := component.OpenEntryReader(f, bodyFormat)
Expand All @@ -63,7 +73,7 @@ func GetBody(dirPath string, format dataset.DataFormat, fcfg dataset.FormatConfi
if fcfg != nil {
assign.FormatConfig = fcfg.Map()
}
st.Assign(stValue, assign)
st.Assign(structure, assign)

return base.ConvertBodyFile(file, stValue, st, limit, offset, all)
return base.ConvertBodyFile(file, structure, st, limit, offset, all)
}
3 changes: 2 additions & 1 deletion fsi/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func (fsi *FSI) InitDataset(p InitParams) (name string, err error) {
return "", fmt.Errorf("invalid path to initialize. '%s' is not a directory", p.Dir)
}

// TODO(dlong): This function should more closely resemble Checkout in lib/fsi.go
// TODO(dlong): This function should more closely resemble Checkout in lib/fsi.go. That is,
// do some stuff to initialize, create components, then call WriteComponents.

// Either use an existing directory, or create one at the given directory.
var targetPath string
Expand Down

0 comments on commit 4cf8132

Please sign in to comment.