-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from specter25/jsonparser1
[GSoC'21] Pr:4 . Complete the JSON parser
- Loading branch information
Showing
6 changed files
with
171 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | ||
|
||
package parser2v2 | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/spdx/tools-golang/spdx" | ||
) | ||
|
||
func (spec JSONSpdxDocument) parseJsonReviews2_2(key string, value interface{}, doc *spdxDocument2_2) error { | ||
//FIXME: Reviewer type property of review not specified in the spec | ||
if reflect.TypeOf(value).Kind() == reflect.Slice { | ||
reviews := reflect.ValueOf(value) | ||
for i := 0; i < reviews.Len(); i++ { | ||
reviewmap := reviews.Index(i).Interface().(map[string]interface{}) | ||
review := spdx.Review2_2{} | ||
// Remove loop all properties are mandatory in annotations | ||
for k, v := range reviewmap { | ||
switch k { | ||
case "reviewer": | ||
subkey, subvalue, err := extractSubs(v.(string)) | ||
if err != nil { | ||
return err | ||
} | ||
if subkey != "Person" && subkey != "Organization" && subkey != "Tool" { | ||
return fmt.Errorf("unrecognized Reviewer type %v", subkey) | ||
} | ||
review.ReviewerType = subkey | ||
review.Reviewer = subvalue | ||
case "comment": | ||
review.ReviewComment = v.(string) | ||
case "reviewDate": | ||
review.ReviewDate = v.(string) | ||
default: | ||
return fmt.Errorf("received unknown tag %v in Review Section section", k) | ||
} | ||
} | ||
doc.Reviews = append(doc.Reviews, &review) | ||
} | ||
|
||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | ||
|
||
package parser2v2 | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/spdx/tools-golang/spdx" | ||
) | ||
|
||
func TestJSONSpdxDocument_parseJsonReviews2_2(t *testing.T) { | ||
|
||
data := []byte(`{ | ||
"revieweds" : [ { | ||
"reviewDate" : "2010-02-10T00:00:00Z", | ||
"reviewer" : "Person: Joe Reviewer", | ||
"comment" : "This is just an example. Some of the non-standard licenses look like they are actually BSD 3 clause licenses" | ||
}, { | ||
"reviewDate" : "2011-03-13T00:00:00Z", | ||
"reviewer" : "Person: Suzanne Reviewer", | ||
"comment" : "Another example reviewer." | ||
}] | ||
} | ||
`) | ||
|
||
reviewstest1 := []*spdx.Review2_2{ | ||
{ | ||
ReviewDate: "2010-02-10T00:00:00Z", | ||
ReviewerType: "Person", | ||
Reviewer: "Joe Reviewer", | ||
ReviewComment: "This is just an example. Some of the non-standard licenses look like they are actually BSD 3 clause licenses", | ||
}, | ||
{ | ||
ReviewDate: "2011-03-13T00:00:00Z", | ||
ReviewerType: "Person", | ||
Reviewer: "Suzanne Reviewer", | ||
ReviewComment: "Another example reviewer.", | ||
}, | ||
} | ||
|
||
var specs JSONSpdxDocument | ||
json.Unmarshal(data, &specs) | ||
|
||
type args struct { | ||
key string | ||
value interface{} | ||
doc *spdxDocument2_2 | ||
SPDXElementID spdx.DocElementID | ||
} | ||
tests := []struct { | ||
name string | ||
spec JSONSpdxDocument | ||
args args | ||
want []*spdx.Review2_2 | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
name: "successTest", | ||
spec: specs, | ||
args: args{ | ||
key: "revieweds", | ||
value: specs["revieweds"], | ||
doc: &spdxDocument2_2{}, | ||
}, | ||
want: reviewstest1, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := tt.spec.parseJsonReviews2_2(tt.args.key, tt.args.value, tt.args.doc); (err != nil) != tt.wantErr { | ||
t.Errorf("JSONSpdxDocument.parseJsonAnnotations2_2() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
|
||
for i := 0; i < len(tt.want); i++ { | ||
if !reflect.DeepEqual(tt.args.doc.Reviews[i], tt.want[i]) { | ||
t.Errorf("Load2_2() = %v, want %v", tt.args.doc.Reviews[i], tt.want[i]) | ||
} | ||
} | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters