knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/configmap/filter.go (about)

     1  /*
     2  Copyright 2018 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      https://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package configmap
    18  
    19  import (
    20  	"errors"
    21  	"reflect"
    22  
    23  	corev1 "k8s.io/api/core/v1"
    24  )
    25  
    26  // TypeFilter accepts instances of types to check against and returns a function transformer that would only let
    27  // the call to f through if value is assignable to any one of types of ts. Example:
    28  //
    29  // F := configmap.TypeFilter(&config.Domain{})(f)
    30  //
    31  // The result is a function F(name string, value interface{}) that will call the underlying function
    32  // f(name, value) iff value is a *config.Domain
    33  func TypeFilter(ts ...interface{}) func(func(string, interface{})) func(string, interface{}) {
    34  	return func(f func(string, interface{})) func(string, interface{}) {
    35  		return func(name string, value interface{}) {
    36  			satisfies := false
    37  			for _, t := range ts {
    38  				t := reflect.TypeOf(t)
    39  				if reflect.TypeOf(value).AssignableTo(t) {
    40  					satisfies = true
    41  					break
    42  				}
    43  			}
    44  			if satisfies {
    45  				f(name, value)
    46  			}
    47  		}
    48  	}
    49  }
    50  
    51  // ValidateConstructor checks the type of the constructor it evaluates
    52  // the constructor to be a function with correct signature.
    53  //
    54  // The expectation is for the constructor to receive a single input
    55  // parameter of type corev1.ConfigMap as the input and return two
    56  // values with the second value being of type error
    57  func ValidateConstructor(constructor interface{}) error {
    58  	cType := reflect.TypeOf(constructor)
    59  
    60  	if cType.Kind() != reflect.Func {
    61  		return errors.New("config constructor must be a function")
    62  	}
    63  
    64  	if cType.NumIn() != 1 || cType.In(0) != reflect.TypeOf(&corev1.ConfigMap{}) {
    65  		return errors.New("config constructor must be of the type func(*k8s.io/api/core/v1/ConfigMap) (..., error)")
    66  	}
    67  
    68  	errorType := reflect.TypeOf((*error)(nil)).Elem()
    69  
    70  	if cType.NumOut() != 2 || !cType.Out(1).Implements(errorType) {
    71  		return errors.New("config constructor must be of the type func(*k8s.io/api/core/v1/ConfigMap) (..., error)")
    72  	}
    73  	return nil
    74  }