...
1
16
17 package defaulting
18
19 import (
20 structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
21 "k8s.io/apimachinery/pkg/runtime"
22 )
23
24
25 func isNonNullableNull(x interface{}, s *structuralschema.Structural) bool {
26 return x == nil && s != nil && s.Generic.Nullable == false
27 }
28
29
30
31
32
33
34 func Default(x interface{}, s *structuralschema.Structural) {
35 if s == nil {
36 return
37 }
38
39 switch x := x.(type) {
40 case map[string]interface{}:
41 for k, prop := range s.Properties {
42 if prop.Default.Object == nil {
43 continue
44 }
45 if _, found := x[k]; !found || isNonNullableNull(x[k], &prop) {
46 x[k] = runtime.DeepCopyJSONValue(prop.Default.Object)
47 }
48 }
49 for k := range x {
50 if prop, found := s.Properties[k]; found {
51 Default(x[k], &prop)
52 } else if s.AdditionalProperties != nil {
53 if isNonNullableNull(x[k], s.AdditionalProperties.Structural) {
54 x[k] = runtime.DeepCopyJSONValue(s.AdditionalProperties.Structural.Default.Object)
55 }
56 Default(x[k], s.AdditionalProperties.Structural)
57 }
58 }
59 case []interface{}:
60 for i := range x {
61 if isNonNullableNull(x[i], s.Items) {
62 x[i] = runtime.DeepCopyJSONValue(s.Items.Default.Object)
63 }
64 Default(x[i], s.Items)
65 }
66 default:
67
68 }
69 }
70
View as plain text