Skip to content

Commit

Permalink
feat(core.HistoryRequests.Log): deliver history as a log of dataset r…
Browse files Browse the repository at this point in the history
…eferences
  • Loading branch information
b5 committed Nov 14, 2017
1 parent f1972dc commit 6ca1839
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
3 changes: 1 addition & 2 deletions api/handlers/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

util "github.com/datatogether/api/apiutil"
"github.com/ipfs/go-datastore"
"github.com/qri-io/dataset"
"github.com/qri-io/qri/core"
"github.com/qri-io/qri/logging"
"github.com/qri-io/qri/repo"
Expand Down Expand Up @@ -40,7 +39,7 @@ func (h *HistoryHandlers) logHandler(w http.ResponseWriter, r *http.Request) {
Path: datastore.NewKey(r.URL.Path[len("/history/"):]),
}

res := []*dataset.Dataset{}
res := []*repo.DatasetRef{}
if err := h.Log(params, &res); err != nil {
h.log.Infof("")
util.WriteErrResponse(w, http.StatusInternalServerError, err)
Expand Down
20 changes: 8 additions & 12 deletions core/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/ipfs/go-datastore"
"github.com/qri-io/dataset"
"github.com/qri-io/dataset/dsfs"
"github.com/qri-io/qri/repo"
)
Expand All @@ -24,32 +23,29 @@ type LogParams struct {
ListParams
}

func (d *HistoryRequests) Log(params *LogParams, log *[]*dataset.Dataset) (err error) {
dss := []*dataset.Dataset{}
func (d *HistoryRequests) Log(params *LogParams, res *[]*repo.DatasetRef) (err error) {
log := []*repo.DatasetRef{}
limit := params.Limit
ds := &dataset.Dataset{Previous: params.Path}
ref := &repo.DatasetRef{Path: params.Path}

if params.Path.String() == "" {
return fmt.Errorf("path is required")
}

for {
if ds.Previous.String() == "" {
break
}

ds, err = dsfs.LoadDataset(d.repo.Store(), ds.Previous)
ref.Dataset, err = dsfs.LoadDataset(d.repo.Store(), ref.Path)
if err != nil {
return err
}
dss = append(dss, ds)
log = append(log, ref)

limit--
if limit == 0 {
if limit == 0 || ref.Dataset.Previous.String() == "" {
break
}
ref = &repo.DatasetRef{Path: ref.Dataset.Previous}
}

*log = dss
*res = log
return nil
}
8 changes: 4 additions & 4 deletions core/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

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

Expand All @@ -22,17 +22,17 @@ func TestHistoryRequestsLog(t *testing.T) {

cases := []struct {
p *LogParams
res []*dataset.Dataset
res []*repo.DatasetRef
err string
}{
{&LogParams{}, nil, "path is required"},
{&LogParams{Path: datastore.NewKey("/badpath")}, nil, "error getting file bytes: datastore: key not found"},
{&LogParams{Path: path}, []*dataset.Dataset{&dataset.Dataset{}}, ""},
{&LogParams{Path: path}, []*repo.DatasetRef{&repo.DatasetRef{Path: path}}, ""},
}

req := NewHistoryRequests(mr)
for i, c := range cases {
got := []*dataset.Dataset{}
got := []*repo.DatasetRef{}
err := req.Log(c.p, &got)

if !(err == nil && c.err == "" || err != nil && err.Error() == c.err) {
Expand Down

0 comments on commit 6ca1839

Please sign in to comment.