aboutsummaryrefslogtreecommitdiff
path: root/jsonloader/parser2v2/parse_snippets.go
blob: a49191d7f8c577677b5552ccf744a495d58eee78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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) parseJsonSnippets2_2(key string, value interface{}, doc *spdxDocument2_2) error {

	if reflect.TypeOf(value).Kind() == reflect.Slice {
		snippets := reflect.ValueOf(value)
		for i := 0; i < snippets.Len(); i++ {
			snippetmap := snippets.Index(i).Interface().(map[string]interface{})
			// create a new package
			snippet := &spdx.Snippet2_2{}
			//extract the SPDXID of the package
			eID, err := extractElementID(snippetmap["SPDXID"].(string))
			if err != nil {
				return fmt.Errorf("%s", err)
			}
			snippet.SnippetSPDXIdentifier = eID
			//range over all other properties now
			for k, v := range snippetmap {
				switch k {
				case "SPDXID", "snippetFromFile":
					//redundant case
				case "name":
					snippet.SnippetName = v.(string)
				case "copyrightText":
					snippet.SnippetCopyrightText = v.(string)
				case "licenseComments":
					snippet.SnippetLicenseComments = v.(string)
				case "licenseConcluded":
					snippet.SnippetLicenseConcluded = v.(string)
				case "licenseInfoInSnippets":
					if reflect.TypeOf(v).Kind() == reflect.Slice {
						info := reflect.ValueOf(v)
						for i := 0; i < info.Len(); i++ {
							snippet.LicenseInfoInSnippet = append(snippet.LicenseInfoInSnippet, info.Index(i).Interface().(string))
						}
					}
				case "attributionTexts":
					if reflect.TypeOf(v).Kind() == reflect.Slice {
						info := reflect.ValueOf(v)
						for i := 0; i < info.Len(); i++ {
							snippet.SnippetAttributionTexts = append(snippet.SnippetAttributionTexts, info.Index(i).Interface().(string))
						}
					}
				case "comment":
					snippet.SnippetComment = v.(string)
				case "ranges":
					//TODO: optimise this logic
					if reflect.TypeOf(v).Kind() == reflect.Slice {
						info := reflect.ValueOf(v)
						for i := 0; i < info.Len(); i++ {
							ranges := info.Index(i).Interface().(map[string]interface{})
							rangeStart := ranges["startPointer"].(map[string]interface{})
							rangeEnd := ranges["endPointer"].(map[string]interface{})
							if rangeStart["lineNumber"] != nil && rangeEnd["lineNumber"] != nil {
								snippet.SnippetLineRangeStart = int(rangeStart["lineNumber"].(float64))
								snippet.SnippetLineRangeEnd = int(rangeEnd["lineNumber"].(float64))
							} else {
								snippet.SnippetByteRangeStart = int(rangeStart["offset"].(float64))
								snippet.SnippetByteRangeEnd = int(rangeEnd["offset"].(float64))
							}
						}
					}
				default:
					return fmt.Errorf("received unknown tag %v in snippet section", k)
				}
			}
			fileID, err2 := extractDocElementID(snippetmap["snippetFromFile"].(string))
			if err2 != nil {
				return fmt.Errorf("%s", err2)
			}
			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.ElementRefID].Snippets[eID] = snippet
		}

	}
	return nil
}