blob: e6d23ef3e520ff73dfa7bede308a99dd5fdef9a1 [file] [log] [blame]
Bob Badoura99ac622021-10-25 16:21:00 -07001// 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
15package compliance
16
17import (
18 "fmt"
19 "strings"
20)
21
22// LicenseCondition describes an individual license condition or requirement
23// originating at a specific target node. (immutable)
24//
25// e.g. A module licensed under GPL terms would originate a `restricted` condition.
26type LicenseCondition struct {
27 name string
28 origin *TargetNode
29}
30
31// Name returns the name of the condition. e.g. "restricted" or "notice"
32func (lc LicenseCondition) Name() string {
33 return lc.name
34}
35
36// Origin identifies the TargetNode where the condition originates.
37func (lc LicenseCondition) Origin() *TargetNode {
38 return lc.origin
39}
40
41// asString returns a string representation of a license condition:
42// origin+separator+condition.
43func (lc LicenseCondition) asString(separator string) string {
44 return lc.origin.name + separator + lc.name
45}
46
47// ConditionList implements introspection methods to arrays of LicenseCondition.
48type ConditionList []LicenseCondition
49
50
51// ConditionList orders arrays of LicenseCondition by Origin and Name.
52
53// Len returns the length of the list.
54func (l ConditionList) Len() int { return len(l) }
55
56// Swap rearranges 2 elements in the list so each occupies the other's former position.
57func (l ConditionList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
58
59// Less returns true when the `i`th element is lexicographically less than tht `j`th element.
60func (l ConditionList) Less(i, j int) bool {
61 if l[i].origin.name == l[j].origin.name {
62 return l[i].name < l[j].name
63 }
64 return l[i].origin.name < l[j].origin.name
65}
66
67// String returns a string representation of the set.
68func (cl ConditionList) String() string {
69 var sb strings.Builder
70 fmt.Fprintf(&sb, "[")
71 sep := ""
72 for _, lc := range cl {
73 fmt.Fprintf(&sb, "%s%s:%s", sep, lc.origin.name, lc.name)
74 sep = ", "
75 }
76 fmt.Fprintf(&sb, "]")
77 return sb.String()
78}
79
80// HasByName returns true if the list contains any condition matching `name`.
81func (cl ConditionList) HasByName(name ConditionNames) bool {
82 for _, lc := range cl {
83 if name.Contains(lc.name) {
84 return true
85 }
86 }
87 return false
88}
89
90// ByName returns the sublist of conditions that match `name`.
91func (cl ConditionList) ByName(name ConditionNames) ConditionList {
92 result := make(ConditionList, 0, cl.CountByName(name))
93 for _, lc := range cl {
94 if name.Contains(lc.name) {
95 result = append(result, lc)
96 }
97 }
98 return result
99}
100
101// CountByName returns the size of the sublist of conditions that match `name`.
102func (cl ConditionList) CountByName(name ConditionNames) int {
103 size := 0
104 for _, lc := range cl {
105 if name.Contains(lc.name) {
106 size++
107 }
108 }
109 return size
110}
111
112// HasByOrigin returns true if the list contains any condition originating at `origin`.
113func (cl ConditionList) HasByOrigin(origin *TargetNode) bool {
114 for _, lc := range cl {
115 if lc.origin.name == origin.name {
116 return true
117 }
118 }
119 return false
120}
121
122// ByOrigin returns the sublist of conditions that originate at `origin`.
123func (cl ConditionList) ByOrigin(origin *TargetNode) ConditionList {
124 result := make(ConditionList, 0, cl.CountByOrigin(origin))
125 for _, lc := range cl {
126 if lc.origin.name == origin.name {
127 result = append(result, lc)
128 }
129 }
130 return result
131}
132
133// CountByOrigin returns the size of the sublist of conditions that originate at `origin`.
134func (cl ConditionList) CountByOrigin(origin *TargetNode) int {
135 size := 0
136 for _, lc := range cl {
137 if lc.origin.name == origin.name {
138 size++
139 }
140 }
141 return size
142}
143
144// ConditionNames implements the Contains predicate for slices of condition
145// name strings.
146type ConditionNames []string
147
148// Contains returns true if the name matches one of the ConditionNames.
149func (cn ConditionNames) Contains(name string) bool {
150 for _, cname := range cn {
151 if cname == name {
152 return true
153 }
154 }
155 return false
156}