...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/crdutil/objectorarray.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util/crdutil

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package crdutil
    16  
    17  import (
    18  	"fmt"
    19  
    20  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    21  )
    22  
    23  var (
    24  	objectSchemaErr = fmt.Errorf("the object schema has neither properties nor additionalProperties in the schema")
    25  	schemaTypeErr   = fmt.Errorf("type of schema must be 'object' or 'array'")
    26  )
    27  
    28  func getConfigurableSchemaForObjectOrArray(schema *apiextensions.JSONSchemaProps) (configurableSchema *apiextensions.JSONSchemaProps, err error) {
    29  	switch schema.Type {
    30  	case "object":
    31  		if schema.Properties != nil {
    32  			configurableSchema = schema
    33  		} else if schema.AdditionalProperties != nil {
    34  			configurableSchema = schema.AdditionalProperties.Schema
    35  		} else {
    36  			return nil, objectSchemaErr
    37  		}
    38  	case "array":
    39  		return getConfigurableSchemaForObjectOrArray(schema.Items.Schema)
    40  	default:
    41  		return nil, schemaTypeErr
    42  	}
    43  	return configurableSchema, nil
    44  }
    45  
    46  func GetSchemaForFieldUnderObjectOrArray(fieldName string, parent *apiextensions.JSONSchemaProps) (schema *apiextensions.JSONSchemaProps, ok bool, err error) {
    47  	var schemaValue apiextensions.JSONSchemaProps
    48  	configurableSchema, err := getConfigurableSchemaForObjectOrArray(parent)
    49  	if err != nil {
    50  		return nil, false, err
    51  	}
    52  	schemaValue, ok = configurableSchema.Properties[fieldName]
    53  	if ok {
    54  		schema = &schemaValue
    55  	}
    56  	return schema, ok, nil
    57  }
    58  
    59  func SetSchemaForFieldUnderObjectOrArray(fieldName string, parent *apiextensions.JSONSchemaProps, schema *apiextensions.JSONSchemaProps) error {
    60  	configurableSchema, err := getConfigurableSchemaForObjectOrArray(parent)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	configurableSchema.Properties[fieldName] = *schema
    65  	return nil
    66  }
    67  
    68  func GetRequiredRuleForObjectOrArray(schema *apiextensions.JSONSchemaProps) ([]string, error) {
    69  	configurableSchema, err := getConfigurableSchemaForObjectOrArray(schema)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	return configurableSchema.Required, nil
    74  }
    75  
    76  func SetRequiredRuleForObjectOrArray(schema *apiextensions.JSONSchemaProps, requiredRule []string) error {
    77  	configurableSchema, err := getConfigurableSchemaForObjectOrArray(schema)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	configurableSchema.Required = requiredRule
    82  	return nil
    83  }
    84  
    85  func GetOneOfRuleForObjectOrArray(schema *apiextensions.JSONSchemaProps) ([]*apiextensions.JSONSchemaProps, error) {
    86  	var oneOfRule []*apiextensions.JSONSchemaProps
    87  	var oneOfRuleValue []apiextensions.JSONSchemaProps
    88  	configurableSchema, err := getConfigurableSchemaForObjectOrArray(schema)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	oneOfRuleValue = configurableSchema.OneOf
    93  	if oneOfRuleValue != nil {
    94  		for i, _ := range oneOfRuleValue {
    95  			oneOfRule = append(oneOfRule, &oneOfRuleValue[i])
    96  		}
    97  	}
    98  	return oneOfRule, nil
    99  }
   100  
   101  func SetOneOfRuleForObjectOrArray(schema *apiextensions.JSONSchemaProps, oneOfRule []*apiextensions.JSONSchemaProps) error {
   102  	var oneOfRuleValue []apiextensions.JSONSchemaProps
   103  	for _, rule := range oneOfRule {
   104  		oneOfRuleValue = append(oneOfRuleValue, *rule)
   105  	}
   106  	configurableSchema, err := getConfigurableSchemaForObjectOrArray(schema)
   107  	if err != nil {
   108  		return err
   109  	}
   110  	configurableSchema.OneOf = oneOfRuleValue
   111  	return nil
   112  }
   113  
   114  func GetNotRuleForObjectOrArray(schema *apiextensions.JSONSchemaProps) (*apiextensions.JSONSchemaProps, error) {
   115  	configurableSchema, err := getConfigurableSchemaForObjectOrArray(schema)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  	return configurableSchema.Not, nil
   120  }
   121  
   122  func SetNotRuleForObjectOrArray(schema *apiextensions.JSONSchemaProps, notRule *apiextensions.JSONSchemaProps) error {
   123  	configurableSchema, err := getConfigurableSchemaForObjectOrArray(schema)
   124  	if err != nil {
   125  		return err
   126  	}
   127  	configurableSchema.Not = notRule
   128  	return nil
   129  }
   130  

View as plain text