refactor: use common.Schema in CorrelatedObject

for reuseability with CEL structural schema impl
This commit is contained in:
Alexander Zielenski 2023-10-10 10:23:06 -07:00
parent 83a1dbf885
commit e76aad1813

View File

@ -60,7 +60,10 @@ func (r *RatchetingSchemaValidator) Validate(new interface{}) *validate.Result {
} }
func (r *RatchetingSchemaValidator) ValidateUpdate(new, old interface{}) *validate.Result { func (r *RatchetingSchemaValidator) ValidateUpdate(new, old interface{}) *validate.Result {
return newRatchetingValueValidator(NewCorrelatedObject(new, old, r.schema), r.schemaArgs).Validate(new) return newRatchetingValueValidator(
NewCorrelatedObject(new, old, &celopenapi.Schema{Schema: r.schemaArgs.schema}),
r.schemaArgs,
).Validate(new)
} }
// ratchetingValueValidator represents an invocation of SchemaValidator.ValidateUpdate // ratchetingValueValidator represents an invocation of SchemaValidator.ValidateUpdate
@ -90,7 +93,7 @@ type CorrelatedObject struct {
// Value being validated // Value being validated
Value interface{} Value interface{}
Schema *spec.Schema Schema common.Schema
// Scratch space below, may change during validation // Scratch space below, may change during validation
@ -113,7 +116,7 @@ type CorrelatedObject struct {
children map[interface{}]*CorrelatedObject children map[interface{}]*CorrelatedObject
} }
func NewCorrelatedObject(new, old interface{}, schema *spec.Schema) *CorrelatedObject { func NewCorrelatedObject(new, old interface{}, schema common.Schema) *CorrelatedObject {
return &CorrelatedObject{ return &CorrelatedObject{
OldValue: old, OldValue: old,
Value: new, Value: new,
@ -243,7 +246,7 @@ func (r *CorrelatedObject) correlateOldValueForChildAtNewIndex(index int) any {
return nil return nil
} }
listType, _ := r.Schema.Extensions.GetString("x-kubernetes-list-type") listType := r.Schema.XListType()
switch listType { switch listType {
case "map": case "map":
// Look up keys for this index in current object // Look up keys for this index in current object
@ -251,7 +254,7 @@ func (r *CorrelatedObject) correlateOldValueForChildAtNewIndex(index int) any {
oldList := r.mapList oldList := r.mapList
if oldList == nil { if oldList == nil {
oldList = celopenapi.MakeMapList(r.Schema, oldAsList) oldList = common.MakeMapList(r.Schema, oldAsList)
r.mapList = oldList r.mapList = oldList
} }
return oldList.Get(currentElement) return oldList.Get(currentElement)
@ -411,11 +414,11 @@ func (l *CorrelatedObject) Key(field string) *CorrelatedObject {
return nil return nil
} }
var propertySchema *spec.Schema var propertySchema common.Schema
if prop, exists := l.Schema.Properties[field]; exists { if prop, exists := l.Schema.Properties()[field]; exists {
propertySchema = &prop propertySchema = prop
} else if addP := l.Schema.AdditionalProperties; addP != nil && addP.Schema != nil { } else if addP := l.Schema.AdditionalProperties(); addP != nil && addP.Schema() != nil {
propertySchema = addP.Schema propertySchema = addP.Schema()
} else { } else {
return nil return nil
} }
@ -449,9 +452,9 @@ func (l *CorrelatedObject) Index(i int) *CorrelatedObject {
if oldValueForIndex == nil { if oldValueForIndex == nil {
return nil return nil
} }
var itemSchema *spec.Schema var itemSchema common.Schema
if i := l.Schema.Items; i != nil && i.Schema != nil { if i := l.Schema.Items(); i != nil {
itemSchema = i.Schema itemSchema = i
} else { } else {
return nil return nil
} }