This repository has been archived by the owner on Jun 2, 2020. It is now read-only.
/
properties.go
96 lines (75 loc) · 2.1 KB
/
properties.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
package ksonnet
import (
"strings"
"github.com/go-openapi/spec"
"github.com/pkg/errors"
)
var (
recursiveRefs = []string{
"io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1beta1.JSONSchemaProps",
"io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1.JSONSchemaProps",
}
)
func extractProperties(c *Catalog, properties map[string]spec.Schema, required []string) (map[string]Property, error) {
if c == nil {
return nil, errors.New("catalog is nil")
}
out := make(map[string]Property)
for name, schema := range properties {
if isSkippedProperty(name, schema) {
if !stringInSlice(name, required) {
continue
}
}
ref := extractRef(schema)
if ref != "" && stringInSlice(ref, recursiveRefs) {
out[name] = NewLiteralField(name, "object", schema.Description, ref)
continue
}
// literal
if t := schema.Type; len(t) == 1 {
out[name] = buildLiteralField(t[0], name, schema)
continue
}
ifr, err := c.isFormatRef(ref)
if err != nil {
return nil, errors.Wrap(err, "check for format ref")
}
if ifr {
// don't have to check for existence here because isFormatRef does the same thing
formatSchema := c.apiSpec.Definitions[ref]
out[name] = buildLiteralField(fieldType(formatSchema), name, schema)
continue
}
// must be a mixin
f := NewReferenceField(name, schema.Description, ref)
out[name] = f
}
return out, nil
}
func buildLiteralField(fieldType, name string, schema spec.Schema) *LiteralField {
var itemRef string
if schema.Items != nil && schema.Items.Schema != nil {
itemRef = extractRef(*schema.Items.Schema)
}
return NewLiteralField(name, fieldType, schema.Description, itemRef)
}
func isSkippedProperty(name string, schema spec.Schema) bool {
if stringInSlice(name, blockedPropertyNames) {
return true
}
if strings.Contains(strings.ToLower(schema.Description), "read-only") && name != "readOnly" {
return true
}
ref := extractRef(schema)
if stringInSlice(ref, blockedReferences) {
return true
}
return false
}
func fieldType(schema spec.Schema) string {
if t := schema.Type; len(t) == 1 {
return t[0]
}
return ""
}