2020-04-20 18:36:34 +02:00
package dynamic
2023-04-17 17:22:05 +02:00
import (
"encoding/json"
"fmt"
"reflect"
)
2020-04-20 18:36:34 +02:00
// +k8s:deepcopy-gen=false
// PluginConf holds the plugin configuration.
2023-04-17 17:22:05 +02:00
type PluginConf map [ string ] any
2020-04-20 18:36:34 +02:00
2023-04-17 17:22:05 +02:00
// DeepCopyInto is a deepcopy function, copying the receiver, writing into out. in must be non-nil.
2020-04-20 18:36:34 +02:00
func ( in * PluginConf ) DeepCopyInto ( out * PluginConf ) {
if in == nil {
* out = nil
} else {
2023-04-17 17:22:05 +02:00
* out = deepCopyJSON ( * in )
2020-04-20 18:36:34 +02:00
}
}
2023-04-17 17:22:05 +02:00
// DeepCopy is a deepcopy function, copying the receiver, creating a new PluginConf.
2020-04-20 18:36:34 +02:00
func ( in * PluginConf ) DeepCopy ( ) * PluginConf {
if in == nil {
return nil
}
out := new ( PluginConf )
in . DeepCopyInto ( out )
return out
}
2023-04-17 17:22:05 +02:00
// inspired by https://github.com/kubernetes/apimachinery/blob/53ecdf01b997ca93c7db7615dfe7b27ad8391983/pkg/runtime/converter.go#L607
func deepCopyJSON ( x map [ string ] any ) map [ string ] any {
return deepCopyJSONValue ( x ) . ( map [ string ] any )
}
func deepCopyJSONValue ( x any ) any {
switch x := x . ( type ) {
case map [ string ] any :
if x == nil {
// Typed nil - an any that contains a type map[string]any with a value of nil
return x
}
clone := make ( map [ string ] any , len ( x ) )
for k , v := range x {
clone [ k ] = deepCopyJSONValue ( v )
}
return clone
case [ ] any :
if x == nil {
// Typed nil - an any that contains a type []any with a value of nil
return x
}
clone := make ( [ ] any , len ( x ) )
for i , v := range x {
clone [ i ] = deepCopyJSONValue ( v )
}
return clone
case string , int64 , bool , float64 , nil , json . Number :
return x
default :
v := reflect . ValueOf ( x )
if v . NumMethod ( ) == 0 {
panic ( fmt . Errorf ( "cannot deep copy %T" , x ) )
}
method := v . MethodByName ( "DeepCopy" )
if method . Kind ( ) == reflect . Invalid {
panic ( fmt . Errorf ( "cannot deep copy %T" , x ) )
}
call := method . Call ( nil )
return call [ 0 ] . Interface ( )
}
}