aboutsummaryrefslogtreecommitdiff
path: root/jsonsaver/saver2v2/save_creation_info.go
blob: 60f4e95323ae0fd3f48eac071a3b98dafe861b8f (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
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

package saver2v2

import (
	"bytes"
	"encoding/json"
	"fmt"

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

func renderCreationInfo2_2(ci *spdx.CreationInfo2_2, buf *bytes.Buffer) error {
	if ci.SPDXIdentifier != "" {
		fmt.Fprintf(buf, "\"%s\": \"%s\",", "SPDXID", spdx.RenderElementID(ci.SPDXIdentifier))
	}
	if ci.SPDXVersion != "" {
		fmt.Fprintf(buf, "\"%s\": \"%s\",", "spdxVersion", ci.SPDXVersion)
	}
	if ci.CreatorComment != "" || ci.Created != "" || ci.CreatorPersons != nil || ci.CreatorOrganizations != nil || ci.CreatorTools != nil || ci.LicenseListVersion != "" {
		fmt.Fprintf(buf, "\"%s\": %s", "creationInfo", "{")
		if ci.CreatorComment != "" {
			commentjson, _ := json.Marshal(ci.CreatorComment)
			fmt.Fprintf(buf, "\"%s\": %s,", "comment", commentjson)
		}
		if ci.Created != "" {
			fmt.Fprintf(buf, "\"%s\": \"%s\",", "created", ci.Created)
		}
		if ci.CreatorPersons != nil || ci.CreatorOrganizations != nil || ci.CreatorTools != nil {
			var creators []string
			for _, v := range ci.CreatorPersons {
				creators = append(creators, fmt.Sprintf("Person: %s", v))
			}
			for _, v := range ci.CreatorOrganizations {
				creators = append(creators, fmt.Sprintf("Organization: %s", v))
			}
			for _, v := range ci.CreatorTools {
				creators = append(creators, fmt.Sprintf("Tool: %s", v))
			}
			creatorsjson, _ := json.Marshal(creators)
			fmt.Fprintf(buf, "\"%s\": %s ,", "creators", creatorsjson)
		}
		if ci.LicenseListVersion != "" {
			fmt.Fprintf(buf, "\"%s\": \"%s\",", "licenseListVersion", ci.LicenseListVersion)
		}
		fmt.Fprintf(buf, "%s", "},")
	}
	if ci.DocumentName != "" {
		fmt.Fprintf(buf, "\"%s\": \"%s\",", "name", ci.DocumentName)
	}
	if ci.DataLicense != "" {
		fmt.Fprintf(buf, "\"%s\": \"%s\",", "dataLicense", ci.DataLicense)
	}
	if ci.DocumentComment != "" {
		fmt.Fprintf(buf, "\"%s\": \"%s\",", "comment", ci.DocumentComment)
	}
	if ci.ExternalDocumentReferences != nil {
		var refs []interface{}
		for _, v := range ci.ExternalDocumentReferences {
			aa := make(map[string]interface{})
			aa["externalDocumentId"] = fmt.Sprintf("DocumentRef-%s", v.DocumentRefID)
			aa["checksum"] = map[string]string{
				"algorithm":     v.Alg,
				"checksumValue": v.Checksum,
			}
			aa["spdxDocument"] = v.URI
			refs = append(refs, aa)
		}
		refsjson, _ := json.Marshal(refs)
		fmt.Fprintf(buf, "\"%s\": %s ,", "externalDocumentRefs", refsjson)
	}

	return nil
}