Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 1 | // Copyright 2021 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package compliance |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "sort" |
| 20 | "strings" |
| 21 | "sync" |
| 22 | ) |
| 23 | |
| 24 | // LicenseGraph describes the immutable license metadata for a set of root |
| 25 | // targets and the transitive closure of their dependencies. |
| 26 | // |
| 27 | // Alternatively, a graph is a set of edges. In this case directed, annotated |
| 28 | // edges from targets to dependencies. |
| 29 | // |
| 30 | // A LicenseGraph provides the frame of reference for all of the other types |
| 31 | // defined here. It is possible to have multiple graphs, and to have targets, |
| 32 | // edges, and resolutions from multiple graphs. But it is an error to try to |
| 33 | // mix items from different graphs in the same operation. |
| 34 | // May panic if attempted. |
| 35 | // |
| 36 | // The compliance package assumes specific private implementations of each of |
| 37 | // these interfaces. May panic if attempts are made to combine different |
| 38 | // implementations of some interfaces with expected implementations of other |
| 39 | // interfaces here. |
| 40 | type LicenseGraph struct { |
| 41 | // rootFiles identifies the original set of files to read. (immutable) |
| 42 | // |
| 43 | // Defines the starting "top" for top-down walks. |
| 44 | // |
| 45 | // Alternatively, an instance of licenseGraphImp conceptually defines a scope within |
| 46 | // the universe of build graphs as a sub-graph rooted at rootFiles where all edges |
| 47 | // and targets for the instance are defined relative to and within that scope. For |
| 48 | // most analyses, the correct scope is to root the graph at all of the distributed |
| 49 | // artifacts. |
| 50 | rootFiles []string |
| 51 | |
| 52 | // edges lists the directed edges in the graph from target to dependency. (guarded by mu) |
| 53 | // |
| 54 | // Alternatively, the graph is the set of `edges`. |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 55 | edges TargetEdgeList |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 56 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 57 | // targets identifies, indexes, and describes the entire set of target node files. |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 58 | /// (guarded by mu) |
| 59 | targets map[string]*TargetNode |
| 60 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 61 | // wgBU becomes non-nil when the bottom-up resolve begins and reaches 0 |
| 62 | // (i.e. Wait() proceeds) when the bottom-up resolve completes. (guarded by mu) |
| 63 | wgBU *sync.WaitGroup |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 64 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 65 | // wgTD becomes non-nil when the top-down resolve begins and reaches 0 (i.e. Wait() |
| 66 | // proceeds) when the top-down resolve completes. (guarded by mu) |
| 67 | wgTD *sync.WaitGroup |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 68 | |
| 69 | // shippedNodes caches the results of a full walk of nodes identifying targets |
| 70 | // distributed either directly or as derivative works. (creation guarded by mu) |
| 71 | shippedNodes *TargetNodeSet |
| 72 | |
| 73 | // mu guards against concurrent update. |
| 74 | mu sync.Mutex |
| 75 | } |
| 76 | |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 77 | // Edges returns the list of edges in the graph. (unordered) |
| 78 | func (lg *LicenseGraph) Edges() TargetEdgeList { |
| 79 | edges := make(TargetEdgeList, 0, len(lg.edges)) |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 80 | edges = append(edges, lg.edges...) |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 81 | return edges |
| 82 | } |
| 83 | |
| 84 | // Targets returns the list of target nodes in the graph. (unordered) |
| 85 | func (lg *LicenseGraph) Targets() TargetNodeList { |
| 86 | targets := make(TargetNodeList, 0, len(lg.targets)) |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 87 | for _, target := range lg.targets { |
| 88 | targets = append(targets, target) |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 89 | } |
| 90 | return targets |
| 91 | } |
| 92 | |
| 93 | // compliance-only LicenseGraph methods |
| 94 | |
| 95 | // newLicenseGraph constructs a new, empty instance of LicenseGraph. |
| 96 | func newLicenseGraph() *LicenseGraph { |
| 97 | return &LicenseGraph{ |
| 98 | rootFiles: []string{}, |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 99 | targets: make(map[string]*TargetNode), |
| 100 | } |
| 101 | } |
| 102 | |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 103 | // TargetEdge describes a directed, annotated edge from a target to a |
| 104 | // dependency. (immutable) |
| 105 | // |
| 106 | // A LicenseGraph, above, is a set of TargetEdges. |
| 107 | // |
| 108 | // i.e. `Target` depends on `Dependency` in the manner described by |
| 109 | // `Annotations`. |
| 110 | type TargetEdge struct { |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 111 | // target and dependency identify the nodes connected by the edge. |
| 112 | target, dependency *TargetNode |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 113 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 114 | // annotations identifies the set of compliance-relevant annotations describing the edge. |
| 115 | annotations TargetEdgeAnnotations |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Target identifies the target that depends on the dependency. |
| 119 | // |
| 120 | // Target needs Dependency to build. |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 121 | func (e *TargetEdge) Target() *TargetNode { |
| 122 | return e.target |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // Dependency identifies the target depended on by the target. |
| 126 | // |
| 127 | // Dependency builds without Target, but Target needs Dependency to build. |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 128 | func (e *TargetEdge) Dependency() *TargetNode { |
| 129 | return e.dependency |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // Annotations describes the type of edge by the set of annotations attached to |
| 133 | // it. |
| 134 | // |
| 135 | // Only annotations prescribed by policy have any meaning for licensing, and |
| 136 | // the meaning for licensing is likewise prescribed by policy. Other annotations |
| 137 | // are preserved and ignored by policy. |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 138 | func (e *TargetEdge) Annotations() TargetEdgeAnnotations { |
| 139 | return e.annotations |
| 140 | } |
| 141 | |
| 142 | // String returns a human-readable string representation of the edge. |
| 143 | func (e *TargetEdge) String() string { |
| 144 | return fmt.Sprintf("%s -[%s]> %s", e.target.name, strings.Join(e.annotations.AsList(), ", "), e.dependency.name) |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // TargetEdgeList orders lists of edges by target then dependency then annotations. |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 148 | type TargetEdgeList []*TargetEdge |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 149 | |
| 150 | // Len returns the count of the elmements in the list. |
| 151 | func (l TargetEdgeList) Len() int { return len(l) } |
| 152 | |
| 153 | // Swap rearranges 2 elements so that each occupies the other's former position. |
| 154 | func (l TargetEdgeList) Swap(i, j int) { l[i], l[j] = l[j], l[i] } |
| 155 | |
| 156 | // Less returns true when the `i`th element is lexicographically less than the `j`th. |
| 157 | func (l TargetEdgeList) Less(i, j int) bool { |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 158 | namei := l[i].target.name |
| 159 | namej := l[j].target.name |
| 160 | if namei == namej { |
| 161 | namei = l[i].dependency.name |
| 162 | namej = l[j].dependency.name |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 163 | } |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 164 | if namei == namej { |
| 165 | return l[i].annotations.Compare(l[j].annotations) < 0 |
| 166 | } |
| 167 | return namei < namej |
| 168 | } |
| 169 | |
| 170 | // TargetEdgePathSegment describes a single arc in a TargetPath associating the |
| 171 | // edge with a context `ctx` defined by whatever process is creating the path. |
| 172 | type TargetEdgePathSegment struct { |
| 173 | edge *TargetEdge |
| 174 | ctx interface{} |
| 175 | } |
| 176 | |
| 177 | // Target identifies the target that depends on the dependency. |
| 178 | // |
| 179 | // Target needs Dependency to build. |
| 180 | func (s TargetEdgePathSegment) Target() *TargetNode { |
| 181 | return s.edge.target |
| 182 | } |
| 183 | |
| 184 | // Dependency identifies the target depended on by the target. |
| 185 | // |
| 186 | // Dependency builds without Target, but Target needs Dependency to build. |
| 187 | func (s TargetEdgePathSegment) Dependency() *TargetNode { |
| 188 | return s.edge.dependency |
| 189 | } |
| 190 | |
| 191 | // Annotations describes the type of edge by the set of annotations attached to |
| 192 | // it. |
| 193 | // |
| 194 | // Only annotations prescribed by policy have any meaning for licensing, and |
| 195 | // the meaning for licensing is likewise prescribed by policy. Other annotations |
| 196 | // are preserved and ignored by policy. |
| 197 | func (s TargetEdgePathSegment) Annotations() TargetEdgeAnnotations { |
| 198 | return s.edge.annotations |
| 199 | } |
| 200 | |
| 201 | // Context returns the context associated with the path segment. The type and |
| 202 | // value of the context defined by the process creating the path. |
| 203 | func (s TargetEdgePathSegment) Context() interface{} { |
| 204 | return s.ctx |
| 205 | } |
| 206 | |
| 207 | // String returns a human-readable string representation of the edge. |
| 208 | func (s TargetEdgePathSegment) String() string { |
| 209 | return fmt.Sprintf("%s -[%s]> %s", s.edge.target.name, strings.Join(s.edge.annotations.AsList(), ", "), s.edge.dependency.name) |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // TargetEdgePath describes a sequence of edges starting at a root and ending |
| 213 | // at some final dependency. |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 214 | type TargetEdgePath []TargetEdgePathSegment |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 215 | |
| 216 | // NewTargetEdgePath creates a new, empty path with capacity `cap`. |
| 217 | func NewTargetEdgePath(cap int) *TargetEdgePath { |
| 218 | p := make(TargetEdgePath, 0, cap) |
| 219 | return &p |
| 220 | } |
| 221 | |
| 222 | // Push appends a new edge to the list verifying that the target of the new |
| 223 | // edge is the dependency of the prior. |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 224 | func (p *TargetEdgePath) Push(edge *TargetEdge, ctx interface{}) { |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 225 | if len(*p) == 0 { |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 226 | *p = append(*p, TargetEdgePathSegment{edge, ctx}) |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 227 | return |
| 228 | } |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 229 | if (*p)[len(*p)-1].edge.dependency != edge.target { |
| 230 | panic(fmt.Errorf("disjoint path %s does not end at %s", p.String(), edge.target.name)) |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 231 | } |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 232 | *p = append(*p, TargetEdgePathSegment{edge, ctx}) |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // Pop shortens the path by 1 edge. |
| 236 | func (p *TargetEdgePath) Pop() { |
| 237 | if len(*p) == 0 { |
| 238 | panic(fmt.Errorf("attempt to remove edge from empty path")) |
| 239 | } |
| 240 | *p = (*p)[:len(*p)-1] |
| 241 | } |
| 242 | |
| 243 | // Clear makes the path length 0. |
| 244 | func (p *TargetEdgePath) Clear() { |
| 245 | *p = (*p)[:0] |
| 246 | } |
| 247 | |
| 248 | // String returns a string representation of the path: [n1 -> n2 -> ... -> nn]. |
| 249 | func (p *TargetEdgePath) String() string { |
| 250 | if p == nil { |
| 251 | return "nil" |
| 252 | } |
| 253 | if len(*p) == 0 { |
| 254 | return "[]" |
| 255 | } |
| 256 | var sb strings.Builder |
| 257 | fmt.Fprintf(&sb, "[") |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 258 | for _, s := range *p { |
| 259 | fmt.Fprintf(&sb, "%s -> ", s.edge.target.name) |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 260 | } |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 261 | lastSegment := (*p)[len(*p)-1] |
| 262 | fmt.Fprintf(&sb, "%s]", lastSegment.edge.dependency.name) |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 263 | return sb.String() |
| 264 | } |
| 265 | |
| 266 | // TargetNode describes a module or target identified by the name of a specific |
| 267 | // metadata file. (immutable) |
| 268 | // |
| 269 | // Each metadata file corresponds to a Soong module or to a Make target. |
| 270 | // |
| 271 | // A target node can appear as the target or as the dependency in edges. |
| 272 | // Most target nodes appear as both target in one edge and as dependency in |
| 273 | // other edges. |
| 274 | type TargetNode targetNode |
| 275 | |
| 276 | // Name returns the string that identifies the target node. |
| 277 | // i.e. path to license metadata file |
| 278 | func (tn *TargetNode) Name() string { |
| 279 | return tn.name |
| 280 | } |
| 281 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 282 | // Dependencies returns the list of edges to dependencies of `tn`. |
| 283 | func (tn *TargetNode) Dependencies() TargetEdgeList { |
| 284 | edges := make(TargetEdgeList, 0, len(tn.edges)) |
| 285 | edges = append(edges, tn.edges...) |
| 286 | return edges |
| 287 | } |
| 288 | |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 289 | // PackageName returns the string that identifes the package for the target. |
| 290 | func (tn *TargetNode) PackageName() string { |
| 291 | return tn.proto.GetPackageName() |
| 292 | } |
| 293 | |
| 294 | // ModuleTypes returns the list of module types implementing the target. |
| 295 | // (unordered) |
| 296 | // |
| 297 | // In an ideal world, only 1 module type would implement each target, but the |
| 298 | // interactions between Soong and Make for host versus product and for a |
| 299 | // variety of architectures sometimes causes multiple module types per target |
| 300 | // (often a regular build target and a prebuilt.) |
| 301 | func (tn *TargetNode) ModuleTypes() []string { |
| 302 | return append([]string{}, tn.proto.ModuleTypes...) |
| 303 | } |
| 304 | |
| 305 | // ModuleClasses returns the list of module classes implementing the target. |
| 306 | // (unordered) |
| 307 | func (tn *TargetNode) ModuleClasses() []string { |
| 308 | return append([]string{}, tn.proto.ModuleClasses...) |
| 309 | } |
| 310 | |
| 311 | // Projects returns the projects defining the target node. (unordered) |
| 312 | // |
| 313 | // In an ideal world, only 1 project defines a target, but the interaction |
| 314 | // between Soong and Make for a variety of architectures and for host versus |
| 315 | // product means a module is sometimes defined more than once. |
| 316 | func (tn *TargetNode) Projects() []string { |
| 317 | return append([]string{}, tn.proto.Projects...) |
| 318 | } |
| 319 | |
| 320 | // LicenseKinds returns the list of license kind names for the module or |
| 321 | // target. (unordered) |
| 322 | // |
| 323 | // e.g. SPDX-license-identifier-MIT or legacy_proprietary |
| 324 | func (tn *TargetNode) LicenseKinds() []string { |
| 325 | return append([]string{}, tn.proto.LicenseKinds...) |
| 326 | } |
| 327 | |
| 328 | // LicenseConditions returns a copy of the set of license conditions |
| 329 | // originating at the target. The values that appear and how each is resolved |
| 330 | // is a matter of policy. (unordered) |
| 331 | // |
| 332 | // e.g. notice or proprietary |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 333 | func (tn *TargetNode) LicenseConditions() LicenseConditionSet { |
| 334 | return tn.licenseConditions |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | // LicenseTexts returns the paths to the files containing the license texts for |
| 338 | // the target. (unordered) |
| 339 | func (tn *TargetNode) LicenseTexts() []string { |
| 340 | return append([]string{}, tn.proto.LicenseTexts...) |
| 341 | } |
| 342 | |
| 343 | // IsContainer returns true if the target represents a container that merely |
| 344 | // aggregates other targets. |
| 345 | func (tn *TargetNode) IsContainer() bool { |
| 346 | return tn.proto.GetIsContainer() |
| 347 | } |
| 348 | |
| 349 | // Built returns the list of files built by the module or target. (unordered) |
| 350 | func (tn *TargetNode) Built() []string { |
| 351 | return append([]string{}, tn.proto.Built...) |
| 352 | } |
| 353 | |
| 354 | // Installed returns the list of files installed by the module or target. |
| 355 | // (unordered) |
| 356 | func (tn *TargetNode) Installed() []string { |
| 357 | return append([]string{}, tn.proto.Installed...) |
| 358 | } |
| 359 | |
| 360 | // InstallMap returns the list of path name transformations to make to move |
| 361 | // files from their original location in the file system to their destination |
| 362 | // inside a container. (unordered) |
| 363 | func (tn *TargetNode) InstallMap() []InstallMap { |
| 364 | result := make([]InstallMap, 0, len(tn.proto.InstallMap)) |
| 365 | for _, im := range tn.proto.InstallMap { |
| 366 | result = append(result, InstallMap{im.GetFromPath(), im.GetContainerPath()}) |
| 367 | } |
| 368 | return result |
| 369 | } |
| 370 | |
| 371 | // Sources returns the list of file names depended on by the target, which may |
| 372 | // be a proper subset of those made available by dependency modules. |
| 373 | // (unordered) |
| 374 | func (tn *TargetNode) Sources() []string { |
| 375 | return append([]string{}, tn.proto.Sources...) |
| 376 | } |
| 377 | |
| 378 | // InstallMap describes the mapping from an input filesystem file to file in a |
| 379 | // container. |
| 380 | type InstallMap struct { |
| 381 | // FromPath is the input path on the filesystem. |
| 382 | FromPath string |
| 383 | |
| 384 | // ContainerPath is the path to the same file inside the container or |
| 385 | // installed location. |
| 386 | ContainerPath string |
| 387 | } |
| 388 | |
| 389 | // TargetEdgeAnnotations describes an immutable set of annotations attached to |
| 390 | // an edge from a target to a dependency. |
| 391 | // |
| 392 | // Annotations typically distinguish between static linkage versus dynamic |
| 393 | // versus tools that are used at build time but are not linked in any way. |
| 394 | type TargetEdgeAnnotations struct { |
Bob Badour | 5446a6f | 2022-01-10 18:44:59 -0800 | [diff] [blame] | 395 | annotations map[string]struct{} |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | // newEdgeAnnotations creates a new instance of TargetEdgeAnnotations. |
| 399 | func newEdgeAnnotations() TargetEdgeAnnotations { |
Bob Badour | 5446a6f | 2022-01-10 18:44:59 -0800 | [diff] [blame] | 400 | return TargetEdgeAnnotations{make(map[string]struct{})} |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // HasAnnotation returns true if an annotation `ann` is in the set. |
| 404 | func (ea TargetEdgeAnnotations) HasAnnotation(ann string) bool { |
| 405 | _, ok := ea.annotations[ann] |
| 406 | return ok |
| 407 | } |
| 408 | |
| 409 | // Compare orders TargetAnnotations returning: |
| 410 | // -1 when ea < other, |
| 411 | // +1 when ea > other, and |
| 412 | // 0 when ea == other. |
| 413 | func (ea TargetEdgeAnnotations) Compare(other TargetEdgeAnnotations) int { |
| 414 | a1 := ea.AsList() |
| 415 | a2 := other.AsList() |
| 416 | sort.Strings(a1) |
| 417 | sort.Strings(a2) |
| 418 | for k := 0; k < len(a1) && k < len(a2); k++ { |
| 419 | if a1[k] < a2[k] { |
| 420 | return -1 |
| 421 | } |
| 422 | if a1[k] > a2[k] { |
| 423 | return 1 |
| 424 | } |
| 425 | } |
| 426 | if len(a1) < len(a2) { |
| 427 | return -1 |
| 428 | } |
| 429 | if len(a1) > len(a2) { |
| 430 | return 1 |
| 431 | } |
| 432 | return 0 |
| 433 | } |
| 434 | |
| 435 | // AsList returns the list of annotation names attached to the edge. |
| 436 | // (unordered) |
| 437 | func (ea TargetEdgeAnnotations) AsList() []string { |
| 438 | l := make([]string, 0, len(ea.annotations)) |
| 439 | for ann := range ea.annotations { |
| 440 | l = append(l, ann) |
| 441 | } |
| 442 | return l |
| 443 | } |
| 444 | |
| 445 | // TargetNodeSet describes a set of distinct nodes in a license graph. |
| 446 | type TargetNodeSet struct { |
Bob Badour | 5446a6f | 2022-01-10 18:44:59 -0800 | [diff] [blame] | 447 | nodes map[*TargetNode]struct{} |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // Contains returns true when `target` is an element of the set. |
| 451 | func (ts *TargetNodeSet) Contains(target *TargetNode) bool { |
| 452 | _, isPresent := ts.nodes[target] |
| 453 | return isPresent |
| 454 | } |
| 455 | |
| 456 | // AsList returns the list of target nodes in the set. (unordered) |
| 457 | func (ts *TargetNodeSet) AsList() TargetNodeList { |
| 458 | result := make(TargetNodeList, 0, len(ts.nodes)) |
| 459 | for tn := range ts.nodes { |
| 460 | result = append(result, tn) |
| 461 | } |
| 462 | return result |
| 463 | } |
| 464 | |
| 465 | // Names returns the array of target node namess in the set. (unordered) |
| 466 | func (ts *TargetNodeSet) Names() []string { |
| 467 | result := make([]string, 0, len(ts.nodes)) |
| 468 | for tn := range ts.nodes { |
| 469 | result = append(result, tn.name) |
| 470 | } |
| 471 | return result |
| 472 | } |
| 473 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame^] | 474 | // String returns a human-readable string representation of the set. |
| 475 | func (ts *TargetNodeSet) String() string { |
| 476 | return fmt.Sprintf("{%s}", strings.Join(ts.Names(), ", ")) |
| 477 | } |
| 478 | |
Bob Badour | a99ac62 | 2021-10-25 16:21:00 -0700 | [diff] [blame] | 479 | // TargetNodeList orders a list of targets by name. |
| 480 | type TargetNodeList []*TargetNode |
| 481 | |
| 482 | // Len returns the count of elements in the list. |
| 483 | func (l TargetNodeList) Len() int { return len(l) } |
| 484 | |
| 485 | // Swap rearranges 2 elements so that each occupies the other's former position. |
| 486 | func (l TargetNodeList) Swap(i, j int) { l[i], l[j] = l[j], l[i] } |
| 487 | |
| 488 | // Less returns true when the `i`th element is lexicographicallt less than the `j`th. |
| 489 | func (l TargetNodeList) Less(i, j int) bool { |
| 490 | return l[i].name < l[j].name |
| 491 | } |
| 492 | |
| 493 | // String returns a string representation of the list. |
| 494 | func (l TargetNodeList) String() string { |
| 495 | var sb strings.Builder |
| 496 | fmt.Fprintf(&sb, "[") |
| 497 | sep := "" |
| 498 | for _, tn := range l { |
| 499 | fmt.Fprintf(&sb, "%s%s", sep, tn.name) |
| 500 | sep = " " |
| 501 | } |
| 502 | fmt.Fprintf(&sb, "]") |
| 503 | return sb.String() |
| 504 | } |
| 505 | |
| 506 | // Names returns an array the names of the nodes in the same order as the nodes in the list. |
| 507 | func (l TargetNodeList) Names() []string { |
| 508 | result := make([]string, 0, len(l)) |
| 509 | for _, tn := range l { |
| 510 | result = append(result, tn.name) |
| 511 | } |
| 512 | return result |
| 513 | } |