Skip to content

Commit 0960e21

Browse files
fix: Update trimDocumentIdPrefix() and trimElementIdPrefix() to handle IDs without prefix
Signed-off-by: Souta Kawahara <souta.kawahara@almalinux.org>
1 parent 6cdc30a commit 0960e21

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

spdx/v2/common/identifier.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,9 @@ func prefixDocumentId(id DocumentID) string {
4747
return val
4848
}
4949

50-
// trimDocumentIdPrefix removes the DocumentRef- prefix from an document ID string or returns an error if it
51-
// does not start with DocumentRef-
50+
// trimDocumentIdPrefix removes the DocumentRef- prefix from an document ID string
5251
func trimDocumentIdPrefix(id string) (DocumentID, error) {
53-
// handle DocumentRef-
54-
if !strings.HasPrefix(id, documentRefPrefix) {
55-
return "", fmt.Errorf("failed to parse SPDX identifier '%s'", id)
56-
}
57-
e := DocumentID(strings.TrimPrefix(id, documentRefPrefix))
58-
return e, nil
52+
return DocumentID(strings.TrimPrefix(id, documentRefPrefix)), nil
5953
}
6054

6155
// ElementID represents the identifier string portion of an SPDX element
@@ -92,15 +86,9 @@ func prefixElementId(id ElementID) string {
9286
return val
9387
}
9488

95-
// trimElementIdPrefix removes the SPDXRef- prefix from an element ID string or returns an error if it
96-
// does not start with SPDXRef-
89+
// trimElementIdPrefix removes the SPDXRef- prefix from an element ID string
9790
func trimElementIdPrefix(id string) (ElementID, error) {
98-
// handle SPDXRef-
99-
if !strings.HasPrefix(id, spdxRefPrefix) {
100-
return "", fmt.Errorf("failed to parse SPDX identifier '%s'", id)
101-
}
102-
e := ElementID(strings.TrimPrefix(id, spdxRefPrefix))
103-
return e, nil
91+
return ElementID(strings.TrimPrefix(id, spdxRefPrefix)), nil
10492
}
10593

10694
// DocElementID represents an SPDX element identifier that could be defined

spdx/v2/common/identifier_test.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,19 @@ func Test_DocElementIDDecoding(t *testing.T) {
103103
},
104104
},
105105
{
106-
name: "DocumentRefID invalid ElementRefID",
107-
value: "DocumentRef-a-doc:invalid",
108-
err: true,
106+
name: "DocumentRefID:ElementRefID without spdxref prefix",
107+
value: "DocumentRef-a-doc:some-id",
108+
expected: DocElementID{
109+
DocumentRefID: "a-doc",
110+
ElementRefID: "some-id",
111+
},
109112
},
110113
{
111-
name: "invalid format",
114+
name: "without spdxref prefix",
112115
value: "some-id-without-spdxref",
113-
err: true,
116+
expected: DocElementID{
117+
ElementRefID: "some-id-without-spdxref",
118+
},
114119
},
115120
{
116121
name: "SpecialID NONE",
@@ -203,9 +208,9 @@ func Test_ElementIDDecoding(t *testing.T) {
203208
expected: ElementID("some-id"),
204209
},
205210
{
206-
name: "invalid format",
211+
name: "without prefix",
207212
value: "some-id-without-spdxref",
208-
err: true,
213+
expected: ElementID("some-id-without-spdxref"),
209214
},
210215
}
211216

@@ -292,9 +297,11 @@ func Test_ElementIDStructDecoding(t *testing.T) {
292297
value: `{"id":"SPDXRef-some-id"}`,
293298
},
294299
{
295-
name: "invalid format",
300+
name: "without prefix",
301+
expected: typ{
302+
Id: ElementID("some-id"),
303+
},
296304
value: `{"id":"some-id"}`,
297-
err: true,
298305
},
299306
}
300307

0 commit comments

Comments
 (0)