Skip to content

Commit

Permalink
Jsonsaver : Create a temporary storage for files instead of modifying…
Browse files Browse the repository at this point in the history
… original document

Signed-off-by: Ujjwal Agarwal <ujjwalcoding012@gmail.com>
  • Loading branch information
specter25 committed Aug 15, 2021
1 parent 87a00cd commit e26f08d
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 17 deletions.
14 changes: 9 additions & 5 deletions jsonsaver/saver2v2/save_document.go
Expand Up @@ -57,22 +57,26 @@ func RenderDocument2_2(doc *spdx.Document2_2, buf *bytes.Buffer) error {
}
jsondocument["documentDescribes"] = describesID
}

allfiles := make(map[spdx.ElementID]*spdx.File2_2)
// save packages from spdx to json
if doc.Packages != nil {
_, err = renderPackage2_2(doc, jsondocument)
_, err = renderPackage2_2(doc, jsondocument, allfiles)
if err != nil {
return err
}
}

for k, v := range doc.UnpackagedFiles {
allfiles[k] = v
}

// save files and snippets from spdx to json
if doc.UnpackagedFiles != nil {
_, err = renderFiles2_2(doc, jsondocument)
if allfiles != nil {
_, err = renderFiles2_2(doc, jsondocument, allfiles)
if err != nil {
return err
}
_, err = renderSnippets2_2(doc, jsondocument)
_, err = renderSnippets2_2(jsondocument, allfiles)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions jsonsaver/saver2v2/save_files.go
Expand Up @@ -8,18 +8,17 @@ import (
"github.com/spdx/tools-golang/spdx"
)

func renderFiles2_2(doc *spdx.Document2_2, jsondocument map[string]interface{}) ([]interface{}, error) {
func renderFiles2_2(doc *spdx.Document2_2, jsondocument map[string]interface{}, allfiles map[spdx.ElementID]*spdx.File2_2) ([]interface{}, error) {

var keys []string
for ke := range doc.UnpackagedFiles {
for ke := range allfiles {
keys = append(keys, string(ke))
}
sort.Strings(keys)

var files []interface{}
// for k, v := range doc.UnpackagedFiles {
for _, k := range keys {
v := doc.UnpackagedFiles[spdx.ElementID(k)]
v := allfiles[spdx.ElementID(k)]
file := make(map[string]interface{})
file["SPDXID"] = spdx.RenderElementID(spdx.ElementID(k))
ann, _ := renderAnnotations2_2(doc.Annotations, spdx.MakeDocElementID("", string(v.FileSPDXIdentifier)))
Expand Down
2 changes: 1 addition & 1 deletion jsonsaver/saver2v2/save_files_test.go
Expand Up @@ -132,7 +132,7 @@ func Test_renderFiles2_2(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := renderFiles2_2(tt.args.doc, tt.args.jsondocument)
got, err := renderFiles2_2(tt.args.doc, tt.args.jsondocument, tt.args.doc.UnpackagedFiles)
if (err != nil) != tt.wantErr {
t.Errorf("renderFiles2_2() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down
4 changes: 2 additions & 2 deletions jsonsaver/saver2v2/save_package.go
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spdx/tools-golang/spdx"
)

func renderPackage2_2(doc *spdx.Document2_2, jsondocument map[string]interface{}) ([]interface{}, error) {
func renderPackage2_2(doc *spdx.Document2_2, jsondocument map[string]interface{}, allfiles map[spdx.ElementID]*spdx.File2_2) ([]interface{}, error) {

var packages []interface{}

Expand Down Expand Up @@ -82,7 +82,7 @@ func renderPackage2_2(doc *spdx.Document2_2, jsondocument map[string]interface{}
if v.Files != nil {
var fileIds []string
for k, v := range v.Files {
doc.UnpackagedFiles[k] = v
allfiles[k] = v
fileIds = append(fileIds, spdx.RenderElementID(k))
}
pkg["hasFiles"] = fileIds
Expand Down
2 changes: 1 addition & 1 deletion jsonsaver/saver2v2/save_package_test.go
Expand Up @@ -206,7 +206,7 @@ func Test_renderPackage2_2(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := renderPackage2_2(tt.args.doc, tt.args.jsondocument)
got, err := renderPackage2_2(tt.args.doc, tt.args.jsondocument, make(map[spdx.ElementID]*spdx.File2_2))
if (err != nil) != tt.wantErr {
t.Errorf("renderPackage2_2() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
5 changes: 2 additions & 3 deletions jsonsaver/saver2v2/save_snippets.go
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/spdx/tools-golang/spdx"
)

func renderSnippets2_2(doc *spdx.Document2_2, jsondocument map[string]interface{}) ([]interface{}, error) {
func renderSnippets2_2(jsondocument map[string]interface{}, allfiles map[spdx.ElementID]*spdx.File2_2) ([]interface{}, error) {

var snippets []interface{}
for _, value := range doc.UnpackagedFiles {
for _, value := range allfiles {
snippet := make(map[string]interface{})

var keys []string
Expand All @@ -21,7 +21,6 @@ func renderSnippets2_2(doc *spdx.Document2_2, jsondocument map[string]interface{
sort.Strings(keys)
for _, k := range keys {
v := value.Snippets[spdx.ElementID(k)]
// for _, v := range value.Snippets {
snippet["SPDXID"] = spdx.RenderElementID(v.SnippetSPDXIdentifier)
if v.SnippetComment != "" {
snippet["comment"] = v.SnippetComment
Expand Down
2 changes: 1 addition & 1 deletion jsonsaver/saver2v2/save_snippets_test.go
Expand Up @@ -102,7 +102,7 @@ func Test_renderSnippets2_2(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := renderSnippets2_2(tt.args.doc, tt.args.jsondocument)
got, err := renderSnippets2_2(tt.args.jsondocument, tt.args.doc.UnpackagedFiles)
if (err != nil) != tt.wantErr {
t.Errorf("renderSnippets2_2() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit e26f08d

Please sign in to comment.