Skip to content

Commit

Permalink
fix(actions.SaveDataset): load transform from store if cafs scriptPat…
Browse files Browse the repository at this point in the history
…h provided
  • Loading branch information
b5 committed Nov 9, 2018
1 parent 5d46a4e commit 274fa76
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 13 deletions.
21 changes: 16 additions & 5 deletions actions/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package actions
import (
"fmt"
"os"
"strings"

"github.com/ipfs/go-datastore"
"github.com/qri-io/cafs"
Expand Down Expand Up @@ -64,12 +65,22 @@ func SaveDataset(node *p2p.QriNode, dsp *dataset.DatasetPod, dryRun, pin bool) (
if ds.Transform != nil {
mutateCheck := mutatedComponentsFunc(dsp)
if ds.Transform.Script == nil {
var f *os.File
f, err = os.Open(ds.Transform.ScriptPath)
if err != nil {
return
if strings.HasPrefix(ds.Transform.ScriptPath, "/ipfs") || strings.HasPrefix(ds.Transform.ScriptPath, "/map") || strings.HasPrefix(ds.Transform.ScriptPath, "/cafs") {
var f cafs.File
f, err = node.Repo.Store().Get(datastore.NewKey(ds.Transform.ScriptPath))
if err != nil {
return
}
ds.Transform.Script = f
} else {
var f *os.File
f, err = os.Open(ds.Transform.ScriptPath)
if err != nil {
return
}
ds.Transform.Script = f
}
ds.Transform.Script = f

}
// TODO - consider making this a standard method on dataset.Transform
script := cafs.NewMemfileReader(ds.Transform.ScriptPath, ds.Transform.Script)
Expand Down
18 changes: 18 additions & 0 deletions actions/recall_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package actions

import "testing"

func TestRecall(t *testing.T) {
node := newTestNode(t)
ref := addNowTransformDataset(t, node)

_, err := Recall(node, "", ref)
if err != nil {
t.Error(err)
}

_, err = Recall(node, "tf", ref)
if err != nil {
t.Error(err)
}
}
18 changes: 14 additions & 4 deletions lib/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,8 @@ func (r *DatasetRequests) Save(p *SaveParams, res *repo.DatasetRef) (err error)
}
}

if p.ReturnBody {
if ref.Dataset != nil {
ref.Dataset.Body = body
}
if p.ReturnBody && ref.Dataset != nil {
ref.Dataset.Body = body
}

*res = ref
Expand All @@ -196,6 +194,15 @@ type UpdateParams struct {
// Update advances a dataset to the latest known version from either a peer or by
// re-running a transform in the peer's namespace
func (r *DatasetRequests) Update(p *UpdateParams, res *repo.DatasetRef) error {
if r.cli != nil {
if p.ReturnBody {
// can't send an io.Reader interface over RPC
p.ReturnBody = false
log.Error("cannot return body bytes over RPC, disabling body return")
}
return r.cli.Call("DatasetRequests.Update", p, res)
}

ref, err := repo.ParseDatasetRef(p.Ref)
if err != nil {
return err
Expand Down Expand Up @@ -225,6 +232,9 @@ func (r *DatasetRequests) Update(p *UpdateParams, res *repo.DatasetRef) error {

// SetPublishStatus updates the publicity of a reference in the peer's namespace
func (r *DatasetRequests) SetPublishStatus(ref *repo.DatasetRef, res *bool) error {
if r.cli != nil {
return r.cli.Call("DatasetRequests.SetPublishStatus", ref, res)
}
res = &ref.Published
return actions.SetPublishStatus(r.node, ref, ref.Published)
}
Expand Down
41 changes: 41 additions & 0 deletions lib/datasets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,47 @@ func TestDatasetRequestsSave(t *testing.T) {
}
}

func TestDatasetRequestsSaveRecall(t *testing.T) {
node := newTestQriNode(t)
ref := addNowTransformDataset(t, node)
r := NewDatasetRequests(node, nil)

res := &repo.DatasetRef{}
err := r.Save(&SaveParams{Dataset: &dataset.DatasetPod{
Peername: ref.Peername,
Name: ref.Name,
Meta: &dataset.Meta{Title: "an updated title"},
}, ReturnBody: true}, res)
if err != nil {
t.Error("save failed")
}

err = r.Save(&SaveParams{
Dataset: &dataset.DatasetPod{
Peername: ref.Peername,
Name: ref.Name,
Meta: &dataset.Meta{Title: "an updated title"},
},
Recall: "wut"}, res)
if err == nil {
t.Error("expected bad recall to error")
}

err = r.Save(&SaveParams{
Dataset: &dataset.DatasetPod{
Peername: ref.Peername,
Name: ref.Name,
Meta: &dataset.Meta{Title: "new title!"},
},
Recall: "tf"}, res)
if err != nil {
t.Error(err)
}
if res.Dataset.Transform == nil {
t.Error("expected transform to exist on recalled save")
}
}

func TestDatasetRequestsSaveZip(t *testing.T) {
rc, _ := regmock.NewMockServer()
mr, err := testrepo.NewTestRepo(rc)
Expand Down
8 changes: 4 additions & 4 deletions lib/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ func AbsPath(path *string) (err error) {
func pathKind(path string) string {
if path == "" {
return "none"
}
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
} else if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
return "http"
}
if strings.HasPrefix(path, "/ipfs") {
} else if strings.HasPrefix(path, "/ipfs") {
return "ipfs"
} else if strings.HasPrefix(path, "/map") || strings.HasPrefix(path, "/cafs") {
return "cafs"
}
return "file"
}
Expand Down

0 comments on commit 274fa76

Please sign in to comment.