-
Notifications
You must be signed in to change notification settings - Fork 10
/
compat.go
82 lines (78 loc) · 2.44 KB
/
compat.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
package models
import (
"encoding/json"
"fmt"
"sort"
)
// Compatibility type to unmarshal controls in the old (map-based) as well
// as the new (array-based) format.
type ControlsParser struct {
Controls []string
}
func (r *ControlsParser) UnmarshalJSON(data []byte) error {
old := map[string]map[string][]string{}
controls := []string{}
if err := json.Unmarshal(data, &old); err == nil {
families := []string{}
for family := range old {
families = append(families, family)
}
sort.Strings(families)
for _, family := range families {
versions := []string{}
for version := range old[family] {
versions = append(versions, version)
}
sort.Strings(versions)
for _, version := range versions {
for _, section := range old[family][version] {
control := fmt.Sprintf("%s_%s_%s", family, version, section)
controls = append(controls, control)
}
}
}
r.Controls = controls
return nil
} else {
if err := json.Unmarshal(data, &controls); err != nil {
return err
} else {
r.Controls = controls
return nil
}
}
}
func (r *RuleResults) UnmarshalJSON(data []byte) error {
compat := struct {
Id string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Platform []string `json:"platform,omitempty"`
Description string `json:"description,omitempty"`
References []RuleResultsReference `json:"references,omitempty"`
Category string `json:"category,omitempty"`
Labels []string `json:"labels,omitempty"`
ServiceGroup string `json:"service_group,omitempty"`
Controls ControlsParser `json:"controls"`
ResourceTypes []string `json:"resource_types,omitempty"`
Results []RuleResult `json:"results"`
Errors []string `json:"errors,omitempty"`
Package_ string `json:"package,omitempty"`
}{}
if err := json.Unmarshal(data, &compat); err != nil {
return err
}
r.Id = compat.Id
r.Title = compat.Title
r.Platform = compat.Platform
r.Description = compat.Description
r.References = compat.References
r.Category = compat.Category
r.Labels = compat.Labels
r.ServiceGroup = compat.ServiceGroup
r.Controls = compat.Controls.Controls
r.ResourceTypes = compat.ResourceTypes
r.Results = compat.Results
r.Errors = compat.Errors
r.Package_ = compat.Package_
return nil
}