AddParent() function to optimise the case where there is only one parent

Avoids creating and discarding a StringSet just to carry the
parameters, and makes the code more readable too.
This commit is contained in:
Bryan Boreham
2018-07-16 20:22:05 +00:00
parent 0d2409c72c
commit 5edc85822b
7 changed files with 14 additions and 18 deletions

View File

@@ -407,9 +407,7 @@ func (c *container) getBaseNode() report.Node {
ContainerCommand: c.getSanitizedCommand(),
ImageID: c.Image(),
ContainerHostname: c.Hostname(),
}).WithParents(report.MakeSets().
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID(c.Image()))),
)
}).WithParent(report.ContainerImage, report.MakeContainerImageNodeID(c.Image()))
result = result.AddPrefixPropertyList(LabelPrefix, c.container.Config.Labels)
if !c.noEnvironmentVariables {
result = result.AddPrefixPropertyList(EnvPrefix, c.env())

View File

@@ -74,7 +74,7 @@ func (t *Tagger) Tag(r report.Report) (report.Report, error) {
})
r.SwarmService.AddNode(node)
r.Container.Nodes[containerID] = container.WithParents(container.Parents.Add(report.SwarmService, report.MakeStringSet(nodeID)))
r.Container.Nodes[containerID] = container.WithParent(report.SwarmService, nodeID)
}
return r, nil
@@ -116,17 +116,13 @@ func (t *Tagger) tag(tree process.Tree, topology *report.Topology) {
}
node = node.WithLatest(ContainerID, mtime.Now(), c.ID())
node = node.WithParents(report.MakeSets().
Add(report.Container, report.MakeStringSet(report.MakeContainerNodeID(c.ID()))),
)
node = node.WithParent(report.Container, report.MakeContainerNodeID(c.ID()))
// If we can work out the image name, add a parent tag for it
image, ok := t.registry.GetContainerImage(c.Image())
if ok && len(image.RepoTags) > 0 {
imageName := ImageNameWithoutTag(image.RepoTags[0])
node = node.WithParents(report.MakeSets().
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID(imageName))),
)
node = node.WithParent(report.ContainerImage, report.MakeContainerImageNodeID(imageName))
}
topology.ReplaceNode(node)