Skip to content

Commit c35ff93

Browse files
committed
do not expose license maps
also corrects formatting issues and puts public functions at top of generated files so they are easier to find
1 parent 609007d commit c35ff93

8 files changed

Lines changed: 962 additions & 859 deletions

File tree

cmd/exceptions.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6+
"go/format"
67
"os"
78
"strings"
89
)
@@ -55,6 +56,27 @@ func extractExceptionLicenseIDs() error {
5556
// Code generated by go-spdx cmd/exceptions.go. DO NOT EDIT.
5657
// Source: https://github.com/spdx/license-list-data specifies official SPDX license list.
5758
59+
import "strings"
60+
61+
// IsException does a case-insensitive lookup for the exception id in the exceptions map.
62+
// It returns true and the case-sensitive ID if found, otherwise false and the original id.
63+
func IsException(id string) (bool, string) {
64+
foundID, ok := exceptionsMap[strings.ToUpper(id)]
65+
if ok {
66+
return true, foundID
67+
}
68+
return false, id
69+
}
70+
71+
// GetExceptionsMap returns a map of exception license IDs keyed by uppercase ID.
72+
func GetExceptionsMap() map[string]string {
73+
copied := make(map[string]string, len(exceptionsMap))
74+
for k, v := range exceptionsMap {
75+
copied[k] = v
76+
}
77+
return copied
78+
}
79+
5880
// GetExceptions returns a slice of exception license IDs.
5981
func GetExceptions() []string {
6082
return []string{
@@ -75,13 +97,13 @@ func GetExceptions() []string {
7597
`...)
7698
}
7799
getExceptionsContents = append(getExceptionsContents, `}
78-
79-
// GetExceptionsMap returns a map of exception license IDs keyed by uppercase ID.
80-
func GetExceptionsMap() map[string]string {
81-
return exceptionsMap
82-
}
83100
`...)
84101

102+
getExceptionsContents, err = format.Source(getExceptionsContents)
103+
if err != nil {
104+
return fmt.Errorf("format generated get_exceptions.go: %w", err)
105+
}
106+
85107
err = os.WriteFile("../spdxexp/spdxlicenses/get_exceptions.go", getExceptionsContents, 0600)
86108
if err != nil {
87109
return err

cmd/license.go

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6+
"go/format"
67
"os"
78
"strings"
89
)
@@ -60,6 +61,27 @@ func extractLicenseIDs() error {
6061
// Code generated by go-spdx cmd/license.go. DO NOT EDIT.
6162
// Source: https://github.com/spdx/license-list-data specifies official SPDX license list.
6263
64+
import "strings"
65+
66+
// IsActiveLicense does a case-insensitive lookup for the license id in the active licenses map.
67+
// It returns true and the case-sensitive ID if found, otherwise false and the original id.
68+
func IsActiveLicense(id string) (bool, string) {
69+
foundID, ok := licensesMap[strings.ToUpper(id)]
70+
if ok {
71+
return true, foundID
72+
}
73+
return false, id
74+
}
75+
76+
// GetLicensesMap returns a map of active license IDs keyed by uppercase ID.
77+
func GetLicensesMap() map[string]string {
78+
copied := make(map[string]string, len(licensesMap))
79+
for k, v := range licensesMap {
80+
copied[k] = v
81+
}
82+
return copied
83+
}
84+
6385
// GetLicenses returns a slice of active license IDs.
6486
func GetLicenses() []string {
6587
return []string{
@@ -80,13 +102,13 @@ func GetLicenses() []string {
80102
`...)
81103
}
82104
getLicensesContents = append(getLicensesContents, `}
83-
84-
// GetLicensesMap returns a map of active license IDs keyed by uppercase ID.
85-
func GetLicensesMap() map[string]string {
86-
return licensesMap
87-
}
88105
`...)
89106

107+
getLicensesContents, err = format.Source(getLicensesContents)
108+
if err != nil {
109+
return fmt.Errorf("format generated get_licenses.go: %w", err)
110+
}
111+
90112
err = os.WriteFile("../spdxexp/spdxlicenses/get_licenses.go", getLicensesContents, 0600)
91113
if err != nil {
92114
return err
@@ -99,6 +121,27 @@ func GetLicensesMap() map[string]string {
99121
// Code generated by go-spdx cmd/license.go. DO NOT EDIT.
100122
// Source: https://github.com/spdx/license-list-data specifies official SPDX license list.
101123
124+
import "strings"
125+
126+
// IsDeprecatedLicense does a case-insensitive lookup for the license id in the deprecated licenses map.
127+
// It returns true and the case-sensitive ID if found, otherwise false and the original id.
128+
func IsDeprecatedLicense(id string) (bool, string) {
129+
foundID, ok := deprecatedMap[strings.ToUpper(id)]
130+
if ok {
131+
return true, foundID
132+
}
133+
return false, id
134+
}
135+
136+
// GetDeprecatedMap returns a map of deprecated license IDs keyed by uppercase ID.
137+
func GetDeprecatedMap() map[string]string {
138+
copied := make(map[string]string, len(deprecatedMap))
139+
for k, v := range deprecatedMap {
140+
copied[k] = v
141+
}
142+
return copied
143+
}
144+
102145
// GetDeprecated returns a slice of deprecated license IDs.
103146
func GetDeprecated() []string {
104147
return []string{
@@ -119,13 +162,13 @@ func GetDeprecated() []string {
119162
`...)
120163
}
121164
getDeprecatedContents = append(getDeprecatedContents, `}
122-
123-
// GetDeprecatedMap returns a map of deprecated license IDs keyed by uppercase ID.
124-
func GetDeprecatedMap() map[string]string {
125-
return deprecatedMap
126-
}
127165
`...)
128166

167+
getDeprecatedContents, err = format.Source(getDeprecatedContents)
168+
if err != nil {
169+
return fmt.Errorf("format generated get_deprecated.go: %w", err)
170+
}
171+
129172
err = os.WriteFile("../spdxexp/spdxlicenses/get_deprecated.go", getDeprecatedContents, 0600)
130173
if err != nil {
131174
return err

spdxexp/license.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// activeLicense returns true if the id is an active license.
1010
func activeLicense(id string) (bool, string) {
11-
return inLicenseList(spdxlicenses.GetLicensesMap(), id)
11+
return spdxlicenses.IsActiveLicense(id)
1212
}
1313

1414
// ActiveLicense returns true if the id is an active license.
@@ -18,21 +18,12 @@ func ActiveLicense(id string) (bool, string) {
1818

1919
// deprecatedLicense returns true if the id is a deprecated license.
2020
func deprecatedLicense(id string) (bool, string) {
21-
return inLicenseList(spdxlicenses.GetDeprecatedMap(), id)
21+
return spdxlicenses.IsDeprecatedLicense(id)
2222
}
2323

2424
// exceptionLicense returns true if the id is an exception license.
2525
func exceptionLicense(id string) (bool, string) {
26-
return inLicenseList(spdxlicenses.GetExceptionsMap(), id)
27-
}
28-
29-
// inLicenseList looks for id in the list of licenses. The check is case-insensitive (e.g. "mit" will match "MIT").
30-
func inLicenseList(licenses map[string]string, id string) (bool, string) {
31-
foundID, ok := licenses[strings.ToUpper(id)]
32-
if ok {
33-
return true, foundID
34-
}
35-
return false, id
26+
return spdxlicenses.IsException(id)
3627
}
3728

3829
const (

spdxexp/satisfies.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func ValidateLicensesWithOptions(licenses []string, options ValidateLicensesOpti
9393
if !options.FailDeprecatedLicenses {
9494
if ok, _ := deprecatedLicense(licensePart); ok {
9595
continue
96-
}
96+
}
9797
}
9898
}
9999
valid = false
@@ -245,7 +245,6 @@ func isLicenseWithException(testExpression string) (bool, string, string) {
245245
return false, "", ""
246246
}
247247

248-
249248
// isCompatible checks if expressionPart is compatible with allowed list.
250249
// Expression part is an array of licenses that are ANDed together.
251250
// Allowed is an array of licenses that can fulfill the expression.

spdxexp/satisfies_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ func TestValidateLicensesWithOptions_FailDeprecatedLicenses(t *testing.T) {
115115
},
116116
},
117117
{
118-
name: "WITH exception rejects deprecated license if FailDeprecatedLicenses is true",
119-
inputLicenses: []string{deprecatedLicense + " WITH Bison-exception-2.2"},
120-
options: ValidateLicensesOptions{FailDeprecatedLicenses: true},
121-
allValid: false,
118+
name: "WITH exception rejects deprecated license if FailDeprecatedLicenses is true",
119+
inputLicenses: []string{deprecatedLicense + " WITH Bison-exception-2.2"},
120+
options: ValidateLicensesOptions{FailDeprecatedLicenses: true},
121+
allValid: false,
122122
invalidLicenses: []string{
123123
deprecatedLicense + " WITH Bison-exception-2.2",
124124
},
@@ -250,7 +250,7 @@ func TestSatisfies_FastPathValidation(t *testing.T) {
250250
allowedList []string
251251
satisfied bool
252252
expectErr bool
253-
expectedErr string
253+
expectedErr string
254254
}{
255255
{
256256
name: "MIT trims whitespace",

spdxexp/spdxlicenses/get_deprecated.go

Lines changed: 53 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)