Skip to content

Commit

Permalink
feat(diff): return source data in diff response
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 authored and ramfox committed Jun 7, 2019
1 parent 7456bdd commit d3eae83
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
18 changes: 1 addition & 17 deletions api/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,23 +342,7 @@ func (h *DatasetHandlers) diffHandler(w http.ResponseWriter, r *http.Request) {
return
}

env := map[string]interface{}{
"meta": map[string]interface{}{
"code": http.StatusOK,
"stat": res.Stat,
},
"data": res.Diff,
}

resData, err := json.Marshal(env)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(resData)
util.WritePageResponse(w, res, r, util.Page{})
}

func (h *DatasetHandlers) peerListHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
20 changes: 17 additions & 3 deletions api/webapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ func (s Server) ServeWebapp(ctx context.Context) {
return
}

m := http.NewServeMux()
m.Handle("/", s.middleware(s.WebappTemplateHandler))
m.Handle("/webapp/", s.FrontendHandler("/webapp"))
m := webappMuxer{
template: s.middleware(s.WebappTemplateHandler),
webapp: s.FrontendHandler("/webapp"),
}

webappserver := &http.Server{Handler: m}

Expand All @@ -37,6 +38,19 @@ func (s Server) ServeWebapp(ctx context.Context) {
return
}

type webappMuxer struct {
webapp, template http.Handler
}

func (h webappMuxer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/webapp") {
h.webapp.ServeHTTP(w, r)
return
}

h.template.ServeHTTP(w, r)
}

// FrontendHandler resolves the current webapp hash, (fetching the compiled frontend in the process)
// and serves it up as a traditional HTTP endpoint, transparently redirecting requests
// for [prefix]/foo.js to [CAFS Hash]/foo.js
Expand Down
13 changes: 10 additions & 3 deletions lib/diff.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lib

import (
"context"
"encoding/csv"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -37,8 +38,10 @@ type DiffParams struct {

// DiffResponse is the result of a call to diff
type DiffResponse struct {
Stat *DiffStat
Diff []*Delta
Stat *DiffStat `json:"stat,omitempty"`
Diff []*Delta `json:"diff,omitempty"`
A interface{} `json:"b,omitempty"`
B interface{} `json:"a,omitempty"`
}

// Diff computes the diff of two datasets
Expand Down Expand Up @@ -71,11 +74,15 @@ func (r *DatasetRequests) Diff(p *DiffParams, res *DiffResponse) (err error) {
return
}

ctx := context.Background()

_res := DiffResponse{
Stat: &deepdiff.Stats{},
A: leftData,
B: rightData,
}

if _res.Diff, err = deepdiff.Diff(leftData, rightData, deepdiff.OptionSetStats(_res.Stat)); err != nil {
if _res.Diff, err = deepdiff.Diff(ctx, leftData, rightData, deepdiff.OptionSetStats(_res.Stat)); err != nil {
return
}

Expand Down

0 comments on commit d3eae83

Please sign in to comment.