1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
// 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"
)
// Resolution describes an action to resolve one or more license conditions.
//
// `AttachesTo` identifies the target node that when distributed triggers the action.
// `ActsOn` identifies the target node that is the object of the action.
// `Resolves` identifies one or more license conditions that the action resolves.
//
// e.g. Suppose an MIT library is linked to a binary that also links to GPL code.
//
// A resolution would attach to the binary to share (act on) the MIT library to
// resolve the restricted condition originating from the GPL code.
type Resolution struct {
attachesTo, actsOn *TargetNode
cs LicenseConditionSet
}
// AttachesTo returns the target node the resolution attaches to.
func (r Resolution) AttachesTo() *TargetNode {
return r.attachesTo
}
// ActsOn returns the target node that must be acted on to resolve the condition.
//
// i.e. The node for which notice must be given or whose source must be shared etc.
func (r Resolution) ActsOn() *TargetNode {
return r.actsOn
}
// Resolves returns the set of license condition the resolution satisfies.
func (r Resolution) Resolves() LicenseConditionSet {
return r.cs
}
// asString returns a string representation of the resolution.
func (r Resolution) asString() string {
var sb strings.Builder
names := r.cs.Names()
sort.Strings(names)
fmt.Fprintf(&sb, "%s -> %s{%s}", r.attachesTo.name, r.actsOn.name, strings.Join(names, ", "))
return sb.String()
}
// ResolutionList represents a partial order of Resolutions ordered by
// AttachesTo() and ActsOn() leaving `Resolves()` unordered.
type ResolutionList []Resolution
// Len returns the count of elements in the list.
func (l ResolutionList) Len() int { return len(l) }
// Swap rearranges 2 elements so that each occupies the other's former position.
func (l ResolutionList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
// Less returns true when the `i`th element is lexicographically less than tht `j`th.
func (l ResolutionList) Less(i, j int) bool {
if l[i].attachesTo.name == l[j].attachesTo.name {
return l[i].actsOn.name < l[j].actsOn.name
}
return l[i].attachesTo.name < l[j].attachesTo.name
}
// String returns a string representation of the list.
func (rl ResolutionList) String() string {
var sb strings.Builder
fmt.Fprintf(&sb, "[")
sep := ""
for _, r := range rl {
fmt.Fprintf(&sb, "%s%s", sep, r.asString())
sep = ", "
}
fmt.Fprintf(&sb, "]")
return sb.String()
}
// AllConditions returns the union of all license conditions resolved by any
// element of the list.
func (rl ResolutionList) AllConditions() LicenseConditionSet {
result := NewLicenseConditionSet()
for _, r := range rl {
result = result.Union(r.cs)
}
return result
}
// ByName returns the sub-list of resolutions resolving conditions matching
// `names`.
func (rl ResolutionList) Matching(conditions LicenseConditionSet) ResolutionList {
result := make(ResolutionList, 0, rl.CountMatching(conditions))
for _, r := range rl {
if r.Resolves().MatchesAnySet(conditions) {
result = append(result, Resolution{r.attachesTo, r.actsOn, r.cs.MatchingAnySet(conditions)})
}
}
return result
}
// CountMatching returns the number of resolutions resolving conditions matching
// `conditions`.
func (rl ResolutionList) CountMatching(conditions LicenseConditionSet) int {
c := 0
for _, r := range rl {
if r.Resolves().MatchesAnySet(conditions) {
c++
}
}
return c
}
// ByActsOn returns the sub-list of resolutions matching `actsOn`.
func (rl ResolutionList) ByActsOn(actsOn *TargetNode) ResolutionList {
result := make(ResolutionList, 0, rl.CountByActsOn(actsOn))
for _, r := range rl {
if r.actsOn == actsOn {
result = append(result, r)
}
}
return result
}
// CountByActsOn returns the number of resolutions matching `actsOn`.
func (rl ResolutionList) CountByActsOn(actsOn *TargetNode) int {
c := 0
for _, r := range rl {
if r.actsOn == actsOn {
c++
}
}
return c
}
|