| 1 |
/* |
| 2 |
Copyright 2024 The Kubernetes 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 |
http://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 mutation |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"github.com/google/cel-go/cel" |
| 22 |
"github.com/google/cel-go/common/types" |
| 23 |
"github.com/google/cel-go/common/types/ref" |
| 24 |
"reflect" |
| 25 |
) |
| 26 |
|
| 27 |
var jsonPatchType = types.NewObjectType(JSONPatchTypeName) |
| 28 |
|
| 29 |
var ( |
| 30 |
jsonPatchOp = "op" |
| 31 |
jsonPatchPath = "path" |
| 32 |
jsonPatchFrom = "from" |
| 33 |
jsonPatchValue = "value" |
| 34 |
) |
| 35 |
|
| 36 |
// JSONPatchType and JSONPatchVal are defined entirely from scratch here because JSONPatchVal |
| 37 |
// has a dynamic 'value' field which can not be defined with an OpenAPI schema, |
| 38 |
// preventing us from using DeclType and UnstructuredToVal. |
| 39 |
|
| 40 |
// JSONPatchType provides a CEL type for "JSONPatch" operations. |
| 41 |
type JSONPatchType struct{} |
| 42 |
|
| 43 |
func (r *JSONPatchType) HasTrait(trait int) bool { |
| 44 |
return jsonPatchType.HasTrait(trait) |
| 45 |
} |
| 46 |
|
| 47 |
// TypeName returns the name of this ObjectType. |
| 48 |
func (r *JSONPatchType) TypeName() string { |
| 49 |
return jsonPatchType.TypeName() |
| 50 |
} |
| 51 |
|
| 52 |
// Val returns an instance given the fields. |
| 53 |
func (r *JSONPatchType) Val(fields map[string]ref.Val) ref.Val { |
| 54 |
result := &JSONPatchVal{} |
| 55 |
for name, value := range fields { |
| 56 |
switch name { |
| 57 |
case jsonPatchOp: |
| 58 |
if s, ok := value.Value().(string); ok { |
| 59 |
result.Op = s |
| 60 |
} else { |
| 61 |
return types.NewErr("unexpected type %T for JSONPatchType 'op' field", value.Value()) |
| 62 |
} |
| 63 |
case jsonPatchPath: |
| 64 |
if s, ok := value.Value().(string); ok { |
| 65 |
result.Path = s |
| 66 |
} else { |
| 67 |
return types.NewErr("unexpected type %T for JSONPatchType 'path' field", value.Value()) |
| 68 |
} |
| 69 |
case jsonPatchFrom: |
| 70 |
if s, ok := value.Value().(string); ok { |
| 71 |
result.From = s |
| 72 |
} else { |
| 73 |
return types.NewErr("unexpected type %T for JSONPatchType 'from' field", value.Value()) |
| 74 |
} |
| 75 |
case jsonPatchValue: |
| 76 |
result.Val = value |
| 77 |
default: |
| 78 |
return types.NewErr("unexpected JSONPatchType field: %s", name) |
| 79 |
} |
| 80 |
} |
| 81 |
return result |
| 82 |
} |
| 83 |
|
| 84 |
func (r *JSONPatchType) Type() *types.Type { |
| 85 |
return jsonPatchType |
| 86 |
} |
| 87 |
|
| 88 |
func (r *JSONPatchType) Field(name string) (*types.FieldType, bool) { |
| 89 |
var fieldType *types.Type |
| 90 |
switch name { |
| 91 |
case jsonPatchOp, jsonPatchFrom, jsonPatchPath: |
| 92 |
fieldType = cel.StringType |
| 93 |
case jsonPatchValue: |
| 94 |
fieldType = types.DynType |
| 95 |
} |
| 96 |
return &types.FieldType{ |
| 97 |
Type: fieldType, |
| 98 |
}, true |
| 99 |
} |
| 100 |
|
| 101 |
func (r *JSONPatchType) FieldNames() ([]string, bool) { |
| 102 |
return []string{jsonPatchOp, jsonPatchFrom, jsonPatchPath, jsonPatchValue}, true |
| 103 |
} |
| 104 |
|
| 105 |
// JSONPatchVal is the ref.Val for a JSONPatch. |
| 106 |
type JSONPatchVal struct { |
| 107 |
Op, From, Path string |
| 108 |
Val ref.Val |
| 109 |
} |
| 110 |
|
| 111 |
func (p *JSONPatchVal) ConvertToNative(typeDesc reflect.Type) (any, error) { |
| 112 |
if typeDesc == reflect.TypeOf(&JSONPatchVal{}) { |
| 113 |
return p, nil |
| 114 |
} |
| 115 |
return nil, fmt.Errorf("cannot convert to native type: %v", typeDesc) |
| 116 |
} |
| 117 |
|
| 118 |
func (p *JSONPatchVal) ConvertToType(typeValue ref.Type) ref.Val { |
| 119 |
if typeValue == jsonPatchType { |
| 120 |
return p |
| 121 |
} else if typeValue == types.TypeType { |
| 122 |
return types.NewTypeTypeWithParam(jsonPatchType) |
| 123 |
} |
| 124 |
return types.NewErr("unsupported type: %s", typeValue.TypeName()) |
| 125 |
} |
| 126 |
|
| 127 |
func (p *JSONPatchVal) Equal(other ref.Val) ref.Val { |
| 128 |
if o, ok := other.(*JSONPatchVal); ok && p != nil && o != nil { |
| 129 |
if p.Op != o.Op || p.From != o.From || p.Path != o.Path { |
| 130 |
return types.False |
| 131 |
} |
| 132 |
if (p.Val == nil) != (o.Val == nil) { |
| 133 |
return types.False |
| 134 |
} |
| 135 |
if p.Val == nil { |
| 136 |
return types.True |
| 137 |
} |
| 138 |
return p.Val.Equal(o.Val) |
| 139 |
} |
| 140 |
return types.False |
| 141 |
} |
| 142 |
|
| 143 |
func (p *JSONPatchVal) Get(index ref.Val) ref.Val { |
| 144 |
if name, ok := index.Value().(string); ok { |
| 145 |
switch name { |
| 146 |
case jsonPatchOp: |
| 147 |
return types.String(p.Op) |
| 148 |
case jsonPatchPath: |
| 149 |
return types.String(p.Path) |
| 150 |
case jsonPatchFrom: |
| 151 |
return types.String(p.From) |
| 152 |
case jsonPatchValue: |
| 153 |
return p.Val |
| 154 |
default: |
| 155 |
|
| 156 |
} |
| 157 |
} |
| 158 |
return types.NewErr("unsupported indexer: %s", index) |
| 159 |
} |
| 160 |
|
| 161 |
func (p *JSONPatchVal) IsSet(field ref.Val) ref.Val { |
| 162 |
if name, ok := field.Value().(string); ok { |
| 163 |
switch name { |
| 164 |
case jsonPatchOp: |
| 165 |
return types.Bool(len(p.Op) > 0) |
| 166 |
case jsonPatchPath: |
| 167 |
return types.Bool(len(p.Path) > 0) |
| 168 |
case jsonPatchFrom: |
| 169 |
return types.Bool(len(p.From) > 0) |
| 170 |
case jsonPatchValue: |
| 171 |
return types.Bool(p.Val != nil) |
| 172 |
} |
| 173 |
} |
| 174 |
return types.NewErr("unsupported field: %s", field) |
| 175 |
} |
| 176 |
|
| 177 |
func (p *JSONPatchVal) Type() ref.Type { |
| 178 |
return jsonPatchType |
| 179 |
} |
| 180 |
|
| 181 |
func (p *JSONPatchVal) Value() any { |
| 182 |
return p |
| 183 |
} |
| 184 |
|
| 185 |
var _ ref.Val = &JSONPatchVal{} |