-
Notifications
You must be signed in to change notification settings - Fork 18
/
normalization.go
131 lines (109 loc) · 2.66 KB
/
normalization.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package signing
import (
"encoding/json"
"github.com/open-component-model/ocm/pkg/errors"
)
type null struct{}
func (n *null) IsEmpty() bool {
return true
}
func (n *null) Marshal(gap string) ([]byte, error) {
return json.Marshal(nil)
}
func (n *null) ToString(gap string) string {
return n.String()
}
func (n *null) String() string {
return "null"
}
func (n *null) Formatted() string {
return n.String()
}
func (n *null) Append(normalized Normalized) {
panic("append on null")
}
func (n *null) Value() interface{} {
return nil
}
func (n *null) SetField(name string, value Normalized) {
panic("set field on null")
}
var Null Normalized = (*null)(nil)
func PrepareNormalization(n Normalization, v interface{}, excludes ExcludeRules) (Normalized, error) {
data, err := json.Marshal(v)
if err != nil {
return nil, err
}
var raw map[string]interface{}
err = json.Unmarshal(data, &raw)
if err != nil {
return nil, err
}
return prepareStruct(n, raw, excludes)
}
func Prepare(n Normalization, v interface{}, ex ExcludeRules) (r Normalized, err error) {
if m, ok := ex.(ValueMappingRule); ok {
v = m.MapValue(v)
}
switch e := v.(type) {
case map[string]interface{}:
r, err = prepareStruct(n, e, ex)
case []interface{}:
r, err = prepareArray(n, e, ex)
default:
return n.NewValue(v), nil
}
if err != nil {
return r, err
}
if f, ok := ex.(NormalizationFilter); ok {
return f.Filter(r)
}
return r, err
}
func prepareStruct(n Normalization, v map[string]interface{}, ex ExcludeRules) (Normalized, error) {
entries := n.NewMap()
for key, value := range v {
name, mapped, prop := ex.Field(key, value)
if name != "" {
nested, err := Prepare(n, mapped, prop)
if err != nil {
return nil, errors.Wrapf(err, "field %q", key)
}
if nested != nil {
if nested == Null {
entries.SetField(name, nil)
} else {
entries.SetField(name, nested)
}
}
}
}
return entries, nil
}
func prepareArray(n Normalization, v []interface{}, ex ExcludeRules) (Normalized, error) {
entries := n.NewArray()
for index, value := range v {
exclude, mapped, prop := ex.Element(value)
if !exclude {
nested, err := Prepare(n, mapped, prop)
if err != nil {
return nil, errors.Wrapf(err, "entry %d", index)
}
if nested != nil {
entries.Append(nested)
}
}
}
return entries, nil
}
func Normalize(n Normalization, v interface{}, ex ExcludeRules) ([]byte, error) {
entries, err := PrepareNormalization(n, v, ex)
if err != nil {
return nil, err
}
return entries.Marshal("")
}