From e40b5386299d68156151222ad5b092dcc6f2a04d Mon Sep 17 00:00:00 2001 From: Steve Winslow Date: Sun, 8 Nov 2020 16:06:08 -0500 Subject: Fix saving for expanded external document refs Signed-off-by: Steve Winslow --- tvsaver/saver2v1/save_creation_info.go | 13 +++++++++++-- tvsaver/saver2v1/save_creation_info_test.go | 16 +++++++++++++--- tvsaver/saver2v2/save_creation_info.go | 13 +++++++++++-- tvsaver/saver2v2/save_creation_info_test.go | 16 +++++++++++++--- 4 files changed, 48 insertions(+), 10 deletions(-) (limited to 'tvsaver') diff --git a/tvsaver/saver2v1/save_creation_info.go b/tvsaver/saver2v1/save_creation_info.go index 17e14c9..6ea6086 100644 --- a/tvsaver/saver2v1/save_creation_info.go +++ b/tvsaver/saver2v1/save_creation_info.go @@ -5,6 +5,7 @@ package saver2v1 import ( "fmt" "io" + "sort" "github.com/spdx/tools-golang/spdx" ) @@ -25,8 +26,16 @@ func renderCreationInfo2_1(ci *spdx.CreationInfo2_1, w io.Writer) error { if ci.DocumentNamespace != "" { fmt.Fprintf(w, "DocumentNamespace: %s\n", ci.DocumentNamespace) } - for _, s := range ci.ExternalDocumentReferences { - fmt.Fprintf(w, "ExternalDocumentRef: %s\n", s) + // print EDRs in order sorted by identifier + edrIDs := []string{} + for docRefID := range ci.ExternalDocumentReferences { + edrIDs = append(edrIDs, docRefID) + } + sort.Strings(edrIDs) + for _, edrID := range edrIDs { + edr := ci.ExternalDocumentReferences[edrID] + fmt.Fprintf(w, "ExternalDocumentRef: DocumentRef-%s %s %s:%s\n", + edr.DocumentRefID, edr.URI, edr.Alg, edr.Checksum) } if ci.LicenseListVersion != "" { fmt.Fprintf(w, "LicenseListVersion: %s\n", ci.LicenseListVersion) diff --git a/tvsaver/saver2v1/save_creation_info_test.go b/tvsaver/saver2v1/save_creation_info_test.go index 3b961ec..cec03c7 100644 --- a/tvsaver/saver2v1/save_creation_info_test.go +++ b/tvsaver/saver2v1/save_creation_info_test.go @@ -17,9 +17,19 @@ func TestSaver2_1CISavesText(t *testing.T) { SPDXIdentifier: spdx.ElementID("DOCUMENT"), DocumentName: "spdx-go-0.0.1.abcdef", DocumentNamespace: "https://github.com/swinslow/spdx-docs/spdx-go/spdx-go-0.0.1.abcdef.whatever", - ExternalDocumentReferences: []string{ - "DocumentRef-spdx-go-0.0.1a https://github.com/swinslow/spdx-docs/spdx-go/spdx-go-0.0.1a.cdefab.whatever SHA1:0123456701234567012345670123456701234567", - "DocumentRef-time-1.2.3 https://github.com/swinslow/spdx-docs/time/time-1.2.3.cdefab.whatever SHA1:0123456701234567012345670123456701234568", + ExternalDocumentReferences: map[string]spdx.ExternalDocumentRef2_1{ + "spdx-go-0.0.1a": spdx.ExternalDocumentRef2_1{ + DocumentRefID: "spdx-go-0.0.1a", + URI: "https://github.com/swinslow/spdx-docs/spdx-go/spdx-go-0.0.1a.cdefab.whatever", + Alg: "SHA1", + Checksum: "0123456701234567012345670123456701234567", + }, + "time-1.2.3": spdx.ExternalDocumentRef2_1{ + DocumentRefID: "time-1.2.3", + URI: "https://github.com/swinslow/spdx-docs/time/time-1.2.3.cdefab.whatever", + Alg: "SHA1", + Checksum: "0123456701234567012345670123456701234568", + }, }, LicenseListVersion: "2.0", CreatorPersons: []string{ diff --git a/tvsaver/saver2v2/save_creation_info.go b/tvsaver/saver2v2/save_creation_info.go index af9b977..1492fb4 100644 --- a/tvsaver/saver2v2/save_creation_info.go +++ b/tvsaver/saver2v2/save_creation_info.go @@ -5,6 +5,7 @@ package saver2v2 import ( "fmt" "io" + "sort" "github.com/spdx/tools-golang/spdx" ) @@ -25,8 +26,16 @@ func renderCreationInfo2_2(ci *spdx.CreationInfo2_2, w io.Writer) error { if ci.DocumentNamespace != "" { fmt.Fprintf(w, "DocumentNamespace: %s\n", ci.DocumentNamespace) } - for _, s := range ci.ExternalDocumentReferences { - fmt.Fprintf(w, "ExternalDocumentRef: %s\n", s) + // print EDRs in order sorted by identifier + edrIDs := []string{} + for docRefID := range ci.ExternalDocumentReferences { + edrIDs = append(edrIDs, docRefID) + } + sort.Strings(edrIDs) + for _, edrID := range edrIDs { + edr := ci.ExternalDocumentReferences[edrID] + fmt.Fprintf(w, "ExternalDocumentRef: DocumentRef-%s %s %s:%s\n", + edr.DocumentRefID, edr.URI, edr.Alg, edr.Checksum) } if ci.LicenseListVersion != "" { fmt.Fprintf(w, "LicenseListVersion: %s\n", ci.LicenseListVersion) diff --git a/tvsaver/saver2v2/save_creation_info_test.go b/tvsaver/saver2v2/save_creation_info_test.go index 3e23c0b..404abfe 100644 --- a/tvsaver/saver2v2/save_creation_info_test.go +++ b/tvsaver/saver2v2/save_creation_info_test.go @@ -17,9 +17,19 @@ func TestSaver2_2CISavesText(t *testing.T) { SPDXIdentifier: spdx.ElementID("DOCUMENT"), DocumentName: "spdx-go-0.0.1.abcdef", DocumentNamespace: "https://github.com/swinslow/spdx-docs/spdx-go/spdx-go-0.0.1.abcdef.whatever", - ExternalDocumentReferences: []string{ - "DocumentRef-spdx-go-0.0.1a https://github.com/swinslow/spdx-docs/spdx-go/spdx-go-0.0.1a.cdefab.whatever SHA1:0123456701234567012345670123456701234567", - "DocumentRef-time-1.2.3 https://github.com/swinslow/spdx-docs/time/time-1.2.3.cdefab.whatever SHA1:0123456701234567012345670123456701234568", + ExternalDocumentReferences: map[string]spdx.ExternalDocumentRef2_2{ + "spdx-go-0.0.1a": spdx.ExternalDocumentRef2_2{ + DocumentRefID: "spdx-go-0.0.1a", + URI: "https://github.com/swinslow/spdx-docs/spdx-go/spdx-go-0.0.1a.cdefab.whatever", + Alg: "SHA1", + Checksum: "0123456701234567012345670123456701234567", + }, + "time-1.2.3": spdx.ExternalDocumentRef2_2{ + DocumentRefID: "time-1.2.3", + URI: "https://github.com/swinslow/spdx-docs/time/time-1.2.3.cdefab.whatever", + Alg: "SHA1", + Checksum: "0123456701234567012345670123456701234568", + }, }, LicenseListVersion: "3.9", CreatorPersons: []string{ -- cgit v1.2.3