-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcyclonedx_json.go
245 lines (208 loc) · 5.7 KB
/
cyclonedx_json.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// This file is part of CycloneDX Go
//
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an “AS IS” BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) OWASP Foundation. All Rights Reserved.
package cyclonedx
import (
"encoding/json"
"errors"
"fmt"
)
func (ev EnvironmentVariableChoice) MarshalJSON() ([]byte, error) {
if ev.Property != nil && *ev.Property != (Property{}) {
return json.Marshal(ev.Property)
} else if ev.Value != "" {
return json.Marshal(ev.Value)
}
return []byte("{}"), nil
}
func (ev *EnvironmentVariableChoice) UnmarshalJSON(bytes []byte) error {
var property Property
err := json.Unmarshal(bytes, &property)
if err != nil {
var ute *json.UnmarshalTypeError
if !errors.As(err, &ute) || ute.Value != "string" {
return err
}
}
if property != (Property{}) {
ev.Property = &property
return nil
}
var value string
err = json.Unmarshal(bytes, &value)
if err != nil {
var ute *json.UnmarshalTypeError
if !errors.As(err, &ute) || ute.Value != "object" {
return err
}
}
ev.Value = value
return nil
}
type mlDatasetChoiceRefJSON struct {
Ref string `json:"ref" xml:"-"`
}
func (dc MLDatasetChoice) MarshalJSON() ([]byte, error) {
if dc.Ref != "" {
return json.Marshal(mlDatasetChoiceRefJSON{Ref: dc.Ref})
} else if dc.ComponentData != nil {
return json.Marshal(dc.ComponentData)
}
return []byte("{}"), nil
}
func (dc *MLDatasetChoice) UnmarshalJSON(bytes []byte) error {
var refObj mlDatasetChoiceRefJSON
err := json.Unmarshal(bytes, &refObj)
if err != nil {
return err
}
if refObj.Ref != "" {
dc.Ref = refObj.Ref
return nil
}
var componentData ComponentData
err = json.Unmarshal(bytes, &componentData)
if err != nil {
return err
}
if componentData != (ComponentData{}) {
dc.ComponentData = &componentData
}
return nil
}
func (sv SpecVersion) MarshalJSON() ([]byte, error) {
return json.Marshal(sv.String())
}
func (sv *SpecVersion) UnmarshalJSON(bytes []byte) error {
var v string
err := json.Unmarshal(bytes, &v)
if err != nil {
return err
}
switch v {
case SpecVersion1_0.String():
*sv = SpecVersion1_0
case SpecVersion1_1.String():
*sv = SpecVersion1_1
case SpecVersion1_2.String():
*sv = SpecVersion1_2
case SpecVersion1_3.String():
*sv = SpecVersion1_3
case SpecVersion1_4.String():
*sv = SpecVersion1_4
case SpecVersion1_5.String():
*sv = SpecVersion1_5
case SpecVersion1_6.String():
*sv = SpecVersion1_6
default:
return ErrInvalidSpecVersion
}
return nil
}
type toolsChoiceJSON struct {
Components *[]Component `json:"components,omitempty" xml:"-"`
Services *[]Service `json:"services,omitempty" xml:"-"`
}
func (tc ToolsChoice) MarshalJSON() ([]byte, error) {
if tc.Tools != nil && (tc.Components != nil || tc.Services != nil) {
return nil, fmt.Errorf("either a list of tools, or an object holding components and services can be used, but not both")
}
if tc.Tools != nil {
return json.Marshal(tc.Tools)
}
choiceJSON := toolsChoiceJSON{
Components: tc.Components,
Services: tc.Services,
}
if choiceJSON.Components != nil || choiceJSON.Services != nil {
return json.Marshal(choiceJSON)
}
return []byte(nil), nil
}
func (tc *ToolsChoice) UnmarshalJSON(bytes []byte) error {
var choiceJSON toolsChoiceJSON
err := json.Unmarshal(bytes, &choiceJSON)
if err != nil {
var typeErr *json.UnmarshalTypeError
if !errors.As(err, &typeErr) || typeErr.Value != "array" {
return err
}
var legacyTools []Tool
err = json.Unmarshal(bytes, &legacyTools)
if err != nil {
return err
}
*tc = ToolsChoice{Tools: &legacyTools}
return nil
}
if choiceJSON.Components != nil || choiceJSON.Services != nil {
*tc = ToolsChoice{
Components: choiceJSON.Components,
Services: choiceJSON.Services,
}
}
return nil
}
func (ev *Evidence) UnmarshalJSON(bytes []byte) error {
type Alias Evidence
var aux = &struct {
Identity json.RawMessage `json:"identity"`
*Alias
}{
Alias: (*Alias)(ev),
}
if err := json.Unmarshal(bytes, &aux); err != nil {
return err
}
// Try to unmarshal Identity field as struct
if aux.Identity != nil {
var identity EvidenceIdentity
err := json.Unmarshal(aux.Identity, &identity)
if err != nil {
var typeErr *json.UnmarshalTypeError
if !errors.As(err, &typeErr) || typeErr.Value != "array" {
return err
}
// Try to unmarshal Identity field as array
var identities []EvidenceIdentity
err = json.Unmarshal(aux.Identity, &identities)
if err != nil {
return err
}
if len(identities) > 0 {
ev.Identity = &identities
}
return nil
}
// The field is required, so we can check for emptiness using this field.
// cf. https://cyclonedx.org/docs/1.6/json/#metadata_component_evidence_identity_oneOf_i0_items_field
if identity.Field != "" {
ev.Identity = &[]EvidenceIdentity{
identity,
}
}
}
return nil
}
var jsonSchemas = map[SpecVersion]string{
SpecVersion1_0: "",
SpecVersion1_1: "",
SpecVersion1_2: "http://cyclonedx.org/schema/bom-1.2.schema.json",
SpecVersion1_3: "http://cyclonedx.org/schema/bom-1.3.schema.json",
SpecVersion1_4: "http://cyclonedx.org/schema/bom-1.4.schema.json",
SpecVersion1_5: "http://cyclonedx.org/schema/bom-1.5.schema.json",
SpecVersion1_6: "http://cyclonedx.org/schema/bom-1.6.schema.json",
}