diff options
author | Ujjwal Agarwal <ujjwalcoding012@gmail.com> | 2021-08-06 18:15:01 +0530 |
---|---|---|
committer | Ujjwal Agarwal <ujjwalcoding012@gmail.com> | 2021-08-06 18:15:01 +0530 |
commit | 1bc87f7cbc680e409a1ec51fdfdd93b5d2b49701 (patch) | |
tree | 250966ff349b1e1dab25505774ccddc87a0d6d45 | |
parent | 1a9690feffaf2257985570998037cefd196e21aa (diff) | |
download | spdx-tools-1bc87f7cbc680e409a1ec51fdfdd93b5d2b49701.tar.gz |
Examples : add example of jsonloader and remove existing conversion examples
- bug fixes in json saver done as well
- test covereage of jsonparser increased
Signed-off-by: Ujjwal Agarwal <ujjwalcoding012@gmail.com>
-rw-r--r-- | examples/10-jsonloader/example_json_loader.go | 55 | ||||
-rw-r--r-- | examples/8-jsontotv/examplejsontotv.go (renamed from examples/8-jsonloader/examplejsontotv.go) | 0 | ||||
-rw-r--r-- | examples/9-tvtojson/exampletvtojson.go (renamed from examples/9-jsonsaver/exampletvtojson.go) | 0 | ||||
-rw-r--r-- | examples/README.md | 10 | ||||
-rw-r--r-- | jsonsaver/jsonsaver_test.go | 4 | ||||
-rw-r--r-- | jsonsaver/saver2v2/save_creation_info_test.go | 2 | ||||
-rw-r--r-- | jsonsaver/saver2v2/save_document.go | 45 | ||||
-rw-r--r-- | jsonsaver/saver2v2/save_document_test.go | 16 | ||||
-rw-r--r-- | jsonsaver/saver2v2/save_files_test.go | 16 |
9 files changed, 117 insertions, 31 deletions
diff --git a/examples/10-jsonloader/example_json_loader.go b/examples/10-jsonloader/example_json_loader.go new file mode 100644 index 0000000..96f47fd --- /dev/null +++ b/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)) +} diff --git a/examples/8-jsonloader/examplejsontotv.go b/examples/8-jsontotv/examplejsontotv.go index ede044e..ede044e 100644 --- a/examples/8-jsonloader/examplejsontotv.go +++ b/examples/8-jsontotv/examplejsontotv.go diff --git a/examples/9-jsonsaver/exampletvtojson.go b/examples/9-tvtojson/exampletvtojson.go index 2c6ec7e..2c6ec7e 100644 --- a/examples/9-jsonsaver/exampletvtojson.go +++ b/examples/9-tvtojson/exampletvtojson.go diff --git a/examples/README.md b/examples/README.md index 5009adc..bd4b3d3 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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. diff --git a/jsonsaver/jsonsaver_test.go b/jsonsaver/jsonsaver_test.go index 2bff958..7992606 100644 --- a/jsonsaver/jsonsaver_test.go +++ b/jsonsaver/jsonsaver_test.go @@ -197,16 +197,13 @@ 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, }, { @@ -214,7 +211,6 @@ func TestSave2_2(t *testing.T) { args: args{ doc: &spdx.Document2_2{}, }, - wantW: "", wantErr: true, }, } diff --git a/jsonsaver/saver2v2/save_creation_info_test.go b/jsonsaver/saver2v2/save_creation_info_test.go index 592ee56..4498025 100644 --- a/jsonsaver/saver2v2/save_creation_info_test.go +++ b/jsonsaver/saver2v2/save_creation_info_test.go @@ -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) } } }) diff --git a/jsonsaver/saver2v2/save_document.go b/jsonsaver/saver2v2/save_document.go index 3fc37c7..2a0e6b7 100644 --- a/jsonsaver/saver2v2/save_document.go +++ b/jsonsaver/saver2v2/save_document.go @@ -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 @@ -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") diff --git a/jsonsaver/saver2v2/save_document_test.go b/jsonsaver/saver2v2/save_document_test.go index a890ffd..90d01f1 100644 --- a/jsonsaver/saver2v2/save_document_test.go +++ b/jsonsaver/saver2v2/save_document_test.go @@ -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", @@ -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", diff --git a/jsonsaver/saver2v2/save_files_test.go b/jsonsaver/saver2v2/save_files_test.go index d7a07c1..c2e61d8 100644 --- a/jsonsaver/saver2v2/save_files_test.go +++ b/jsonsaver/saver2v2/save_files_test.go @@ -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", @@ -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", |