Skip to content

Commit

Permalink
fix(Save): fix saving dataset with viz stored in cafs
Browse files Browse the repository at this point in the history
I tried to update the cfl dataset, couldn't get it to work on account of the viz script trying to load from a filepath.

This now seems like a set of consistent issues we should look into when working on the "file pattern refacter":
* making sure we always load the right things from the right places
* maintaining a consistent dataset state through the save process, with readers being loaded once, and that point in time forming an assumption other function calls can rely on
  • Loading branch information
b5 committed Nov 14, 2018
1 parent 0096995 commit bc2b00a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
6 changes: 6 additions & 0 deletions base/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ func CreateDataset(r repo.Repo, streams ioes.IOStreams, name string, ds *dataset
return
}

// TODO - we should remove the need for this by having viz always be kept in the right
// state until this point
if err = prepareViz(r, ds); err != nil {
return
}

// TODO - move dsfs.prepareDataset stuff up here into a "SetComputedValues" func

if err = ValidateDataset(name, ds); err != nil {
Expand Down
4 changes: 0 additions & 4 deletions base/dataset_prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ func InferValues(pro *profile.Profile, name *string, ds *dataset.Dataset, body c
res = cafs.NewMemfileReader(body.FileName(), io.MultiReader(buf, body))
}

if err = prepareViz(ds); err != nil {
return
}

if ds.Transform != nil && ds.Transform.IsEmpty() {
ds.Transform = nil
}
Expand Down
28 changes: 20 additions & 8 deletions base/viz.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
package base

import (
"bytes"
"io/ioutil"
"os"
"strings"

datastore "github.com/ipfs/go-datastore"
"github.com/qri-io/cafs"
"github.com/qri-io/dataset"
"github.com/qri-io/qri/repo"
)

// PrepareViz loads vizualization bytes from a local filepath
func prepareViz(ds *dataset.Dataset) (err error) {
func prepareViz(r repo.Repo, ds *dataset.Dataset) (err error) {
// remove any empty vizualizations
if ds.Viz != nil && ds.Viz.IsEmpty() {
ds.Viz = nil
return nil
}

if ds.Viz != nil && ds.Viz.ScriptPath != "" {
// create a reader of script bytes
scriptdata, err := ioutil.ReadFile(ds.Viz.ScriptPath)
if err != nil {
return err
if strings.HasPrefix(ds.Viz.ScriptPath, "/ipfs") || strings.HasPrefix(ds.Viz.ScriptPath, "/map") || strings.HasPrefix(ds.Viz.ScriptPath, "/cafs") {
var f cafs.File
f, err = r.Store().Get(datastore.NewKey(ds.Viz.ScriptPath))
if err != nil {
return
}
ds.Viz.Script = f
} else {
var f *os.File
f, err = os.Open(ds.Viz.ScriptPath)
if err != nil {
return
}
ds.Viz.Script = f
}
ds.Viz.Script = bytes.NewReader(scriptdata)
}
return nil
}
24 changes: 22 additions & 2 deletions base/viz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,43 @@ import (
"io/ioutil"
"testing"

"github.com/qri-io/cafs"

"github.com/qri-io/dataset"
)

func TestPrepareViz(t *testing.T) {
r := newTestRepo(t)
tmpl := []byte(`<html><head><title>hallo</title></head></html>`)

f, err := ioutil.TempFile("", "viz")
if err != nil {
t.Fatal(err.Error())
}
f.Write([]byte(`<html><head><title>hallo</title></head></html>`))
f.Write(tmpl)

ds := &dataset.Dataset{
Viz: &dataset.Viz{
ScriptPath: f.Name(),
},
}

if err := prepareViz(ds); err != nil {
if err := prepareViz(r, ds); err != nil {
t.Error(err.Error())
}

key, err := r.Store().Put(cafs.NewMemfileBytes("tmpl.html", tmpl), true)
if err != nil {
t.Fatal(key)
}

ds = &dataset.Dataset{
Viz: &dataset.Viz{
ScriptPath: key.String(),
},
}

if err := prepareViz(r, ds); err != nil {
t.Error(err.Error())
}
}

0 comments on commit bc2b00a

Please sign in to comment.