Skip to content

Commit

Permalink
Merge pull request #83 from specter25/jsonparser1
Browse files Browse the repository at this point in the history
[GSoC'21] Pr:3 . bugfixes in json parser 2v2
  • Loading branch information
swinslow committed Jun 23, 2021
2 parents ca9e0d9 + 0a0bb1f commit 95b9024
Show file tree
Hide file tree
Showing 26 changed files with 182 additions and 73 deletions.
13 changes: 7 additions & 6 deletions examples/8-jsonloader/examplejsontotv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"os"

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

Expand All @@ -29,13 +27,16 @@ func main() {
}

// open the SPDX file
jsonData, err := ioutil.ReadFile(args[1]) // jsondata has type []byte
fileIn := args[1]
r, err := os.Open(fileIn)
if err != nil {
log.Fatal(err)
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 := jsonloader2v2.Load2_2(jsonData)
doc, err := jsonloader.Load2_2(r)
if err != nil {
fmt.Printf("Error while parsing %v: %v", args[1], err)
return
Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ and then printing the corresponding spdx struct for the document.

## 8-jsonloader

*jsonloader2v2*, *tvsaver*
*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.
24 changes: 24 additions & 0 deletions jsonloader/jsonloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader

import (
"bytes"
"io"

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

// Takes in a file Reader and returns the pertaining spdx document
// or the error if any is encountered while setting the doc.
func Load2_2(content io.Reader) (*spdx.Document2_2, error) {
//convert io.Reader to a slice of bytes and call the parser
buf := new(bytes.Buffer)
buf.ReadFrom(content)
var doc, err = parser2v2.Load2_2(buf.Bytes())
if err != nil {
return nil, err
}
return doc, nil
}
61 changes: 61 additions & 0 deletions jsonloader/jsonloader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader

import (
"fmt"
"io"
"os"
"reflect"
"testing"

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

func TestLoad2_2(t *testing.T) {

file, err := os.Open("./parser2v2/jsonfiles/jsonloadertest.json")
if err != nil {
panic(fmt.Errorf("error opening File: %s", err))
}

type args struct {
content io.Reader
}
tests := []struct {
name string
args args
want *spdx.Document2_2
wantErr bool
}{
// TODO: Add test cases.
{
name: "success test",
args: args{
content: file,
},
want: &spdx.Document2_2{
CreationInfo: &spdx.CreationInfo2_2{
DataLicense: "CC0-1.0",
SPDXVersion: "SPDX-2.2",
SPDXIdentifier: "DOCUMENT",
DocumentName: "SPDX-Tools-v2.0",
ExternalDocumentReferences: make(map[string]spdx.ExternalDocumentRef2_2),
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Load2_2(tt.args.content)
if (err != nil) != tt.wantErr {
t.Errorf("Load2_2() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got.CreationInfo, tt.want.CreationInfo) {
t.Errorf("Load2_2() = %v, want %v", got.CreationInfo, tt.want.CreationInfo)
}
})
}
}
6 changes: 6 additions & 0 deletions jsonloader/parser2v2/jsonfiles/jsonloadertest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"SPDXID" : "SPDXRef-DOCUMENT",
"spdxVersion" : "SPDX-2.2",
"name" : "SPDX-Tools-v2.0",
"dataLicense" : "CC0-1.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"comment" : "Document level annotation"
} ],
"documentNamespace" : "http://spdx.org/spdxdocs/spdx-example-444504E0-4F89-41D3-9A0C-0305E82C3301",
"documentDescribes" : [ "SPDXRef-File", "SPDXRef-Package" ],
"packages" : [ {
"SPDXID" : "SPDXRef-Package",
"annotations" : [ {
Expand Down Expand Up @@ -261,11 +262,11 @@
"spdxElementId" : "SPDXRef-Package",
"relatedSpdxElement" : "SPDXRef-JenaLib",
"relationshipType" : "CONTAINS"
},{
}, {
"spdxElementId" : "SPDXRef-CommonsLangSrc",
"relatedSpdxElement" : "NOASSERTION",
"relationshipType" : "GENERATED_FROM"
} ,{
}, {
"spdxElementId" : "SPDXRef-JenaLib",
"relatedSpdxElement" : "SPDXRef-Package",
"relationshipType" : "CONTAINS"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"reflect"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"fmt"
Expand Down Expand Up @@ -138,15 +138,21 @@ func (spec JSONSpdxDocument) parseJsonPackages2_2(key string, value interface{},
pkg.PackageFileName = v.(string)
case "packageVerificationCode":
code := v.(map[string]interface{})
if reflect.TypeOf(code["packageVerificationCodeExcludedFiles"]).Kind() == reflect.Slice {
efiles := reflect.ValueOf(code["packageVerificationCodeExcludedFiles"])
_, filename, err := extractSubs(efiles.Index(0).Interface().(string))
if err != nil {
return fmt.Errorf("%s", err)
for codekey, codeval := range code {
switch codekey {
case "packageVerificationCodeExcludedFiles":
if reflect.TypeOf(codeval).Kind() == reflect.Slice {
efiles := reflect.ValueOf(codeval)
_, filename, err := extractSubs(efiles.Index(0).Interface().(string))
if err != nil {
return fmt.Errorf("%s", err)
}
pkg.PackageVerificationCodeExcludedFile = filename
}
case "packageVerificationCodeValue":
pkg.PackageVerificationCode = code["packageVerificationCodeValue"].(string)
}
pkg.PackageVerificationCodeExcludedFile = filename
}
pkg.PackageVerificationCode = code["packageVerificationCodeValue"].(string)
case "sourceInfo":
pkg.PackageSourceInfo = v.(string)
case "summary":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"fmt"
Expand Down Expand Up @@ -57,29 +57,30 @@ func (spec JSONSpdxDocument) parseJsonSnippets2_2(key string, value interface{},
if reflect.TypeOf(v).Kind() == reflect.Slice {
info := reflect.ValueOf(v)
lineRanges := info.Index(0).Interface().(map[string]interface{})
lineRangeStart := lineRanges["endPointer"].(map[string]interface{})
lineRangeEnd := lineRanges["startPointer"].(map[string]interface{})
lineRangeStart := lineRanges["startPointer"].(map[string]interface{})
lineRangeEnd := lineRanges["endPointer"].(map[string]interface{})
snippet.SnippetLineRangeStart = int(lineRangeStart["lineNumber"].(float64))
snippet.SnippetLineRangeEnd = int(lineRangeEnd["lineNumber"].(float64))

byteRanges := info.Index(1).Interface().(map[string]interface{})
byteRangeStart := byteRanges["endPointer"].(map[string]interface{})
byteRangeEnd := byteRanges["startPointer"].(map[string]interface{})
snippet.SnippetLineRangeStart = int(byteRangeStart["offset"].(float64))
snippet.SnippetLineRangeEnd = int(byteRangeEnd["offset"].(float64))
byteRangeStart := byteRanges["startPointer"].(map[string]interface{})
byteRangeEnd := byteRanges["endPointer"].(map[string]interface{})
snippet.SnippetByteRangeStart = int(byteRangeStart["offset"].(float64))
snippet.SnippetByteRangeEnd = int(byteRangeEnd["offset"].(float64))
}
default:
return fmt.Errorf("received unknown tag %v in files section", k)
}
}
fileID, err2 := extractElementID(snippetmap["snippetFromFile"].(string))
fileID, err2 := extractDocElementID(snippetmap["snippetFromFile"].(string))
if err2 != nil {
return fmt.Errorf("%s", err2)
}
if doc.UnpackagedFiles[fileID].Snippets == nil {
doc.UnpackagedFiles[fileID].Snippets = make(map[spdx.ElementID]*spdx.Snippet2_2)
snippet.SnippetFromFileSPDXIdentifier = fileID
if doc.UnpackagedFiles[fileID.ElementRefID].Snippets == nil {
doc.UnpackagedFiles[fileID.ElementRefID].Snippets = make(map[spdx.ElementID]*spdx.Snippet2_2)
}
doc.UnpackagedFiles[fileID].Snippets[eID] = snippet
doc.UnpackagedFiles[fileID.ElementRefID].Snippets[eID] = snippet
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"encoding/json"
"log"
"reflect"
"testing"

"github.com/spdx/tools-golang/spdx"
Expand Down Expand Up @@ -50,17 +51,18 @@ func TestJSONSpdxDocument_parseJsonSnippets2_2(t *testing.T) {
FileChecksums: map[spdx.ChecksumAlgorithm]spdx.Checksum{},
Snippets: map[spdx.ElementID]*spdx.Snippet2_2{
"Snippet": {
SnippetSPDXIdentifier: "Snippet",
SnippetComment: "This snippet was identified as significant and highlighted in this Apache-2.0 file, when a commercial scanner identified it as being derived from file foo.c in package xyz which is licensed under GPL-2.0.",
SnippetCopyrightText: "Copyright 2008-2010 John Smith",
SnippetLicenseComments: "The concluded license was taken from package xyz, from which the snippet was copied into the current file. The concluded license information was found in the COPYING.txt file in package xyz.",
SnippetLicenseConcluded: "GPL-2.0-only",
LicenseInfoInSnippet: []string{"GPL-2.0-only"},
SnippetName: "from linux kernel",
SnippetByteRangeStart: 310,
SnippetByteRangeEnd: 420,
SnippetLineRangeStart: 5,
SnippetLineRangeEnd: 23,
SnippetSPDXIdentifier: "Snippet",
SnippetFromFileSPDXIdentifier: spdx.DocElementID{ElementRefID: "DoapSource"},
SnippetComment: "This snippet was identified as significant and highlighted in this Apache-2.0 file, when a commercial scanner identified it as being derived from file foo.c in package xyz which is licensed under GPL-2.0.",
SnippetCopyrightText: "Copyright 2008-2010 John Smith",
SnippetLicenseComments: "The concluded license was taken from package xyz, from which the snippet was copied into the current file. The concluded license information was found in the COPYING.txt file in package xyz.",
SnippetLicenseConcluded: "GPL-2.0-only",
LicenseInfoInSnippet: []string{"GPL-2.0-only"},
SnippetName: "from linux kernel",
SnippetByteRangeStart: 310,
SnippetByteRangeEnd: 420,
SnippetLineRangeStart: 5,
SnippetLineRangeEnd: 23,
},
},
}
Expand Down Expand Up @@ -107,6 +109,13 @@ func TestJSONSpdxDocument_parseJsonSnippets2_2(t *testing.T) {
if err := tt.spec.parseJsonSnippets2_2(tt.args.key, tt.args.value, tt.args.doc); (err != nil) != tt.wantErr {
t.Errorf("JSONSpdxDocument.parseJsonSnippets2_2() error = %v, wantErr %v", err, tt.wantErr)
}

for k, v := range tt.want.Snippets {
if !reflect.DeepEqual(tt.args.doc.UnpackagedFiles["DoapSource"].Snippets[k], v) {
t.Errorf("Load2_2() = %v, want %v", tt.args.doc.UnpackagedFiles["DoapSource"].Snippets[k], v)
}
}

})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// into tools-golang data structures.
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package jsonloader2v2
package parser2v2

import (
"encoding/json"
Expand Down

0 comments on commit 95b9024

Please sign in to comment.