diff options
author | specter25 <ujjwalcoding012@gmail.com> | 2021-06-13 16:19:00 +0530 |
---|---|---|
committer | specter25 <ujjwalcoding012@gmail.com> | 2021-06-13 16:19:00 +0530 |
commit | 440a86c7f946cb3c6629be8551e21b2a43927275 (patch) | |
tree | ebd3625eff5a3be8aa3001e539fcfe429105c894 | |
parent | a0cfd0ef09eaa526e9ff4ecceaeab9933ae15b07 (diff) | |
download | spdx-tools-440a86c7f946cb3c6629be8551e21b2a43927275.tar.gz |
SPDXELementId fix in Annotations object
-Annotations SPDXElementId is equal to the SPDXID of current elemnt being parsed .
Signed-off-by: specter25 <ujjwalcoding012@gmail.com>
-rw-r--r-- | jsonloader2v2/jsonloader.go | 7 | ||||
-rw-r--r-- | jsonloader2v2/jsonloader_test.go | 33 | ||||
-rw-r--r-- | jsonloader2v2/parse_annotations.go | 4 | ||||
-rw-r--r-- | jsonloader2v2/parse_annotations_test.go | 49 |
4 files changed, 52 insertions, 41 deletions
diff --git a/jsonloader2v2/jsonloader.go b/jsonloader2v2/jsonloader.go index b655475..d027e5c 100644 --- a/jsonloader2v2/jsonloader.go +++ b/jsonloader2v2/jsonloader.go @@ -49,7 +49,11 @@ func (spec JSONSpdxDocument) newDocument(doc *spdxDocument2_2) error { return err } case "annotations": - err := spec.parseJsonAnnotations2_2(key, val, doc) + id, err := extractDocElementID(spec["SPDXID"].(string)) + if err != nil { + return fmt.Errorf("%s", err) + } + err = spec.parseJsonAnnotations2_2(key, val, doc, id) if err != nil { return err } @@ -60,7 +64,6 @@ func (spec JSONSpdxDocument) newDocument(doc *spdxDocument2_2) error { } default: return fmt.Errorf("unrecognized key here %v", key) - } } diff --git a/jsonloader2v2/jsonloader_test.go b/jsonloader2v2/jsonloader_test.go index 5ed2cef..66cae25 100644 --- a/jsonloader2v2/jsonloader_test.go +++ b/jsonloader2v2/jsonloader_test.go @@ -61,25 +61,28 @@ func TestLoad2_2(t *testing.T) { }, Annotations: []*spdx.Annotation2_2{ { - AnnotationDate: "2010-02-10T00:00:00Z", - AnnotationType: "REVIEW", - AnnotatorType: "Person", - Annotator: "Joe Reviewer", - AnnotationComment: "This is just an example. Some of the non-standard licenses look like they are actually BSD 3 clause licenses", + AnnotationSPDXIdentifier: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"}, + AnnotationDate: "2010-02-10T00:00:00Z", + AnnotationType: "REVIEW", + AnnotatorType: "Person", + Annotator: "Joe Reviewer", + AnnotationComment: "This is just an example. Some of the non-standard licenses look like they are actually BSD 3 clause licenses", }, { - AnnotationDate: "2011-03-13T00:00:00Z", - AnnotationType: "REVIEW", - AnnotatorType: "Person", - Annotator: "Suzanne Reviewer", - AnnotationComment: "Another example reviewer.", + AnnotationSPDXIdentifier: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"}, + AnnotationDate: "2011-03-13T00:00:00Z", + AnnotationType: "REVIEW", + AnnotatorType: "Person", + Annotator: "Suzanne Reviewer", + AnnotationComment: "Another example reviewer.", }, { - AnnotationDate: "2010-01-29T18:30:22Z", - AnnotationType: "OTHER", - AnnotatorType: "Person", - Annotator: "Jane Doe ()", - AnnotationComment: "Document level annotation", + AnnotationSPDXIdentifier: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"}, + AnnotationDate: "2010-01-29T18:30:22Z", + AnnotationType: "OTHER", + AnnotatorType: "Person", + Annotator: "Jane Doe ()", + AnnotationComment: "Document level annotation", }, }, Relationships: []*spdx.Relationship2_2{ diff --git a/jsonloader2v2/parse_annotations.go b/jsonloader2v2/parse_annotations.go index e233bc3..0d71964 100644 --- a/jsonloader2v2/parse_annotations.go +++ b/jsonloader2v2/parse_annotations.go @@ -9,13 +9,13 @@ import ( "github.com/spdx/tools-golang/spdx" ) -func (spec JSONSpdxDocument) parseJsonAnnotations2_2(key string, value interface{}, doc *spdxDocument2_2) error { +func (spec JSONSpdxDocument) parseJsonAnnotations2_2(key string, value interface{}, doc *spdxDocument2_2, SPDXElementId spdx.DocElementID) error { //FIXME: SPDXID property not defined in spec but it is needed if reflect.TypeOf(value).Kind() == reflect.Slice { annotations := reflect.ValueOf(value) for i := 0; i < annotations.Len(); i++ { annotation := annotations.Index(i).Interface().(map[string]interface{}) - ann := spdx.Annotation2_2{} + ann := spdx.Annotation2_2{AnnotationSPDXIdentifier: SPDXElementId} // Remove loop all properties are mandatory in annotations for k, v := range annotation { switch k { diff --git a/jsonloader2v2/parse_annotations_test.go b/jsonloader2v2/parse_annotations_test.go index b29f29d..f2858de 100644 --- a/jsonloader2v2/parse_annotations_test.go +++ b/jsonloader2v2/parse_annotations_test.go @@ -34,25 +34,28 @@ func TestJSONSpdxDocument_parseJsonAnnotations2_2(t *testing.T) { annotationstest1 := []*spdx.Annotation2_2{ { - AnnotationDate: "2010-02-10T00:00:00Z", - AnnotationType: "REVIEW", - AnnotatorType: "Person", - Annotator: "Joe Reviewer", - AnnotationComment: "This is just an example. Some of the non-standard licenses look like they are actually BSD 3 clause licenses", + AnnotationSPDXIdentifier: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"}, + AnnotationDate: "2010-02-10T00:00:00Z", + AnnotationType: "REVIEW", + AnnotatorType: "Person", + Annotator: "Joe Reviewer", + AnnotationComment: "This is just an example. Some of the non-standard licenses look like they are actually BSD 3 clause licenses", }, { - AnnotationDate: "2011-03-13T00:00:00Z", - AnnotationType: "REVIEW", - AnnotatorType: "Person", - Annotator: "Suzanne Reviewer", - AnnotationComment: "Another example reviewer.", + AnnotationSPDXIdentifier: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"}, + AnnotationDate: "2011-03-13T00:00:00Z", + AnnotationType: "REVIEW", + AnnotatorType: "Person", + Annotator: "Suzanne Reviewer", + AnnotationComment: "Another example reviewer.", }, { - AnnotationDate: "2010-01-29T18:30:22Z", - AnnotationType: "OTHER", - AnnotatorType: "Person", - Annotator: "Jane Doe ()", - AnnotationComment: "Document level annotation", + AnnotationSPDXIdentifier: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"}, + AnnotationDate: "2010-01-29T18:30:22Z", + AnnotationType: "OTHER", + AnnotatorType: "Person", + Annotator: "Jane Doe ()", + AnnotationComment: "Document level annotation", }, } @@ -60,9 +63,10 @@ func TestJSONSpdxDocument_parseJsonAnnotations2_2(t *testing.T) { json.Unmarshal(data, &specs) type args struct { - key string - value interface{} - doc *spdxDocument2_2 + key string + value interface{} + doc *spdxDocument2_2 + SPDXElementID spdx.DocElementID } tests := []struct { name string @@ -76,9 +80,10 @@ func TestJSONSpdxDocument_parseJsonAnnotations2_2(t *testing.T) { name: "successTest", spec: specs, args: args{ - key: "annotations", - value: specs["annotations"], - doc: &spdxDocument2_2{}, + key: "annotations", + value: specs["annotations"], + doc: &spdxDocument2_2{}, + SPDXElementID: spdx.DocElementID{DocumentRefID: "", ElementRefID: "DOCUMENT"}, }, want: annotationstest1, wantErr: false, @@ -86,7 +91,7 @@ func TestJSONSpdxDocument_parseJsonAnnotations2_2(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if err := tt.spec.parseJsonAnnotations2_2(tt.args.key, tt.args.value, tt.args.doc); (err != nil) != tt.wantErr { + if err := tt.spec.parseJsonAnnotations2_2(tt.args.key, tt.args.value, tt.args.doc, tt.args.SPDXElementID); (err != nil) != tt.wantErr { t.Errorf("JSONSpdxDocument.parseJsonAnnotations2_2() error = %v, wantErr %v", err, tt.wantErr) } |