Created
April 24, 2019 14:37
-
-
Save sttts/a96a0ba8e423131079d29bd7a563008d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2019 The Kubernetes Authors. | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
package schema | |
import ( | |
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" | |
) | |
type Structural struct { | |
Generic | |
// Object specifies an object. It is mutual exclusive with StringMap. | |
Object *Object | |
// StringMap specifies a JSON object with arbitrary, non-prunes keys, but typed value. It is mutual exlusive with Object. | |
StringMap *StringMap | |
// Array specifies an array. | |
Array *Array | |
// Primitive is a typed primitive value. | |
Primitive *Primitive | |
// NoPrune is set to stop pruning for the whole JSON subtree which is unspecified. An object stops NoPrune for everything below that object (if not specified explicitly again). | |
NoPrune bool | |
// Value holds the optional value validation. | |
Value *Value | |
} | |
type Generic struct { | |
ReadOnly bool | |
Nullable bool | |
Description string | |
Default interface{} | |
} | |
func (s *Structural) Polymorphic() bool { | |
c := 0 | |
if s.Object != nil { | |
c++ | |
} | |
if s.Array != nil { | |
c++ | |
} | |
if s.Primitive != nil { | |
c++ | |
} | |
if s.StringMap != nil { | |
c++ | |
} | |
return c > 1 | |
} | |
type Object struct { | |
Properties map[string]Structural | |
UnionFields []string | |
Discriminator string | |
} | |
type Array struct { | |
Items Structural | |
} | |
type Primitive struct { | |
Type string | |
} | |
type StringMap struct { | |
AdditionalProperties *Structural | |
} | |
type Value struct { | |
Schema string | |
Ref *string | |
Format string | |
Title string | |
Maximum *float64 | |
ExclusiveMaximum bool | |
Minimum *float64 | |
ExclusiveMinimum bool | |
MaxLength *int64 | |
MinLength *int64 | |
Pattern string | |
MaxItems *int64 | |
MinItems *int64 | |
UniqueItems bool | |
MultipleOf *float64 | |
Enum []interface{} | |
MaxProperties *int64 | |
MinProperties *int64 | |
Required []string | |
AllOf []NestedValue | |
OneOf []NestedValue | |
AnyOf []NestedValue | |
Not *NestedValue | |
} | |
type NestedValue struct { | |
Value | |
Type string | |
Items *NestedValue | |
Properties map[string]NestedValue | |
} | |
func NewValue(s *apiextensions.JSONSchemaProps) *Value { | |
if s == nil { | |
return nil | |
} | |
v := &Value{ | |
Schema: string(s.Schema), | |
Format: s.Format, | |
Title: s.Title, | |
Maximum: s.Maximum, | |
ExclusiveMaximum: s.ExclusiveMaximum, | |
Minimum: s.Minimum, | |
ExclusiveMinimum: s.ExclusiveMinimum, | |
MaxLength: s.MaxLength, | |
MinLength: s.MinLength, | |
Pattern: s.Pattern, | |
MaxItems: s.MaxItems, | |
MinItems: s.MinItems, | |
UniqueItems: s.UniqueItems, | |
MultipleOf: s.MultipleOf, | |
MaxProperties: s.MaxProperties, | |
MinProperties: s.MinProperties, | |
Required: s.Required, | |
Not: NewNestedValue(s.Not), | |
} | |
for _, e := range s.Enum { | |
v.Enum = append(v.Enum, e) | |
} | |
for _, x := range s.AllOf { | |
v.AllOf = append(v.AllOf, *NewNestedValue(&x)) | |
} | |
for _, x := range s.AnyOf { | |
v.AllOf = append(v.AnyOf, *NewNestedValue(&x)) | |
} | |
for _, x := range s.OneOf { | |
v.AllOf = append(v.OneOf, *NewNestedValue(&x)) | |
} | |
return v | |
} | |
func NewNestedValue(s *apiextensions.JSONSchemaProps) *NestedValue { | |
if s == nil { | |
return nil | |
} | |
v := &NestedValue{ | |
Value: *NewValue(s), | |
Type: s.Type, | |
} | |
if s.Items != nil { | |
v.Items = NewNestedValue(s.Items.Schema) // we validate that it is not an array | |
} | |
if len(s.Properties) > 0 { | |
v.Properties = make(map[string]NestedValue, len(s.Properties)) | |
for k, x := range s.Properties { | |
v.Properties[k] = *NewNestedValue(&x) | |
} | |
} | |
return v | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment