| 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 library |
| 18 |
|
| 19 |
import ( |
| 20 |
"github.com/google/cel-go/cel" |
| 21 |
"github.com/google/cel-go/common/types" |
| 22 |
"github.com/google/cel-go/common/types/ref" |
| 23 |
"strings" |
| 24 |
) |
| 25 |
|
| 26 |
// JSONPatch provides a CEL function library extension of JSONPatch functions. |
| 27 |
// |
| 28 |
// jsonpatch.escapeKey |
| 29 |
// |
| 30 |
// Escapes a string for use as a JSONPatch path key. |
| 31 |
// |
| 32 |
// jsonpatch.escapeKey(<string>) <string> |
| 33 |
// |
| 34 |
// Examples: |
| 35 |
// |
| 36 |
// "/metadata/labels/" + jsonpatch.escapeKey('k8s.io/my~label') // returns "/metadata/labels/k8s.io~1my~0label" |
| 37 |
func JSONPatch() cel.EnvOption { |
| 38 |
return cel.Lib(jsonPatchLib) |
| 39 |
} |
| 40 |
|
| 41 |
var jsonPatchLib = &jsonPatch{} |
| 42 |
|
| 43 |
type jsonPatch struct{} |
| 44 |
|
| 45 |
func (*jsonPatch) LibraryName() string { |
| 46 |
return "kubernetes.jsonpatch" |
| 47 |
} |
| 48 |
|
| 49 |
func (*jsonPatch) declarations() map[string][]cel.FunctionOpt { |
| 50 |
return jsonPatchLibraryDecls |
| 51 |
} |
| 52 |
|
| 53 |
func (*jsonPatch) Types() []*cel.Type { |
| 54 |
return []*cel.Type{} |
| 55 |
} |
| 56 |
|
| 57 |
var jsonPatchLibraryDecls = map[string][]cel.FunctionOpt{ |
| 58 |
"jsonpatch.escapeKey": { |
| 59 |
cel.Overload("string_jsonpatch_escapeKey_string", []*cel.Type{cel.StringType}, cel.StringType, |
| 60 |
cel.UnaryBinding(escape)), |
| 61 |
}, |
| 62 |
} |
| 63 |
|
| 64 |
func (*jsonPatch) CompileOptions() []cel.EnvOption { |
| 65 |
var options []cel.EnvOption |
| 66 |
for name, overloads := range jsonPatchLibraryDecls { |
| 67 |
options = append(options, cel.Function(name, overloads...)) |
| 68 |
} |
| 69 |
return options |
| 70 |
} |
| 71 |
|
| 72 |
func (*jsonPatch) ProgramOptions() []cel.ProgramOption { |
| 73 |
return []cel.ProgramOption{} |
| 74 |
} |
| 75 |
|
| 76 |
var jsonPatchReplacer = strings.NewReplacer("/", "~1", "~", "~0") |
| 77 |
|
| 78 |
func escapeKey(k string) string { |
| 79 |
return jsonPatchReplacer.Replace(k) |
| 80 |
} |
| 81 |
|
| 82 |
func escape(arg ref.Val) ref.Val { |
| 83 |
s, ok := arg.Value().(string) |
| 84 |
if !ok { |
| 85 |
return types.MaybeNoSuchOverloadErr(arg) |
| 86 |
} |
| 87 |
escaped := escapeKey(s) |
| 88 |
return types.String(escaped) |
| 89 |
} |