2021-10-26 01:21:00 +02:00
|
|
|
// Copyright 2021 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package compliance
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LicenseGraph describes the immutable license metadata for a set of root
|
|
|
|
// targets and the transitive closure of their dependencies.
|
|
|
|
//
|
|
|
|
// Alternatively, a graph is a set of edges. In this case directed, annotated
|
|
|
|
// edges from targets to dependencies.
|
|
|
|
//
|
|
|
|
// A LicenseGraph provides the frame of reference for all of the other types
|
|
|
|
// defined here. It is possible to have multiple graphs, and to have targets,
|
|
|
|
// edges, and resolutions from multiple graphs. But it is an error to try to
|
|
|
|
// mix items from different graphs in the same operation.
|
|
|
|
// May panic if attempted.
|
|
|
|
//
|
|
|
|
// The compliance package assumes specific private implementations of each of
|
|
|
|
// these interfaces. May panic if attempts are made to combine different
|
|
|
|
// implementations of some interfaces with expected implementations of other
|
|
|
|
// interfaces here.
|
|
|
|
type LicenseGraph struct {
|
|
|
|
// rootFiles identifies the original set of files to read. (immutable)
|
|
|
|
//
|
|
|
|
// Defines the starting "top" for top-down walks.
|
|
|
|
//
|
|
|
|
// Alternatively, an instance of licenseGraphImp conceptually defines a scope within
|
|
|
|
// the universe of build graphs as a sub-graph rooted at rootFiles where all edges
|
|
|
|
// and targets for the instance are defined relative to and within that scope. For
|
|
|
|
// most analyses, the correct scope is to root the graph at all of the distributed
|
|
|
|
// artifacts.
|
|
|
|
rootFiles []string
|
|
|
|
|
|
|
|
// edges lists the directed edges in the graph from target to dependency. (guarded by mu)
|
|
|
|
//
|
|
|
|
// Alternatively, the graph is the set of `edges`.
|
2022-01-10 22:50:57 +01:00
|
|
|
edges TargetEdgeList
|
2021-10-26 01:21:00 +02:00
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// targets identifies, indexes, and describes the entire set of target node files.
|
2021-10-26 01:21:00 +02:00
|
|
|
/// (guarded by mu)
|
|
|
|
targets map[string]*TargetNode
|
|
|
|
|
2022-10-30 06:45:12 +01:00
|
|
|
// onceBottomUp makes sure the bottom-up resolve walk only happens one time.
|
|
|
|
onceBottomUp sync.Once
|
2021-10-26 01:21:00 +02:00
|
|
|
|
2022-10-30 06:45:12 +01:00
|
|
|
// onceTopDown makes sure the top-down resolve walk only happens one time.
|
|
|
|
onceTopDown sync.Once
|
2021-10-26 01:21:00 +02:00
|
|
|
|
|
|
|
// shippedNodes caches the results of a full walk of nodes identifying targets
|
|
|
|
// distributed either directly or as derivative works. (creation guarded by mu)
|
|
|
|
shippedNodes *TargetNodeSet
|
|
|
|
|
|
|
|
// mu guards against concurrent update.
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edges returns the list of edges in the graph. (unordered)
|
|
|
|
func (lg *LicenseGraph) Edges() TargetEdgeList {
|
|
|
|
edges := make(TargetEdgeList, 0, len(lg.edges))
|
2022-01-10 22:50:57 +01:00
|
|
|
edges = append(edges, lg.edges...)
|
2021-10-26 01:21:00 +02:00
|
|
|
return edges
|
|
|
|
}
|
|
|
|
|
|
|
|
// Targets returns the list of target nodes in the graph. (unordered)
|
|
|
|
func (lg *LicenseGraph) Targets() TargetNodeList {
|
|
|
|
targets := make(TargetNodeList, 0, len(lg.targets))
|
2022-01-10 22:50:57 +01:00
|
|
|
for _, target := range lg.targets {
|
|
|
|
targets = append(targets, target)
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
return targets
|
|
|
|
}
|
|
|
|
|
2022-11-12 06:46:12 +01:00
|
|
|
// TargetNames returns the list of target node names in the graph. (unordered)
|
|
|
|
func (lg *LicenseGraph) TargetNames() []string {
|
|
|
|
targets := make([]string, 0, len(lg.targets))
|
|
|
|
for target := range lg.targets {
|
|
|
|
targets = append(targets, target)
|
|
|
|
}
|
|
|
|
return targets
|
|
|
|
}
|
|
|
|
|
2021-10-26 01:21:00 +02:00
|
|
|
// compliance-only LicenseGraph methods
|
|
|
|
|
|
|
|
// newLicenseGraph constructs a new, empty instance of LicenseGraph.
|
|
|
|
func newLicenseGraph() *LicenseGraph {
|
|
|
|
return &LicenseGraph{
|
|
|
|
rootFiles: []string{},
|
|
|
|
targets: make(map[string]*TargetNode),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TargetEdge describes a directed, annotated edge from a target to a
|
|
|
|
// dependency. (immutable)
|
|
|
|
//
|
|
|
|
// A LicenseGraph, above, is a set of TargetEdges.
|
|
|
|
//
|
|
|
|
// i.e. `Target` depends on `Dependency` in the manner described by
|
|
|
|
// `Annotations`.
|
|
|
|
type TargetEdge struct {
|
2022-01-10 22:50:57 +01:00
|
|
|
// target and dependency identify the nodes connected by the edge.
|
|
|
|
target, dependency *TargetNode
|
2021-10-26 01:21:00 +02:00
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// annotations identifies the set of compliance-relevant annotations describing the edge.
|
|
|
|
annotations TargetEdgeAnnotations
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Target identifies the target that depends on the dependency.
|
|
|
|
//
|
|
|
|
// Target needs Dependency to build.
|
2022-01-10 22:50:57 +01:00
|
|
|
func (e *TargetEdge) Target() *TargetNode {
|
|
|
|
return e.target
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dependency identifies the target depended on by the target.
|
|
|
|
//
|
|
|
|
// Dependency builds without Target, but Target needs Dependency to build.
|
2022-01-10 22:50:57 +01:00
|
|
|
func (e *TargetEdge) Dependency() *TargetNode {
|
|
|
|
return e.dependency
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Annotations describes the type of edge by the set of annotations attached to
|
|
|
|
// it.
|
|
|
|
//
|
|
|
|
// Only annotations prescribed by policy have any meaning for licensing, and
|
|
|
|
// the meaning for licensing is likewise prescribed by policy. Other annotations
|
|
|
|
// are preserved and ignored by policy.
|
2022-01-10 22:50:57 +01:00
|
|
|
func (e *TargetEdge) Annotations() TargetEdgeAnnotations {
|
|
|
|
return e.annotations
|
|
|
|
}
|
|
|
|
|
2022-10-22 03:28:05 +02:00
|
|
|
// IsRuntimeDependency returns true for edges representing shared libraries
|
|
|
|
// linked dynamically at runtime.
|
|
|
|
func (e *TargetEdge) IsRuntimeDependency() bool {
|
|
|
|
return edgeIsDynamicLink(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDerivation returns true for edges where the target is a derivative
|
|
|
|
// work of dependency.
|
|
|
|
func (e *TargetEdge) IsDerivation() bool {
|
|
|
|
return edgeIsDerivation(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsBuildTool returns true for edges where the target is built
|
|
|
|
// by dependency.
|
|
|
|
func (e *TargetEdge) IsBuildTool() bool {
|
|
|
|
return !edgeIsDerivation(e) && !edgeIsDynamicLink(e)
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// String returns a human-readable string representation of the edge.
|
|
|
|
func (e *TargetEdge) String() string {
|
|
|
|
return fmt.Sprintf("%s -[%s]> %s", e.target.name, strings.Join(e.annotations.AsList(), ", "), e.dependency.name)
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TargetEdgeList orders lists of edges by target then dependency then annotations.
|
2022-01-10 22:50:57 +01:00
|
|
|
type TargetEdgeList []*TargetEdge
|
2021-10-26 01:21:00 +02:00
|
|
|
|
|
|
|
// Len returns the count of the elmements in the list.
|
2022-01-28 00:18:52 +01:00
|
|
|
func (l TargetEdgeList) Len() int { return len(l) }
|
2021-10-26 01:21:00 +02:00
|
|
|
|
|
|
|
// Swap rearranges 2 elements so that each occupies the other's former position.
|
|
|
|
func (l TargetEdgeList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
|
|
|
|
|
|
|
// Less returns true when the `i`th element is lexicographically less than the `j`th.
|
|
|
|
func (l TargetEdgeList) Less(i, j int) bool {
|
2022-01-10 22:50:57 +01:00
|
|
|
namei := l[i].target.name
|
|
|
|
namej := l[j].target.name
|
|
|
|
if namei == namej {
|
|
|
|
namei = l[i].dependency.name
|
|
|
|
namej = l[j].dependency.name
|
|
|
|
}
|
|
|
|
if namei == namej {
|
|
|
|
return l[i].annotations.Compare(l[j].annotations) < 0
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
return namei < namej
|
|
|
|
}
|
|
|
|
|
|
|
|
// TargetEdgePathSegment describes a single arc in a TargetPath associating the
|
|
|
|
// edge with a context `ctx` defined by whatever process is creating the path.
|
|
|
|
type TargetEdgePathSegment struct {
|
|
|
|
edge *TargetEdge
|
2022-01-28 00:18:52 +01:00
|
|
|
ctx interface{}
|
2022-01-10 22:50:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Target identifies the target that depends on the dependency.
|
|
|
|
//
|
|
|
|
// Target needs Dependency to build.
|
|
|
|
func (s TargetEdgePathSegment) Target() *TargetNode {
|
|
|
|
return s.edge.target
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dependency identifies the target depended on by the target.
|
|
|
|
//
|
|
|
|
// Dependency builds without Target, but Target needs Dependency to build.
|
|
|
|
func (s TargetEdgePathSegment) Dependency() *TargetNode {
|
|
|
|
return s.edge.dependency
|
|
|
|
}
|
|
|
|
|
2022-10-22 03:28:05 +02:00
|
|
|
// Edge describes the target edge.
|
|
|
|
func (s TargetEdgePathSegment) Edge() *TargetEdge {
|
|
|
|
return s.edge
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// Annotations describes the type of edge by the set of annotations attached to
|
|
|
|
// it.
|
|
|
|
//
|
|
|
|
// Only annotations prescribed by policy have any meaning for licensing, and
|
|
|
|
// the meaning for licensing is likewise prescribed by policy. Other annotations
|
|
|
|
// are preserved and ignored by policy.
|
|
|
|
func (s TargetEdgePathSegment) Annotations() TargetEdgeAnnotations {
|
|
|
|
return s.edge.annotations
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context returns the context associated with the path segment. The type and
|
|
|
|
// value of the context defined by the process creating the path.
|
|
|
|
func (s TargetEdgePathSegment) Context() interface{} {
|
|
|
|
return s.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a human-readable string representation of the edge.
|
|
|
|
func (s TargetEdgePathSegment) String() string {
|
|
|
|
return fmt.Sprintf("%s -[%s]> %s", s.edge.target.name, strings.Join(s.edge.annotations.AsList(), ", "), s.edge.dependency.name)
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TargetEdgePath describes a sequence of edges starting at a root and ending
|
|
|
|
// at some final dependency.
|
2022-01-10 22:50:57 +01:00
|
|
|
type TargetEdgePath []TargetEdgePathSegment
|
2021-10-26 01:21:00 +02:00
|
|
|
|
|
|
|
// NewTargetEdgePath creates a new, empty path with capacity `cap`.
|
|
|
|
func NewTargetEdgePath(cap int) *TargetEdgePath {
|
|
|
|
p := make(TargetEdgePath, 0, cap)
|
|
|
|
return &p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push appends a new edge to the list verifying that the target of the new
|
|
|
|
// edge is the dependency of the prior.
|
2022-01-10 22:50:57 +01:00
|
|
|
func (p *TargetEdgePath) Push(edge *TargetEdge, ctx interface{}) {
|
2021-10-26 01:21:00 +02:00
|
|
|
if len(*p) == 0 {
|
2022-01-10 22:50:57 +01:00
|
|
|
*p = append(*p, TargetEdgePathSegment{edge, ctx})
|
2021-10-26 01:21:00 +02:00
|
|
|
return
|
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
if (*p)[len(*p)-1].edge.dependency != edge.target {
|
|
|
|
panic(fmt.Errorf("disjoint path %s does not end at %s", p.String(), edge.target.name))
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
*p = append(*p, TargetEdgePathSegment{edge, ctx})
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pop shortens the path by 1 edge.
|
|
|
|
func (p *TargetEdgePath) Pop() {
|
|
|
|
if len(*p) == 0 {
|
|
|
|
panic(fmt.Errorf("attempt to remove edge from empty path"))
|
|
|
|
}
|
|
|
|
*p = (*p)[:len(*p)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear makes the path length 0.
|
|
|
|
func (p *TargetEdgePath) Clear() {
|
|
|
|
*p = (*p)[:0]
|
|
|
|
}
|
|
|
|
|
2021-12-10 07:10:43 +01:00
|
|
|
// Copy makes a new path with the same value.
|
|
|
|
func (p *TargetEdgePath) Copy() *TargetEdgePath {
|
|
|
|
result := make(TargetEdgePath, 0, len(*p))
|
|
|
|
for _, e := range *p {
|
|
|
|
result = append(result, e)
|
|
|
|
}
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2021-10-26 01:21:00 +02:00
|
|
|
// String returns a string representation of the path: [n1 -> n2 -> ... -> nn].
|
|
|
|
func (p *TargetEdgePath) String() string {
|
|
|
|
if p == nil {
|
|
|
|
return "nil"
|
|
|
|
}
|
|
|
|
if len(*p) == 0 {
|
|
|
|
return "[]"
|
|
|
|
}
|
|
|
|
var sb strings.Builder
|
|
|
|
fmt.Fprintf(&sb, "[")
|
2022-01-10 22:50:57 +01:00
|
|
|
for _, s := range *p {
|
|
|
|
fmt.Fprintf(&sb, "%s -> ", s.edge.target.name)
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
lastSegment := (*p)[len(*p)-1]
|
|
|
|
fmt.Fprintf(&sb, "%s]", lastSegment.edge.dependency.name)
|
2021-10-26 01:21:00 +02:00
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TargetNode describes a module or target identified by the name of a specific
|
|
|
|
// metadata file. (immutable)
|
|
|
|
//
|
|
|
|
// Each metadata file corresponds to a Soong module or to a Make target.
|
|
|
|
//
|
|
|
|
// A target node can appear as the target or as the dependency in edges.
|
|
|
|
// Most target nodes appear as both target in one edge and as dependency in
|
|
|
|
// other edges.
|
|
|
|
type TargetNode targetNode
|
|
|
|
|
|
|
|
// Name returns the string that identifies the target node.
|
|
|
|
// i.e. path to license metadata file
|
|
|
|
func (tn *TargetNode) Name() string {
|
|
|
|
return tn.name
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// Dependencies returns the list of edges to dependencies of `tn`.
|
|
|
|
func (tn *TargetNode) Dependencies() TargetEdgeList {
|
|
|
|
edges := make(TargetEdgeList, 0, len(tn.edges))
|
|
|
|
edges = append(edges, tn.edges...)
|
|
|
|
return edges
|
|
|
|
}
|
|
|
|
|
2021-10-26 01:21:00 +02:00
|
|
|
// PackageName returns the string that identifes the package for the target.
|
|
|
|
func (tn *TargetNode) PackageName() string {
|
|
|
|
return tn.proto.GetPackageName()
|
|
|
|
}
|
|
|
|
|
2022-10-22 03:28:05 +02:00
|
|
|
// ModuleName returns the module name of the target.
|
|
|
|
func (tn *TargetNode) ModuleName() string {
|
|
|
|
return tn.proto.GetModuleName()
|
|
|
|
}
|
|
|
|
|
2021-10-26 01:21:00 +02:00
|
|
|
// Projects returns the projects defining the target node. (unordered)
|
|
|
|
//
|
|
|
|
// In an ideal world, only 1 project defines a target, but the interaction
|
|
|
|
// between Soong and Make for a variety of architectures and for host versus
|
|
|
|
// product means a module is sometimes defined more than once.
|
|
|
|
func (tn *TargetNode) Projects() []string {
|
|
|
|
return append([]string{}, tn.proto.Projects...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LicenseConditions returns a copy of the set of license conditions
|
|
|
|
// originating at the target. The values that appear and how each is resolved
|
|
|
|
// is a matter of policy. (unordered)
|
|
|
|
//
|
|
|
|
// e.g. notice or proprietary
|
2022-01-10 22:50:57 +01:00
|
|
|
func (tn *TargetNode) LicenseConditions() LicenseConditionSet {
|
|
|
|
return tn.licenseConditions
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LicenseTexts returns the paths to the files containing the license texts for
|
|
|
|
// the target. (unordered)
|
|
|
|
func (tn *TargetNode) LicenseTexts() []string {
|
|
|
|
return append([]string{}, tn.proto.LicenseTexts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsContainer returns true if the target represents a container that merely
|
|
|
|
// aggregates other targets.
|
|
|
|
func (tn *TargetNode) IsContainer() bool {
|
|
|
|
return tn.proto.GetIsContainer()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Built returns the list of files built by the module or target. (unordered)
|
|
|
|
func (tn *TargetNode) Built() []string {
|
|
|
|
return append([]string{}, tn.proto.Built...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Installed returns the list of files installed by the module or target.
|
|
|
|
// (unordered)
|
|
|
|
func (tn *TargetNode) Installed() []string {
|
|
|
|
return append([]string{}, tn.proto.Installed...)
|
|
|
|
}
|
|
|
|
|
2021-12-10 07:10:43 +01:00
|
|
|
// TargetFiles returns the list of files built or installed by the module or
|
|
|
|
// target. (unordered)
|
|
|
|
func (tn *TargetNode) TargetFiles() []string {
|
|
|
|
return append(tn.proto.Built, tn.proto.Installed...)
|
|
|
|
}
|
|
|
|
|
2021-10-26 01:21:00 +02:00
|
|
|
// InstallMap returns the list of path name transformations to make to move
|
|
|
|
// files from their original location in the file system to their destination
|
|
|
|
// inside a container. (unordered)
|
|
|
|
func (tn *TargetNode) InstallMap() []InstallMap {
|
|
|
|
result := make([]InstallMap, 0, len(tn.proto.InstallMap))
|
|
|
|
for _, im := range tn.proto.InstallMap {
|
|
|
|
result = append(result, InstallMap{im.GetFromPath(), im.GetContainerPath()})
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sources returns the list of file names depended on by the target, which may
|
|
|
|
// be a proper subset of those made available by dependency modules.
|
|
|
|
// (unordered)
|
|
|
|
func (tn *TargetNode) Sources() []string {
|
|
|
|
return append([]string{}, tn.proto.Sources...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstallMap describes the mapping from an input filesystem file to file in a
|
|
|
|
// container.
|
|
|
|
type InstallMap struct {
|
|
|
|
// FromPath is the input path on the filesystem.
|
|
|
|
FromPath string
|
|
|
|
|
|
|
|
// ContainerPath is the path to the same file inside the container or
|
|
|
|
// installed location.
|
|
|
|
ContainerPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// TargetEdgeAnnotations describes an immutable set of annotations attached to
|
|
|
|
// an edge from a target to a dependency.
|
|
|
|
//
|
|
|
|
// Annotations typically distinguish between static linkage versus dynamic
|
|
|
|
// versus tools that are used at build time but are not linked in any way.
|
|
|
|
type TargetEdgeAnnotations struct {
|
2022-01-11 03:44:59 +01:00
|
|
|
annotations map[string]struct{}
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newEdgeAnnotations creates a new instance of TargetEdgeAnnotations.
|
|
|
|
func newEdgeAnnotations() TargetEdgeAnnotations {
|
2022-01-11 03:44:59 +01:00
|
|
|
return TargetEdgeAnnotations{make(map[string]struct{})}
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasAnnotation returns true if an annotation `ann` is in the set.
|
|
|
|
func (ea TargetEdgeAnnotations) HasAnnotation(ann string) bool {
|
|
|
|
_, ok := ea.annotations[ann]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare orders TargetAnnotations returning:
|
|
|
|
// -1 when ea < other,
|
|
|
|
// +1 when ea > other, and
|
|
|
|
// 0 when ea == other.
|
|
|
|
func (ea TargetEdgeAnnotations) Compare(other TargetEdgeAnnotations) int {
|
|
|
|
a1 := ea.AsList()
|
|
|
|
a2 := other.AsList()
|
|
|
|
sort.Strings(a1)
|
|
|
|
sort.Strings(a2)
|
|
|
|
for k := 0; k < len(a1) && k < len(a2); k++ {
|
|
|
|
if a1[k] < a2[k] {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if a1[k] > a2[k] {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(a1) < len(a2) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if len(a1) > len(a2) {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsList returns the list of annotation names attached to the edge.
|
|
|
|
// (unordered)
|
|
|
|
func (ea TargetEdgeAnnotations) AsList() []string {
|
|
|
|
l := make([]string, 0, len(ea.annotations))
|
|
|
|
for ann := range ea.annotations {
|
|
|
|
l = append(l, ann)
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// TargetNodeSet describes a set of distinct nodes in a license graph.
|
2022-12-18 23:15:02 +01:00
|
|
|
type TargetNodeSet map[*TargetNode]struct{}
|
2021-10-26 01:21:00 +02:00
|
|
|
|
|
|
|
// Contains returns true when `target` is an element of the set.
|
2022-12-18 23:15:02 +01:00
|
|
|
func (ts TargetNodeSet) Contains(target *TargetNode) bool {
|
|
|
|
_, isPresent := ts[target]
|
2021-10-26 01:21:00 +02:00
|
|
|
return isPresent
|
|
|
|
}
|
|
|
|
|
|
|
|
// Names returns the array of target node namess in the set. (unordered)
|
2022-12-18 23:15:02 +01:00
|
|
|
func (ts TargetNodeSet) Names() []string {
|
|
|
|
result := make([]string, 0, len(ts))
|
|
|
|
for tn := range ts {
|
2021-10-26 01:21:00 +02:00
|
|
|
result = append(result, tn.name)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// String returns a human-readable string representation of the set.
|
2022-12-18 23:15:02 +01:00
|
|
|
func (ts TargetNodeSet) String() string {
|
2022-01-10 22:50:57 +01:00
|
|
|
return fmt.Sprintf("{%s}", strings.Join(ts.Names(), ", "))
|
|
|
|
}
|
|
|
|
|
2021-10-26 01:21:00 +02:00
|
|
|
// TargetNodeList orders a list of targets by name.
|
|
|
|
type TargetNodeList []*TargetNode
|
|
|
|
|
|
|
|
// Len returns the count of elements in the list.
|
2022-01-28 00:18:52 +01:00
|
|
|
func (l TargetNodeList) Len() int { return len(l) }
|
2021-10-26 01:21:00 +02:00
|
|
|
|
|
|
|
// Swap rearranges 2 elements so that each occupies the other's former position.
|
|
|
|
func (l TargetNodeList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
|
|
|
|
|
|
|
|
// Less returns true when the `i`th element is lexicographicallt less than the `j`th.
|
|
|
|
func (l TargetNodeList) Less(i, j int) bool {
|
|
|
|
return l[i].name < l[j].name
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string representation of the list.
|
|
|
|
func (l TargetNodeList) String() string {
|
|
|
|
var sb strings.Builder
|
|
|
|
fmt.Fprintf(&sb, "[")
|
|
|
|
sep := ""
|
|
|
|
for _, tn := range l {
|
|
|
|
fmt.Fprintf(&sb, "%s%s", sep, tn.name)
|
|
|
|
sep = " "
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&sb, "]")
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Names returns an array the names of the nodes in the same order as the nodes in the list.
|
|
|
|
func (l TargetNodeList) Names() []string {
|
|
|
|
result := make([]string, 0, len(l))
|
|
|
|
for _, tn := range l {
|
|
|
|
result = append(result, tn.name)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|