mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-28 17:51:21 +00:00
Details panel backend redesign
Megasquish:
[app] remove unused edge endpoint
[WIP] refactoring node details api endpoint
[WIP] plumbing the children through the rendering process
adding IDList.Remove and StringSet.Remove
[WIP] working on adding parents to detailed node renderings
WIP UI components with mock backend data for new details
grouping children by type
UI components for node details health and info
metric formatters for details panel
Column headers and links for details table
[WIP] started on rendering node metadata and metrics in the detail view
DetailedNode.LabelMajor -> DetailedNode.Label
rendering decent labels for parents of detailed nodes
render metrics onto the top-level detailed node
removing dead code
Links to relatives
metrics have a Format not Unit
Show more/less actions for tables and relatives
adjusted metric formatter
TopologyTagger should tag k8s topology nodes
make renderablenode ids more consistent, e.g. container:abcd1234
working on rendering correct summaries for each node
adding report.Node.Rank, so that merging is independent of order
rendering children and parents correctly
output child renderableNode ids, so we can link to them
add group field to metrics, so they can be grouped
Refactored details health items to prepare for grouping
add metrics to processNodeSummaries
hide summary section if there is no data for it
fixing up tests
moving detailed node rendering into a separate package
Node ID/Topology are fields not metadata
- This way I think we don't have to care about Metadata being non-commutative.
- ID and topology are still non-commutative, as I'm not sure how to sanely
merge them, but it's possible we don't care.
host memory usage is a filesize, not a percent
working on fixing some tests
adding children to hosts detail panel
- Had to redo how parents are calculated, so that children wouldn't interfere with it
- have to have the host at the end because it is non-commutative
only render links for linkable children (i.e. not unconnected processes)
resolving TODOs
fixing up lint errors
make nil a valid value for render.Children so tests are cleaner
working on backend tests
make client handle missing metrics property
Stop rendering container image nodes with process summaries/parents
fix parent link to container images
Calculate parents as a set on report.Node (except k8s)
refactoring detailed.NodeSummary stuff
removing RenderableNode.Summary*, we already track it on report.Node
working on tests
add Columns field to NodeSummaryGroup
fixing up render/topologies_test
fix children links to container images
get children of hosts rendering right
working on host renderer tests
Change container report.Node.ID to a1b2c3;<container>
The id should be globally unique, so we don't need the host id.
This lets the kubernetes probe return a container node with the pod id,
which will get merged into the real containers with other reports. The
catch is that the kubernetes api doesn't tell us which hostname the
container is running on, so we can't populate the old-style node ids.
change terminology of system pods and services
Fix kubernetes services with no selector
Fixes handling of kubernetes service, which has no pods
fix parent links for pods/services
refactor detailed metadata to include sets and latest data
fixing up host rendering tests
fleshing out tests for node metadata and metrics
don't render container pseudo-nodes as processes
Update test for id format change.
This commit is contained in:
24
report/id.go
24
report/id.go
@@ -102,13 +102,18 @@ func MakeHostNodeID(hostID string) string {
|
||||
}
|
||||
|
||||
// MakeContainerNodeID produces a container node ID from its composite parts.
|
||||
func MakeContainerNodeID(hostID, containerID string) string {
|
||||
return hostID + ScopeDelim + containerID
|
||||
func MakeContainerNodeID(containerID string) string {
|
||||
return containerID + ScopeDelim + "<container>"
|
||||
}
|
||||
|
||||
// MakeContainerImageNodeID produces a container image node ID from its composite parts.
|
||||
func MakeContainerImageNodeID(hostID, containerImageID string) string {
|
||||
return hostID + ScopeDelim + containerImageID
|
||||
}
|
||||
|
||||
// MakePodNodeID produces a pod node ID from its composite parts.
|
||||
func MakePodNodeID(hostID, podID string) string {
|
||||
return hostID + ScopeDelim + podID
|
||||
func MakePodNodeID(namespaceID, podID string) string {
|
||||
return namespaceID + ScopeDelim + podID
|
||||
}
|
||||
|
||||
// MakeServiceNodeID produces a service node ID from its composite parts.
|
||||
@@ -143,14 +148,13 @@ func ParseEndpointNodeID(endpointNodeID string) (hostID, address, port string, o
|
||||
return fields[0], fields[1], fields[2], true
|
||||
}
|
||||
|
||||
// ParseContainerNodeID produces the host and container id from an container
|
||||
// node ID.
|
||||
func ParseContainerNodeID(containerNodeID string) (hostID, containerID string, ok bool) {
|
||||
// ParseContainerNodeID produces the container id from an container node ID.
|
||||
func ParseContainerNodeID(containerNodeID string) (containerID string, ok bool) {
|
||||
fields := strings.SplitN(containerNodeID, ScopeDelim, 2)
|
||||
if len(fields) != 2 {
|
||||
return "", "", false
|
||||
if len(fields) != 2 || fields[1] != "<container>" {
|
||||
return "", false
|
||||
}
|
||||
return fields[0], fields[1], true
|
||||
return fields[0], true
|
||||
}
|
||||
|
||||
// ParseAddressNodeID produces the host ID, address from an address node ID.
|
||||
|
||||
@@ -15,6 +15,11 @@ func (a IDList) Add(ids ...string) IDList {
|
||||
return IDList(StringSet(a).Add(ids...))
|
||||
}
|
||||
|
||||
// Remove is the only correct way to remove IDs from an IDList.
|
||||
func (a IDList) Remove(ids ...string) IDList {
|
||||
return IDList(StringSet(a).Remove(ids...))
|
||||
}
|
||||
|
||||
// Copy returns a copy of the IDList.
|
||||
func (a IDList) Copy() IDList {
|
||||
return IDList(StringSet(a).Copy())
|
||||
|
||||
@@ -193,7 +193,30 @@ func TestMergeNodes(t *testing.T) {
|
||||
}),
|
||||
},
|
||||
},
|
||||
"Merge conflict": {
|
||||
"Merge conflict with rank difference": {
|
||||
a: report.Nodes{
|
||||
":192.168.1.1:12345": report.MakeNodeWith(map[string]string{
|
||||
PID: "23128",
|
||||
Name: "curl",
|
||||
Domain: "node-a.local",
|
||||
}),
|
||||
},
|
||||
b: report.Nodes{
|
||||
":192.168.1.1:12345": report.MakeNodeWith(map[string]string{ // <-- same ID
|
||||
PID: "0",
|
||||
Name: "curl",
|
||||
Domain: "node-a.local",
|
||||
}),
|
||||
},
|
||||
want: report.Nodes{
|
||||
":192.168.1.1:12345": report.MakeNodeWith(map[string]string{
|
||||
PID: "23128",
|
||||
Name: "curl",
|
||||
Domain: "node-a.local",
|
||||
}),
|
||||
},
|
||||
},
|
||||
"Merge conflict with no rank difference": {
|
||||
a: report.Nodes{
|
||||
":192.168.1.1:12345": report.MakeNodeWith(map[string]string{
|
||||
PID: "23128",
|
||||
|
||||
@@ -235,7 +235,9 @@ func parseTime(s string) time.Time {
|
||||
return t
|
||||
}
|
||||
|
||||
func (m Metric) toIntermediate() WireMetrics {
|
||||
// ToIntermediate converts the metric to a representation suitable
|
||||
// for serialization.
|
||||
func (m Metric) ToIntermediate() WireMetrics {
|
||||
samples := []Sample{}
|
||||
if m.Samples != nil {
|
||||
m.Samples.Reverse().ForEach(func(s interface{}) {
|
||||
@@ -268,7 +270,7 @@ func (m WireMetrics) fromIntermediate() Metric {
|
||||
// MarshalJSON implements json.Marshaller
|
||||
func (m Metric) MarshalJSON() ([]byte, error) {
|
||||
buf := bytes.Buffer{}
|
||||
in := m.toIntermediate()
|
||||
in := m.ToIntermediate()
|
||||
err := json.NewEncoder(&buf).Encode(in)
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
@@ -286,7 +288,7 @@ func (m *Metric) UnmarshalJSON(input []byte) error {
|
||||
// GobEncode implements gob.Marshaller
|
||||
func (m Metric) GobEncode() ([]byte, error) {
|
||||
buf := bytes.Buffer{}
|
||||
err := gob.NewEncoder(&buf).Encode(m.toIntermediate())
|
||||
err := gob.NewEncoder(&buf).Encode(m.ToIntermediate())
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
|
||||
69
report/node_set.go
Normal file
69
report/node_set.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package report
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
// NodeSet is a sorted set of nodes keyed on (Topology, ID). Clients must use
|
||||
// the Add method to add nodes
|
||||
type NodeSet []Node
|
||||
|
||||
// MakeNodeSet makes a new NodeSet with the given nodes.
|
||||
// TODO: Make this more efficient
|
||||
func MakeNodeSet(nodes ...Node) NodeSet {
|
||||
if len(nodes) <= 0 {
|
||||
return nil
|
||||
}
|
||||
result := NodeSet{}
|
||||
for _, node := range nodes {
|
||||
result = result.Add(node)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Add adds the nodes to the NodeSet. Add is the only valid way to grow a
|
||||
// NodeSet. Add returns the NodeSet to enable chaining.
|
||||
func (n NodeSet) Add(nodes ...Node) NodeSet {
|
||||
for _, node := range nodes {
|
||||
i := sort.Search(len(n), func(i int) bool {
|
||||
return n[i].Topology >= node.Topology && n[i].ID >= node.ID
|
||||
})
|
||||
if i < len(n) && n[i].Topology == node.Topology && n[i].ID == node.ID {
|
||||
// The list already has the element.
|
||||
continue
|
||||
}
|
||||
// It a new element, insert it in order.
|
||||
n = append(n, Node{})
|
||||
copy(n[i+1:], n[i:])
|
||||
n[i] = node.Copy()
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// Merge combines the two NodeSets and returns a new result.
|
||||
// TODO: Make this more efficient
|
||||
func (n NodeSet) Merge(other NodeSet) NodeSet {
|
||||
switch {
|
||||
case len(other) <= 0: // Optimise special case, to avoid allocating
|
||||
return n // (note unit test DeepEquals breaks if we don't do this)
|
||||
case len(n) <= 0:
|
||||
return other
|
||||
}
|
||||
result := n.Copy()
|
||||
for _, node := range other {
|
||||
result = result.Add(node)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Copy returns a value copy of the NodeSet.
|
||||
func (n NodeSet) Copy() NodeSet {
|
||||
if n == nil {
|
||||
return n
|
||||
}
|
||||
result := make(NodeSet, len(n))
|
||||
for i, node := range n {
|
||||
result[i] = node.Copy()
|
||||
}
|
||||
return result
|
||||
}
|
||||
152
report/node_set_test.go
Normal file
152
report/node_set_test.go
Normal file
@@ -0,0 +1,152 @@
|
||||
package report_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/weaveworks/scope/report"
|
||||
)
|
||||
|
||||
type nodeSpec struct {
|
||||
topology string
|
||||
id string
|
||||
}
|
||||
|
||||
func TestMakeNodeSet(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
inputs []nodeSpec
|
||||
wants []nodeSpec
|
||||
}{
|
||||
{inputs: nil, wants: nil},
|
||||
{inputs: []nodeSpec{}, wants: []nodeSpec{}},
|
||||
{
|
||||
inputs: []nodeSpec{{"", "a"}},
|
||||
wants: []nodeSpec{{"", "a"}},
|
||||
},
|
||||
{
|
||||
inputs: []nodeSpec{{"", "a"}, {"", "a"}, {"1", "a"}},
|
||||
wants: []nodeSpec{{"", "a"}, {"1", "a"}},
|
||||
},
|
||||
{
|
||||
inputs: []nodeSpec{{"", "b"}, {"", "c"}, {"", "a"}},
|
||||
wants: []nodeSpec{{"", "a"}, {"", "b"}, {"", "c"}},
|
||||
},
|
||||
{
|
||||
inputs: []nodeSpec{{"2", "a"}, {"3", "a"}, {"1", "a"}},
|
||||
wants: []nodeSpec{{"1", "a"}, {"2", "a"}, {"3", "a"}},
|
||||
},
|
||||
} {
|
||||
var (
|
||||
inputs []report.Node
|
||||
wants []report.Node
|
||||
)
|
||||
for _, spec := range testcase.inputs {
|
||||
inputs = append(inputs, report.MakeNode().WithTopology(spec.topology).WithID(spec.id))
|
||||
}
|
||||
for _, spec := range testcase.wants {
|
||||
wants = append(wants, report.MakeNode().WithTopology(spec.topology).WithID(spec.id))
|
||||
}
|
||||
if want, have := report.NodeSet(wants), report.MakeNodeSet(inputs...); !reflect.DeepEqual(want, have) {
|
||||
t.Errorf("%#v: want %#v, have %#v", testcase.inputs, want, have)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeSetAdd(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
input report.NodeSet
|
||||
nodes []report.Node
|
||||
want report.NodeSet
|
||||
}{
|
||||
{input: report.NodeSet(nil), nodes: []report.Node{}, want: report.NodeSet(nil)},
|
||||
{
|
||||
input: report.MakeNodeSet(),
|
||||
nodes: []report.Node{},
|
||||
want: report.MakeNodeSet(),
|
||||
},
|
||||
{
|
||||
input: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
nodes: []report.Node{},
|
||||
want: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
},
|
||||
{
|
||||
input: report.MakeNodeSet(),
|
||||
nodes: []report.Node{report.MakeNode().WithID("a")},
|
||||
want: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
},
|
||||
{
|
||||
input: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
nodes: []report.Node{report.MakeNode().WithID("a")},
|
||||
want: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
},
|
||||
{
|
||||
input: report.MakeNodeSet(report.MakeNode().WithID("b")),
|
||||
nodes: []report.Node{report.MakeNode().WithID("a"), report.MakeNode().WithID("b")},
|
||||
want: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("b")),
|
||||
},
|
||||
{
|
||||
input: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
nodes: []report.Node{report.MakeNode().WithID("c"), report.MakeNode().WithID("b")},
|
||||
want: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("b"), report.MakeNode().WithID("c")),
|
||||
},
|
||||
{
|
||||
input: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("c")),
|
||||
nodes: []report.Node{report.MakeNode().WithID("b"), report.MakeNode().WithID("b"), report.MakeNode().WithID("b")},
|
||||
want: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("b"), report.MakeNode().WithID("c")),
|
||||
},
|
||||
} {
|
||||
if want, have := testcase.want, testcase.input.Add(testcase.nodes...); !reflect.DeepEqual(want, have) {
|
||||
t.Errorf("%v + %v: want %v, have %v", testcase.input, testcase.nodes, want, have)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeSetMerge(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
input report.NodeSet
|
||||
other report.NodeSet
|
||||
want report.NodeSet
|
||||
}{
|
||||
{input: report.NodeSet(nil), other: report.NodeSet(nil), want: report.NodeSet(nil)},
|
||||
{input: report.MakeNodeSet(), other: report.MakeNodeSet(), want: report.MakeNodeSet()},
|
||||
{
|
||||
input: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
other: report.MakeNodeSet(),
|
||||
want: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
},
|
||||
{
|
||||
input: report.MakeNodeSet(),
|
||||
other: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
want: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
},
|
||||
{
|
||||
input: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
other: report.MakeNodeSet(report.MakeNode().WithID("b")),
|
||||
want: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("b")),
|
||||
},
|
||||
{
|
||||
input: report.MakeNodeSet(report.MakeNode().WithID("b")),
|
||||
other: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
want: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("b")),
|
||||
},
|
||||
{
|
||||
input: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
other: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
want: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
},
|
||||
{
|
||||
input: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("c")),
|
||||
other: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("b")),
|
||||
want: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("b"), report.MakeNode().WithID("c")),
|
||||
},
|
||||
{
|
||||
input: report.MakeNodeSet(report.MakeNode().WithID("b")),
|
||||
other: report.MakeNodeSet(report.MakeNode().WithID("a")),
|
||||
want: report.MakeNodeSet(report.MakeNode().WithID("a"), report.MakeNode().WithID("b")),
|
||||
},
|
||||
} {
|
||||
if want, have := testcase.want, testcase.input.Merge(testcase.other); !reflect.DeepEqual(want, have) {
|
||||
t.Errorf("%v + %v: want %v, have %v", testcase.input, testcase.other, want, have)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -84,6 +84,8 @@ func (n Nodes) Merge(other Nodes) Nodes {
|
||||
// given node in a given topology, along with the edges emanating from the
|
||||
// node and metadata about those edges.
|
||||
type Node struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Topology string `json:"topology,omitempty"`
|
||||
Metadata Metadata `json:"metadata,omitempty"`
|
||||
Counters Counters `json:"counters,omitempty"`
|
||||
Sets Sets `json:"sets,omitempty"`
|
||||
@@ -92,6 +94,7 @@ type Node struct {
|
||||
Controls NodeControls `json:"controls,omitempty"`
|
||||
Latest LatestMap `json:"latest,omitempty"`
|
||||
Metrics Metrics `json:"metrics,omitempty"`
|
||||
Parents Sets `json:"parents,omitempty"`
|
||||
}
|
||||
|
||||
// MakeNode creates a new Node with no initial metadata.
|
||||
@@ -99,12 +102,12 @@ func MakeNode() Node {
|
||||
return Node{
|
||||
Metadata: Metadata{},
|
||||
Counters: Counters{},
|
||||
Sets: Sets{},
|
||||
Adjacency: MakeIDList(),
|
||||
Edges: EdgeMetadatas{},
|
||||
Controls: MakeNodeControls(),
|
||||
Latest: MakeLatestMap(),
|
||||
Metrics: Metrics{},
|
||||
Parents: Sets{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,6 +116,20 @@ func MakeNodeWith(m map[string]string) Node {
|
||||
return MakeNode().WithMetadata(m)
|
||||
}
|
||||
|
||||
// WithID returns a fresh copy of n, with ID changed.
|
||||
func (n Node) WithID(id string) Node {
|
||||
result := n.Copy()
|
||||
result.ID = id
|
||||
return result
|
||||
}
|
||||
|
||||
// WithTopology returns a fresh copy of n, with ID changed.
|
||||
func (n Node) WithTopology(topology string) Node {
|
||||
result := n.Copy()
|
||||
result.Topology = topology
|
||||
return result
|
||||
}
|
||||
|
||||
// WithMetadata returns a fresh copy of n, with Metadata m merged in.
|
||||
func (n Node) WithMetadata(m map[string]string) Node {
|
||||
result := n.Copy()
|
||||
@@ -130,8 +147,7 @@ func (n Node) WithCounters(c map[string]int) Node {
|
||||
// WithSet returns a fresh copy of n, with set merged in at key.
|
||||
func (n Node) WithSet(key string, set StringSet) Node {
|
||||
result := n.Copy()
|
||||
existing := n.Sets[key]
|
||||
result.Sets[key] = existing.Merge(set)
|
||||
result.Sets = result.Sets.Merge(Sets{key: set})
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -186,9 +202,18 @@ func (n Node) WithLatest(k string, ts time.Time, v string) Node {
|
||||
return result
|
||||
}
|
||||
|
||||
// WithParents returns a fresh copy of n, with sets merged in.
|
||||
func (n Node) WithParents(parents Sets) Node {
|
||||
result := n.Copy()
|
||||
result.Parents = result.Parents.Merge(parents)
|
||||
return result
|
||||
}
|
||||
|
||||
// Copy returns a value copy of the Node.
|
||||
func (n Node) Copy() Node {
|
||||
cp := MakeNode()
|
||||
cp.ID = n.ID
|
||||
cp.Topology = n.Topology
|
||||
cp.Metadata = n.Metadata.Copy()
|
||||
cp.Counters = n.Counters.Copy()
|
||||
cp.Sets = n.Sets.Copy()
|
||||
@@ -197,6 +222,7 @@ func (n Node) Copy() Node {
|
||||
cp.Controls = n.Controls.Copy()
|
||||
cp.Latest = n.Latest.Copy()
|
||||
cp.Metrics = n.Metrics.Copy()
|
||||
cp.Parents = n.Parents.Copy()
|
||||
return cp
|
||||
}
|
||||
|
||||
@@ -204,6 +230,12 @@ func (n Node) Copy() Node {
|
||||
// fresh node.
|
||||
func (n Node) Merge(other Node) Node {
|
||||
cp := n.Copy()
|
||||
if cp.ID == "" {
|
||||
cp.ID = other.ID
|
||||
}
|
||||
if cp.Topology == "" {
|
||||
cp.Topology = other.Topology
|
||||
}
|
||||
cp.Metadata = cp.Metadata.Merge(other.Metadata)
|
||||
cp.Counters = cp.Counters.Merge(other.Counters)
|
||||
cp.Sets = cp.Sets.Merge(other.Sets)
|
||||
@@ -212,6 +244,7 @@ func (n Node) Merge(other Node) Node {
|
||||
cp.Controls = cp.Controls.Merge(other.Controls)
|
||||
cp.Latest = cp.Latest.Merge(other.Latest)
|
||||
cp.Metrics = cp.Metrics.Merge(other.Metrics)
|
||||
cp.Parents = cp.Parents.Merge(other.Parents)
|
||||
return cp
|
||||
}
|
||||
|
||||
@@ -268,6 +301,9 @@ type Sets map[string]StringSet
|
||||
func (s Sets) Merge(other Sets) Sets {
|
||||
result := s.Copy()
|
||||
for k, v := range other {
|
||||
if result == nil {
|
||||
result = Sets{}
|
||||
}
|
||||
result[k] = result[k].Merge(v)
|
||||
}
|
||||
return result
|
||||
@@ -275,6 +311,9 @@ func (s Sets) Merge(other Sets) Sets {
|
||||
|
||||
// Copy returns a value copy of the sets map.
|
||||
func (s Sets) Copy() Sets {
|
||||
if s == nil {
|
||||
return s
|
||||
}
|
||||
result := Sets{}
|
||||
for k, v := range s {
|
||||
result[k] = v.Copy()
|
||||
@@ -321,6 +360,21 @@ func (s StringSet) Add(strs ...string) StringSet {
|
||||
return s
|
||||
}
|
||||
|
||||
// Remove removes the strings from the StringSet. Remove is the only valid way
|
||||
// to shrink a StringSet. Remove returns the StringSet to enable chaining.
|
||||
func (s StringSet) Remove(strs ...string) StringSet {
|
||||
for _, str := range strs {
|
||||
i := sort.Search(len(s), func(i int) bool { return s[i] >= str })
|
||||
if i >= len(s) || s[i] != str {
|
||||
// The list does not have the element.
|
||||
continue
|
||||
}
|
||||
// has the element, remove it.
|
||||
s = append(s[:i], s[i+1:]...)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Merge combines the two StringSets and returns a new result.
|
||||
func (s StringSet) Merge(other StringSet) StringSet {
|
||||
switch {
|
||||
|
||||
@@ -45,6 +45,27 @@ func TestStringSetAdd(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringSetRemove(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
input report.StringSet
|
||||
strs []string
|
||||
want report.StringSet
|
||||
}{
|
||||
{input: report.StringSet(nil), strs: []string{}, want: report.StringSet(nil)},
|
||||
{input: report.MakeStringSet(), strs: []string{}, want: report.MakeStringSet()},
|
||||
{input: report.MakeStringSet("a"), strs: []string{}, want: report.MakeStringSet("a")},
|
||||
{input: report.MakeStringSet(), strs: []string{"a"}, want: report.MakeStringSet()},
|
||||
{input: report.MakeStringSet("a"), strs: []string{"a"}, want: report.StringSet{}},
|
||||
{input: report.MakeStringSet("b"), strs: []string{"a", "b"}, want: report.StringSet{}},
|
||||
{input: report.MakeStringSet("a"), strs: []string{"c", "b"}, want: report.MakeStringSet("a")},
|
||||
{input: report.MakeStringSet("a", "c"), strs: []string{"b", "b", "b"}, want: report.MakeStringSet("a", "c")},
|
||||
} {
|
||||
if want, have := testcase.want, testcase.input.Remove(testcase.strs...); !reflect.DeepEqual(want, have) {
|
||||
t.Errorf("%v - %v: want %#v, have %#v", testcase.input, testcase.strs, want, have)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringSetMerge(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
input report.StringSet
|
||||
|
||||
Reference in New Issue
Block a user