Skip to content

Commit

Permalink
Examples : add example of jsonloader and remove existing conversion e…
Browse files Browse the repository at this point in the history
…xamples

- bug fixes in json saver done as well
- test covereage of jsonparser increased

Signed-off-by: Ujjwal Agarwal <ujjwalcoding012@gmail.com>
  • Loading branch information
specter25 committed Aug 6, 2021
1 parent 1a9690f commit 1bc87f7
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 31 deletions.
55 changes: 55 additions & 0 deletions examples/10-jsonloader/example_json_loader.go
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

// Example for: *jsonparser2v2*

// This example demonstrates loading an SPDX json from disk into memory,
// and then logging out some attributes to the console .

package main

import (
"fmt"
"os"
"strings"

"github.com/spdx/tools-golang/jsonloader"
)

func main() {

// check that we've received the right number of arguments
args := os.Args
if len(args) != 3 {
fmt.Printf("Usage: %v <spdx-file-in> <spdx-file-out>\n", args[0])
fmt.Printf(" Load SPDX 2.2 tag-value file <spdx-file-in>, and\n")
fmt.Printf(" save it out to <spdx-file-out>.\n")
return
}

// open the SPDX file
fileIn := args[1]
r, err := os.Open(fileIn)
if err != nil {
fmt.Printf("Error while opening %v for reading: %v", fileIn, err)
return
}
defer r.Close()

// try to load the SPDX file's contents as a json file, version 2.2
doc, err := jsonloader.Load2_2(r)
if err != nil {
fmt.Printf("Error while parsing %v: %v", args[1], err)
return
}

// if we got here, the file is now loaded into memory.
fmt.Printf("Successfully loaded %s\n", args[1])

fmt.Println(strings.Repeat("=", 80))
fmt.Println("Some Attributes of the Document:")
fmt.Printf("Document Name: %s\n", doc.CreationInfo.DocumentName)
fmt.Printf("DataLicense: %s\n", doc.CreationInfo.DataLicense)
fmt.Printf("Document NameSpace: %s\n", doc.CreationInfo.DocumentNamespace)
fmt.Printf("SPDX Document Version: %s\n", doc.CreationInfo.SPDXVersion)
fmt.Println(strings.Repeat("=", 80))
}
File renamed without changes.
File renamed without changes.
10 changes: 8 additions & 2 deletions examples/README.md
Expand Up @@ -64,17 +64,23 @@ the same identifier in both documents.
This example demonstrates loading an SPDX rdf file from disk into memory
and then printing the corresponding spdx struct for the document.

## 8-jsonloader
## 8-jsontotv

*jsonloader*, *tvsaver*

This example demonstrates loading an SPDX json from disk into memory
and then re-saving it to a different file on disk in tag-value format.

## 9-jsonsaver
## 9-tvtojson

*jsonsaver*, *tvloader*

This example demonstrates loading an SPDX tag-value from disk into memory
and then re-saving it to a different file on disk in json format.

## 10-jsonloader

*jsonloader*

This example demonstrates loading an SPDX json from disk into memory
and then logging some of the attributes to the console.
4 changes: 0 additions & 4 deletions jsonsaver/jsonsaver_test.go
Expand Up @@ -197,24 +197,20 @@ func TestSave2_2(t *testing.T) {
tests := []struct {
name string
args args
wantW string
wantErr bool
}{
// TODO: Add test cases.
{
name: "success",
args: args{
doc: test1,
},
wantW: "",
wantErr: false,
},
{
name: "failure",
args: args{
doc: &spdx.Document2_2{},
},
wantW: "",
wantErr: true,
},
}
Expand Down
2 changes: 1 addition & 1 deletion jsonsaver/saver2v2/save_creation_info_test.go
Expand Up @@ -82,7 +82,7 @@ func Test_renderCreationInfo2_2(t *testing.T) {
}
for k, v := range tt.want {
if !reflect.DeepEqual(tt.args.jsondocument[k], v) {
t.Errorf("Load2_2() = %v, want %v", tt.args.jsondocument[k], v)
t.Errorf("renderCreationInfo2_2() = %v, want %v", tt.args.jsondocument[k], v)
}
}
})
Expand Down
45 changes: 37 additions & 8 deletions jsonsaver/saver2v2/save_document.go
Expand Up @@ -24,17 +24,28 @@ func RenderDocument2_2(doc *spdx.Document2_2, buf *bytes.Buffer) error {
if doc.CreationInfo == nil {
return fmt.Errorf("document had nil CreationInfo section")
}
renderCreationInfo2_2(doc.CreationInfo, jsondocument)
err := renderCreationInfo2_2(doc.CreationInfo, jsondocument)
if err != nil {
return err
}

// save otherlicenses from sodx struct to json
if doc.OtherLicenses != nil {
renderOtherLicenses2_2(doc.OtherLicenses, jsondocument)
_, err = renderOtherLicenses2_2(doc.OtherLicenses, jsondocument)
if err != nil {
return err
}
}

// save document level annotations
if doc.Annotations != nil {
ann, _ := renderAnnotations2_2(doc.Annotations, spdx.MakeDocElementID("", string(doc.CreationInfo.SPDXIdentifier)))
ann, err := renderAnnotations2_2(doc.Annotations, spdx.MakeDocElementID("", string(doc.CreationInfo.SPDXIdentifier)))
if err != nil {
return err
}

jsondocument["annotations"] = ann

}

// save document describes
Expand All @@ -49,23 +60,41 @@ func RenderDocument2_2(doc *spdx.Document2_2, buf *bytes.Buffer) error {

// save packages from spdx to json
if doc.Packages != nil {
renderPackage2_2(doc, jsondocument)
_, err = renderPackage2_2(doc, jsondocument)
if err != nil {
return err
}
}

// save files and snippets from spdx to json
if doc.UnpackagedFiles != nil {
renderFiles2_2(doc, jsondocument)
renderSnippets2_2(doc, jsondocument)
_, err = renderFiles2_2(doc, jsondocument)
if err != nil {
return err
}
_, err = renderSnippets2_2(doc, jsondocument)
if err != nil {
return err
}

}

// save reviews from spdx to json
if doc.Reviews != nil {
renderReviews2_2(doc.Reviews, jsondocument)
_, err = renderReviews2_2(doc.Reviews, jsondocument)
if err != nil {
return err
}

}

// save relationships from spdx to json
if doc.Relationships != nil {
renderRelationships2_2(doc.Relationships, jsondocument)
_, err = renderRelationships2_2(doc.Relationships, jsondocument)
if err != nil {
return err
}

}

jsonspec, err := json.MarshalIndent(jsondocument, "", "\t")
Expand Down
16 changes: 8 additions & 8 deletions jsonsaver/saver2v2/save_document_test.go
Expand Up @@ -181,10 +181,10 @@ func TestRenderDocument2_2(t *testing.T) {
Algorithm: "SHA1",
Value: "d6a770ba38583ed4bb4525bd96e50461655d2758",
},
"MD5": {
Algorithm: "MD5",
Value: "624c1abb3664f4b35547e7c73864ad24",
},
// "MD5": {
// Algorithm: "MD5",
// Value: "624c1abb3664f4b35547e7c73864ad24",
// },
},
FileComment: "The concluded license was taken from the package level that the file was .\nThis information was found in the COPYING.txt file in the xyz directory.",
FileCopyrightText: "Copyright 2008-2010 John Smith",
Expand Down Expand Up @@ -332,10 +332,10 @@ func TestRenderDocument2_2(t *testing.T) {
"algorithm": "SHA1",
"checksumValue": "d6a770ba38583ed4bb4525bd96e50461655d2758",
},
map[string]interface{}{
"algorithm": "MD5",
"checksumValue": "624c1abb3664f4b35547e7c73864ad24",
},
// map[string]interface{}{
// "algorithm": "MD5",
// "checksumValue": "624c1abb3664f4b35547e7c73864ad24",
// },
},
"comment": "The concluded license was taken from the package level that the file was .\nThis information was found in the COPYING.txt file in the xyz directory.",
"copyrightText": "Copyright 2008-2010 John Smith",
Expand Down
16 changes: 8 additions & 8 deletions jsonsaver/saver2v2/save_files_test.go
Expand Up @@ -75,10 +75,10 @@ func Test_renderFiles2_2(t *testing.T) {
Algorithm: "SHA1",
Value: "d6a770ba38583ed4bb4525bd96e50461655d2758",
},
"MD5": {
Algorithm: "MD5",
Value: "624c1abb3664f4b35547e7c73864ad24",
},
// "MD5": {
// Algorithm: "MD5",
// Value: "624c1abb3664f4b35547e7c73864ad24",
// },
},
FileComment: "The concluded license was taken from the package level that the file was .",
FileCopyrightText: "Copyright 2008-2010 John Smith",
Expand Down Expand Up @@ -111,10 +111,10 @@ func Test_renderFiles2_2(t *testing.T) {
"algorithm": "SHA1",
"checksumValue": "d6a770ba38583ed4bb4525bd96e50461655d2758",
},
map[string]interface{}{
"algorithm": "MD5",
"checksumValue": "624c1abb3664f4b35547e7c73864ad24",
},
// map[string]interface{}{
// "algorithm": "MD5",
// "checksumValue": "624c1abb3664f4b35547e7c73864ad24",
// },
},
"comment": "The concluded license was taken from the package level that the file was .",
"copyrightText": "Copyright 2008-2010 John Smith",
Expand Down

0 comments on commit 1bc87f7

Please sign in to comment.