forked from kubeflow/crd-validation
/
convert_types.go
133 lines (119 loc) · 5.14 KB
/
convert_types.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
package utils
import (
"fmt"
"k8s.io/apimachinery/pkg/util/sets"
spec "github.com/go-openapi/spec"
extensionsobj "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
common "k8s.io/kube-openapi/pkg/common"
)
var visitedRefStringSet = sets.String{}
// SchemaPropsToJSONPropsArray converts []Schema to []JSONSchemaProps
func SchemaPropsToJSONPropsArray(schemas []spec.Schema, openapiSpec map[string]common.OpenAPIDefinition, nested bool) []extensionsobj.JSONSchemaProps {
var s []extensionsobj.JSONSchemaProps
for _, schema := range schemas {
s = append(s, *SchemaPropsToJSONProps(&schema, openapiSpec, nested))
}
return s
}
// StringOrArrayToString converts StringOrArray to string
func StringOrArrayToString(strOrArray spec.StringOrArray) string {
if len(strOrArray) > 0 {
return strOrArray[0]
}
return ""
}
// EnumJSON converts []interface{} to []JSON
func EnumJSON(enum []interface{}) []extensionsobj.JSON {
var s []extensionsobj.JSON
for _, elt := range enum {
s = append(s, extensionsobj.JSON{
Raw: []byte(fmt.Sprintf("%v", elt)),
})
}
return s
}
// SchemaOrArrayToJSONItems converts *SchemaOrArray to *JSONSchemaPropsOrArray
func SchemaOrArrayToJSONItems(schemaOrArray *spec.SchemaOrArray, openapiSpec map[string]common.OpenAPIDefinition, nested bool) *extensionsobj.JSONSchemaPropsOrArray {
var array *extensionsobj.JSONSchemaPropsOrArray
if schemaOrArray == nil {
return array
}
return &extensionsobj.JSONSchemaPropsOrArray{
Schema: SchemaPropsToJSONProps(schemaOrArray.Schema, openapiSpec, nested),
JSONSchemas: SchemaPropsToJSONPropsArray(schemaOrArray.Schemas, openapiSpec, nested),
}
}
// SchemaOrBoolToJSONProps converts *SchemaOrBool to *JSONSchemaPropsOrBool
func SchemaOrBoolToJSONProps(schemaOrBool *spec.SchemaOrBool, openapiSpec map[string]common.OpenAPIDefinition, nested bool) *extensionsobj.JSONSchemaPropsOrBool {
var s *extensionsobj.JSONSchemaPropsOrBool
if schemaOrBool == nil {
return s
}
return &extensionsobj.JSONSchemaPropsOrBool{
Schema: SchemaPropsToJSONProps(schemaOrBool.Schema, openapiSpec, nested),
Allows: schemaOrBool.Allows,
}
}
// SchemPropsMapToJSONMap converts map[string]Schema to map[string]JSONSchemaProps
func SchemPropsMapToJSONMap(schemaMap map[string]spec.Schema, openapiSpec map[string]common.OpenAPIDefinition, nested bool) map[string]extensionsobj.JSONSchemaProps {
var m map[string]extensionsobj.JSONSchemaProps
m = make(map[string]extensionsobj.JSONSchemaProps)
for key, schema := range schemaMap {
m[key] = *SchemaPropsToJSONProps(&schema, openapiSpec, nested)
}
return m
}
// SchemaPropsToJSONProps converts a SchemaProps to a JSONProps
func SchemaPropsToJSONProps(schema *spec.Schema, openapiSpec map[string]common.OpenAPIDefinition, nested bool) *extensionsobj.JSONSchemaProps {
var props *extensionsobj.JSONSchemaProps
if schema == nil {
return props
}
schemaProps := &schema.SchemaProps
var ref *string
if refString := schemaProps.Ref.String(); refString != "" && !visitedRefStringSet.Has(refString) {
visitedRefStringSet.Insert(refString)
if nested {
propref := openapiSpec[refString].Schema
// If nested just return a pointer to the reference
return SchemaPropsToJSONProps(&propref, openapiSpec, nested)
}
ref = new(string)
*ref = refString
}
props = &extensionsobj.JSONSchemaProps{
Ref: ref,
ID: schemaProps.ID,
Schema: extensionsobj.JSONSchemaURL(string(schema.Schema)),
Description: schemaProps.Description,
Type: StringOrArrayToString(schemaProps.Type),
Format: schemaProps.Format,
Title: schemaProps.Title,
Maximum: schemaProps.Maximum,
ExclusiveMaximum: schemaProps.ExclusiveMaximum,
Minimum: schemaProps.Minimum,
ExclusiveMinimum: schemaProps.ExclusiveMinimum,
MaxLength: schemaProps.MaxLength,
MinLength: schemaProps.MinLength,
Pattern: schemaProps.Pattern,
MaxItems: schemaProps.MaxItems,
MinItems: schemaProps.MinItems,
UniqueItems: schemaProps.UniqueItems,
MultipleOf: schemaProps.MultipleOf,
Enum: EnumJSON(schemaProps.Enum),
MaxProperties: schemaProps.MaxProperties,
MinProperties: schemaProps.MinProperties,
Required: schemaProps.Required,
Items: SchemaOrArrayToJSONItems(schemaProps.Items, openapiSpec, nested),
AllOf: SchemaPropsToJSONPropsArray(schemaProps.AllOf, openapiSpec, nested),
OneOf: SchemaPropsToJSONPropsArray(schemaProps.OneOf, openapiSpec, nested),
AnyOf: SchemaPropsToJSONPropsArray(schemaProps.AnyOf, openapiSpec, nested),
Not: SchemaPropsToJSONProps(schemaProps.Not, openapiSpec, nested),
Properties: SchemPropsMapToJSONMap(schemaProps.Properties, openapiSpec, nested),
// @TODO(01-25-2018) Field not accepted by the current CRD Validation Spec
AdditionalProperties: SchemaOrBoolToJSONProps(schemaProps.AdditionalProperties, openapiSpec, nested),
PatternProperties: SchemPropsMapToJSONMap(schemaProps.PatternProperties, openapiSpec, nested),
AdditionalItems: SchemaOrBoolToJSONProps(schemaProps.AdditionalItems, openapiSpec, nested),
}
return props
}