Skip to content

Commit 1e01e57

Browse files
fix: update utilities for DocumentID type change
And fix broken tagvalue example (#267) Signed-off-by: Souta Kawahara <souta.kawahara@almalinux.org>
1 parent 46cc501 commit 1e01e57

25 files changed

+73
-67
lines changed

convert/spdx_document_conversion_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ func Test_ConvertSPDXDocuments(t *testing.T) {
8080
DocumentNamespace: "doc namespace",
8181
ExternalDocumentReferences: []v2_1.ExternalDocumentRef{
8282
{
83-
DocumentRefID: "doc ref id 1",
83+
DocumentRefID: common.DocumentID("doc ref id 1"),
8484
URI: "uri 1",
8585
Checksum: common.Checksum{
8686
Algorithm: "algo 1",
8787
Value: "value 1",
8888
},
8989
},
9090
{
91-
DocumentRefID: "doc ref id 2",
91+
DocumentRefID: common.DocumentID("doc ref id 2"),
9292
URI: "uri 2",
9393
Checksum: common.Checksum{
9494
Algorithm: "algo 2",
@@ -583,15 +583,15 @@ func Test_ConvertSPDXDocuments(t *testing.T) {
583583
DocumentNamespace: "doc namespace",
584584
ExternalDocumentReferences: []spdx.ExternalDocumentRef{
585585
{
586-
DocumentRefID: "doc ref id 1",
586+
DocumentRefID: common.DocumentID("doc ref id 1"),
587587
URI: "uri 1",
588588
Checksum: common.Checksum{
589589
Algorithm: "algo 1",
590590
Value: "value 1",
591591
},
592592
},
593593
{
594-
DocumentRefID: "doc ref id 2",
594+
DocumentRefID: common.DocumentID("doc ref id 2"),
595595
URI: "uri 2",
596596
Checksum: common.Checksum{
597597
Algorithm: "algo 2",
@@ -1090,15 +1090,15 @@ func Test_ConvertSPDXDocuments(t *testing.T) {
10901090
DocumentNamespace: "doc namespace",
10911091
ExternalDocumentReferences: []v2_2.ExternalDocumentRef{
10921092
{
1093-
DocumentRefID: "doc ref id 1",
1093+
DocumentRefID: common.DocumentID("doc ref id 1"),
10941094
URI: "uri 1",
10951095
Checksum: common.Checksum{
10961096
Algorithm: "algo 1",
10971097
Value: "value 1",
10981098
},
10991099
},
11001100
{
1101-
DocumentRefID: "doc ref id 2",
1101+
DocumentRefID: common.DocumentID("doc ref id 2"),
11021102
URI: "uri 2",
11031103
Checksum: common.Checksum{
11041104
Algorithm: "algo 2",
@@ -1602,15 +1602,15 @@ func Test_ConvertSPDXDocuments(t *testing.T) {
16021602
DocumentNamespace: "doc namespace",
16031603
ExternalDocumentReferences: []spdx.ExternalDocumentRef{
16041604
{
1605-
DocumentRefID: "doc ref id 1",
1605+
DocumentRefID: common.DocumentID("doc ref id 1"),
16061606
URI: "uri 1",
16071607
Checksum: common.Checksum{
16081608
Algorithm: "algo 1",
16091609
Value: "value 1",
16101610
},
16111611
},
16121612
{
1613-
DocumentRefID: "doc ref id 2",
1613+
DocumentRefID: common.DocumentID("doc ref id 2"),
16141614
URI: "uri 2",
16151615
Checksum: common.Checksum{
16161616
Algorithm: "algo 2",

examples/sample-docs/tv/SPDXTagExample-v2.3.spdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ DataLicense: CC0-1.0
33
SPDXID: SPDXRef-DOCUMENT
44
DocumentName: SPDX-Tools-v2.0
55
DocumentNamespace: http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301
6-
ExternalDocumentRef: DocumentRef-DocumentRef-spdx-tool-1.2 http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301 SHA1:d6a770ba38583ed4bb4525bd96e50461655d2759
6+
ExternalDocumentRef: DocumentRef-spdx-tool-1.2 http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301 SHA1:d6a770ba38583ed4bb4525bd96e50461655d2759
77
DocumentComment: This document was created using SPDX 2.0 using licenses from the web site.
88
LicenseListVersion: 3.9
99
Creator: Tool: LicenseFind-1.0

spdx/v2/v2_1/document.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type ExternalDocumentRef struct {
1818
// DocumentRefID is the ID string defined in the start of the
1919
// reference. It should _not_ contain the "DocumentRef-" part
2020
// of the mandatory ID string.
21-
DocumentRefID string `json:"externalDocumentId"`
21+
DocumentRefID common.DocumentID `json:"externalDocumentId"`
2222

2323
// URI is the URI defined for the external document
2424
URI string `json:"spdxDocument"`

spdx/v2/v2_1/tagvalue/reader/parse_creation_info.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (parser *tvParser) parsePairFromCreationInfo(tag string, value string) erro
103103

104104
// ===== Helper functions =====
105105

106-
func extractExternalDocumentReference(value string) (string, string, string, string, error) {
106+
func extractExternalDocumentReference(value string) (common.DocumentID, string, string, string, error) {
107107
sp := strings.Split(value, " ")
108108
// remove any that are just whitespace
109109
keepSp := []string{}
@@ -114,42 +114,43 @@ func extractExternalDocumentReference(value string) (string, string, string, str
114114
}
115115
}
116116

117-
var documentRefID, uri, alg, checksum string
117+
var documentRefID common.DocumentID
118+
var strDocRefID, uri, alg, checksum string
118119

119120
// now, should have 4 items (or 3, if Alg and Checksum were joined)
120121
// and should be able to map them
121122
if len(keepSp) == 4 {
122-
documentRefID = keepSp[0]
123+
strDocRefID = keepSp[0]
123124
uri = keepSp[1]
124125
alg = keepSp[2]
125126
// check that colon is present for alg, and remove it
126127
if !strings.HasSuffix(alg, ":") {
127-
return "", "", "", "", fmt.Errorf("algorithm does not end with colon")
128+
return common.DocumentID(""), "", "", "", fmt.Errorf("algorithm does not end with colon")
128129
}
129130
alg = strings.TrimSuffix(alg, ":")
130131
checksum = keepSp[3]
131132
} else if len(keepSp) == 3 {
132-
documentRefID = keepSp[0]
133+
strDocRefID = keepSp[0]
133134
uri = keepSp[1]
134135
// split on colon into alg and checksum
135136
parts := strings.SplitN(keepSp[2], ":", 2)
136137
if len(parts) != 2 {
137-
return "", "", "", "", fmt.Errorf("missing colon separator between algorithm and checksum")
138+
return common.DocumentID(""), "", "", "", fmt.Errorf("missing colon separator between algorithm and checksum")
138139
}
139140
alg = parts[0]
140141
checksum = parts[1]
141142
} else {
142-
return "", "", "", "", fmt.Errorf("expected 4 elements, got %d", len(keepSp))
143+
return common.DocumentID(""), "", "", "", fmt.Errorf("expected 4 elements, got %d", len(keepSp))
143144
}
144145

145146
// additionally, we should be able to parse the first element as a
146147
// DocumentRef- ID string, and we should remove that prefix
147-
if !strings.HasPrefix(documentRefID, "DocumentRef-") {
148-
return "", "", "", "", fmt.Errorf("expected first element to have DocumentRef- prefix")
148+
if !strings.HasPrefix(strDocRefID, "DocumentRef-") {
149+
return common.DocumentID(""), "", "", "", fmt.Errorf("expected first element to have DocumentRef- prefix")
149150
}
150-
documentRefID = strings.TrimPrefix(documentRefID, "DocumentRef-")
151+
documentRefID = common.DocumentID(strings.TrimPrefix(strDocRefID, "DocumentRef-"))
151152
if documentRefID == "" {
152-
return "", "", "", "", fmt.Errorf("document identifier has nothing after prefix")
153+
return common.DocumentID(""), "", "", "", fmt.Errorf("document identifier has nothing after prefix")
153154
}
154155

155156
return documentRefID, uri, alg, checksum, nil

spdx/v2/v2_1/tagvalue/reader/parse_creation_info_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
spdx "github.com/spdx/tools-golang/spdx/v2/v2_1"
8+
"github.com/spdx/tools-golang/spdx/v2/common"
89
)
910

1011
// ===== Parser creation info state change tests =====
@@ -360,7 +361,7 @@ func TestParserCICreatesAnnotation(t *testing.T) {
360361

361362
func TestCanExtractExternalDocumentReference(t *testing.T) {
362363
refstring := "DocumentRef-spdx-tool-1.2 http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301 SHA1:d6a770ba38583ed4bb4525bd96e50461655d2759"
363-
wantDocumentRefID := "spdx-tool-1.2"
364+
wantDocumentRefID := common.DocumentID("spdx-tool-1.2")
364365
wantURI := "http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301"
365366
wantAlg := "SHA1"
366367
wantChecksum := "d6a770ba38583ed4bb4525bd96e50461655d2759"
@@ -385,7 +386,7 @@ func TestCanExtractExternalDocumentReference(t *testing.T) {
385386

386387
func TestCanExtractExternalDocumentReferenceWithExtraWhitespace(t *testing.T) {
387388
refstring := " DocumentRef-spdx-tool-1.2 \t http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301 \t SHA1: \t d6a770ba38583ed4bb4525bd96e50461655d2759"
388-
wantDocumentRefID := "spdx-tool-1.2"
389+
wantDocumentRefID := common.DocumentID("spdx-tool-1.2")
389390
wantURI := "http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301"
390391
wantAlg := "SHA1"
391392
wantChecksum := "d6a770ba38583ed4bb4525bd96e50461655d2759"

spdx/v2/v2_1/tagvalue/reader/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func extractDocElementID(value string) (common.DocElementID, error) {
7070
}
7171

7272
// we're good
73-
return common.DocElementID{DocumentRefID: docRefID, ElementRefID: common.ElementID(eltRefID)}, nil
73+
return common.DocElementID{DocumentRefID: common.DocumentID(docRefID), ElementRefID: common.ElementID(eltRefID)}, nil
7474
}
7575

7676
// used to extract SPDXRef values only from an SPDX Identifier which can point

spdx/v2/v2_1/tagvalue/reader/util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func helperForExtractDocElementID(t *testing.T, tst string, wantErr bool, wantDo
5858
if err == nil && wantErr == true {
5959
t.Errorf("testing %v: expected non-nil error, got nil", tst)
6060
}
61-
if deID.DocumentRefID != wantDoc {
61+
if deID.DocumentRefID != common.DocumentID(wantDoc) {
6262
if wantDoc == "" {
6363
t.Errorf("testing %v: want empty string for DocumentRefID, got %v", tst, deID.DocumentRefID)
6464
} else {

spdx/v2/v2_2/document.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type ExternalDocumentRef struct {
2121
// DocumentRefID is the ID string defined in the start of the
2222
// reference. It should _not_ contain the "DocumentRef-" part
2323
// of the mandatory ID string.
24-
DocumentRefID string `json:"externalDocumentId"`
24+
DocumentRefID common.DocumentID `json:"externalDocumentId"`
2525

2626
// URI is the URI defined for the external document
2727
URI string `json:"spdxDocument"`

spdx/v2/v2_2/example/example.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var example = spdx.Document{
4141
DocumentComment: "This document was created using SPDX 2.0 using licenses from the web site.",
4242
ExternalDocumentReferences: []spdx.ExternalDocumentRef{
4343
{
44-
DocumentRefID: "DocumentRef-spdx-tool-1.2",
44+
DocumentRefID: common.DocumentID("spdx-tool-1.2"),
4545
URI: "http://spdx.org/spdxdocs/spdx-tools-v1.2-3F2504E0-4F89-41D3-9A0C-0305E82C3301",
4646
Checksum: common.Checksum{
4747
Algorithm: common.SHA1,

spdx/v2/v2_2/rdf/reader/parse_spdx_document.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (parser *rdfParser2_2) getExternalDocumentRefFromNode(node *gordfParser.Nod
9696
switch triple.Predicate.ID {
9797
case SPDX_EXTERNAL_DOCUMENT_ID:
9898
// cardinality: exactly 1
99-
edr.DocumentRefID = triple.Object.ID
99+
edr.DocumentRefID = common.DocumentID(triple.Object.ID)
100100
case SPDX_SPDX_DOCUMENT:
101101
// cardinality: exactly 1
102102
// assumption: "spdxDocument" property of an external document

0 commit comments

Comments
 (0)