зеркало из https://github.com/github/go-spdx.git
feat: add method for extracting licenses from expression
This commit is contained in:
Родитель
304897a7aa
Коммит
df5d42ebe2
|
@ -0,0 +1,24 @@
|
|||
package spdxexp
|
||||
|
||||
// flatten will take an array of nested array and return
|
||||
// all nested elements in an array. e.g. [[1,2,[3]],4] -> [1,2,3,4]
|
||||
func flatten[T any](lists [][]T) []T {
|
||||
var res []T
|
||||
for _, list := range lists {
|
||||
res = append(res, list...)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// removeDuplicates will remove all duplicates from a slice
|
||||
func removeDuplicates[T string](sliceList []T) []T {
|
||||
allKeys := make(map[T]bool)
|
||||
list := []T{}
|
||||
for _, item := range sliceList {
|
||||
if _, value := allKeys[item]; !value {
|
||||
allKeys[item] = true
|
||||
list = append(list, item)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
|
@ -50,6 +50,25 @@ func Satisfies(testExpression string, allowedList []string) (bool, error) {
|
|||
return false, nil
|
||||
}
|
||||
|
||||
// ExtractLicenses extracts licenses from the given expression without duplicates.
|
||||
func ExtractLicenses(expression string) ([]string, error) {
|
||||
node, err := parse(expression)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
expanded := node.expand(true)
|
||||
licenses := make([]string, 0)
|
||||
allLicenses := flatten(expanded)
|
||||
for _, license := range allLicenses {
|
||||
licenses = append(licenses, license.lic.license)
|
||||
}
|
||||
|
||||
licenses = removeDuplicates(licenses)
|
||||
|
||||
return licenses, nil
|
||||
}
|
||||
|
||||
// stringsToNodes converts an array of single license strings to to an array of license nodes.
|
||||
func stringsToNodes(licenseStrings []string) ([]*node, error) {
|
||||
nodes := make([]*node, len(licenseStrings))
|
||||
|
|
|
@ -37,6 +37,28 @@ func TestValidateLicenses(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestExtractLicenses(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
inputExpression string
|
||||
extractedLicenses []string
|
||||
}{
|
||||
{"Single license", "MIT", []string{"MIT"}},
|
||||
{"AND'ed licenses", "MIT AND Apache-2.0", []string{"MIT", "Apache-2.0"}},
|
||||
{"AND'ed & OR'ed licenses", "(MIT AND Apache-2.0) OR GPL-3.0", []string{"GPL-3.0", "MIT", "Apache-2.0"}},
|
||||
{"ONLY modifiers", "LGPL-2.1-only OR MIT OR BSD-3-Clause", []string{"MIT", "BSD-3-Clause", "LGPL-2.1-only"}},
|
||||
{"WITH modifiers", "GPL-2.0-or-later WITH Bison-exception-2.2", []string{"GPL-2.0-or-later"}},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
licenses, err := ExtractLicenses(test.inputExpression)
|
||||
assert.ElementsMatch(t, test.extractedLicenses, licenses)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestSatisfiesSingle lets you quickly test a single call to Satisfies with a specific license expression and allowed list of licenses.
|
||||
// To test a different expression, change the expression, allowed licenses, and expected result in the function body.
|
||||
// TO RUN: go test ./expression -run TestSatisfiesSingle
|
||||
|
|
Загрузка…
Ссылка в новой задаче