Skip to content

Commit

Permalink
refactor: use new FilterFunc.Apply
Browse files Browse the repository at this point in the history
instead of constructing temporary Filter renderers.

This also makes clearer what is going on.
  • Loading branch information
rade committed Nov 20, 2017
1 parent fd86efb commit 5095116
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
10 changes: 3 additions & 7 deletions app/api_topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,17 @@ func handleNode(ctx context.Context, renderer render.Renderer, filter render.Fil
)
// We must not lose the node during filtering. We achieve that by
// (1) rendering the report with the base renderer, without
// filtering, which gives us the node (if it exists at all), then
// (2) performing a normal filtered render of the report. If the
// filtering, which gives us the node (if it exists at all), and
// then (2) applying the filter separately to that result. If the
// node is lost in the second step, we simply put it back.
//
// To avoid repeating the work from step (1) in step (2), we
// replace the renderer in the latter with a constant renderer of
// the result obtained in step (1).
nodes := renderer.Render(rc.Report)
node, ok := nodes.Nodes[nodeID]
if !ok {
http.NotFound(w, r)
return
}
if filter != nil {
nodes = render.Render(rc.Report, render.ConstantRenderer{Nodes: nodes}, filter)
nodes = filter.Apply(nodes)
if filteredNode, ok := nodes.Nodes[nodeID]; ok {
node = filteredNode
} else { // we've lost the node during filtering; put it back
Expand Down
15 changes: 3 additions & 12 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ func (r Nodes) Merge(o Nodes) Nodes {

// Render renders the report and then applies the filter
func Render(rpt report.Report, renderer Renderer, filter FilterFunc) Nodes {
nodes := renderer.Render(rpt)
if filter != nil {
renderer = MakeFilterPseudo(filter, renderer)
nodes = filter.Apply(nodes)
}
return renderer.Render(rpt)
return nodes
}

// Reduce renderer is a Renderer which merges together the output of several
Expand Down Expand Up @@ -147,16 +148,6 @@ func (cr conditionalRenderer) Render(rpt report.Report) Nodes {
return Nodes{}
}

// ConstantRenderer renders a fixed set of nodes
type ConstantRenderer struct {
Nodes
}

// Render implements Renderer
func (c ConstantRenderer) Render(_ report.Report) Nodes {
return c.Nodes
}

// joinResults is used by Renderers that join sets of nodes
type joinResults struct {
nodes report.Nodes
Expand Down

0 comments on commit 5095116

Please sign in to comment.