-
Notifications
You must be signed in to change notification settings - Fork 3
/
convert.go
174 lines (137 loc) · 3.86 KB
/
convert.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
// Package convert is the package that contains the convert functionality.
package convert
import (
"errors"
"github.com/aiven/aiven-go-client"
"github.com/mitchellh/copystructure"
"golang.org/x/exp/slices"
"github.com/aiven/go-api-schemas/internal/pkg/types"
)
// errUnexpected is the error that is returned when an unexpected error occurs.
var errUnexpected = errors.New("unexpected conversion error")
// maxSafeNumber the last number in double precision floating point that can make valid comparison
const maxSafeNumber = float64(1<<53 - 1)
// UserConfigSchema converts aiven.UserConfigSchema to UserConfigSchema.
// nolint:funlen,gocognit // This function is long, but it's a conversion function.
// // This function is complex, but it's a conversion function.
func UserConfigSchema(v aiven.UserConfigSchema) (*types.UserConfigSchema, error) {
var r []string
r = append(r, v.Required...)
var cnp map[string]types.UserConfigSchema
if len(v.Properties) != 0 {
cnp = make(map[string]types.UserConfigSchema, len(v.Properties))
p, err := copystructure.Copy(v.Properties)
if err != nil {
return nil, err
}
ap, ok := p.(map[string]aiven.UserConfigSchema)
if !ok {
return nil, errUnexpected
}
var cv *types.UserConfigSchema
for k, v := range ap {
if isImmutableObject(v) {
continue
}
cv, err = UserConfigSchema(v)
if err != nil {
return nil, err
}
cnp[k] = *cv
}
}
var cni *types.UserConfigSchema
if v.Items != nil {
var err error
cni, err = UserConfigSchema(*v.Items)
if err != nil {
return nil, err
}
}
var cno []types.UserConfigSchema
if len(v.OneOf) != 0 {
cno = make([]types.UserConfigSchema, len(v.OneOf))
o, err := copystructure.Copy(v.OneOf)
if err != nil {
return nil, err
}
ao, ok := o.([]aiven.UserConfigSchema)
if !ok {
return nil, errUnexpected
}
var cv *types.UserConfigSchema
for i, v := range ao {
cv, err = UserConfigSchema(v)
if err != nil {
return nil, err
}
cno[i] = *cv
}
}
e := make([]types.UserConfigSchemaEnumValue, 0, len(v.Enum))
for _, v := range v.Enum {
e = append(e, types.UserConfigSchemaEnumValue{Value: v})
}
// YAML uses scientific notation for floats, they won't change that
// https://github.com/go-yaml/yaml/issues/669
var max *float64
if v.Maximum != nil {
// If this is an integer it has to be lte maxSafeNumber
// Otherwise, uses it as is
if !slices.Contains(normalizeTypes(v.Type), "integer") || *v.Maximum <= maxSafeNumber {
max = v.Maximum
}
}
return &types.UserConfigSchema{
Title: v.Title,
Description: v.Description,
Type: v.Type,
Default: v.Default,
Required: r,
Properties: cnp,
Items: cni,
OneOf: cno,
Enum: e,
Minimum: v.Minimum,
Maximum: max,
MinLength: v.MinLength,
MaxLength: v.MaxLength,
MaxItems: v.MaxItems,
CreateOnly: v.CreateOnly,
Pattern: v.Pattern,
Example: v.Example,
UserError: v.UserError,
}, nil
}
// normalizeTypes json type field can be a string or list of strings.
// Turns into a slice of strings
func normalizeTypes(t any) []string {
s, ok := t.(string)
if ok {
return []string{s}
}
typeList := make([]string, 0)
a, ok := t.([]any)
if !ok {
return typeList
}
for _, v := range a {
if vv, ok := v.(string); ok {
typeList = append(typeList, vv)
}
}
return typeList
}
// isImmutableObject Ignores immutable objects.
// Returns true if the object has no properties and does not allow additional properties.
// Ignores `patternProperties`.
func isImmutableObject(u aiven.UserConfigSchema) bool {
// An object with empty properties
t, ok := u.Type.(string)
if !(ok && t == "object" && len(u.Properties) == 0) {
return false
}
// Either no additional properties allowed or it is nil
allowed, ok := u.AdditionalProperties.(bool)
return ok != allowed || u.AdditionalProperties == nil
}