Debian Codegraph @

pkg/cel/format.go

raw
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 cel
18
19 import (
20 "fmt"
21 "reflect"
22
23 "github.com/google/cel-go/cel"
24 "github.com/google/cel-go/checker/decls"
25 "github.com/google/cel-go/common/types"
26 "github.com/google/cel-go/common/types/ref"
27 )
28
29 var (
30 FormatObject = decls.NewObjectType("kubernetes.NamedFormat")
31 FormatType = cel.ObjectType("kubernetes.NamedFormat")
32 )
33
34 // Format provdes a CEL representation of kubernetes format
35 type Format struct {
36 Name string
37 ValidateFunc func(string) []string
38
39 // Size of the regex string or estimated equivalent regex string used
40 // for cost estimation
41 MaxRegexSize int
42 }
43
44 func (d Format) ConvertToNative(typeDesc reflect.Type) (interface{}, error) {
45 return nil, fmt.Errorf("type conversion error from 'Format' to '%v'", typeDesc)
46 }
47
48 func (d Format) ConvertToType(typeVal ref.Type) ref.Val {
49 switch typeVal {
50 case FormatType:
51 return d
52 case types.TypeType:
53 return FormatType
54 default:
55 return types.NewErr("type conversion error from '%s' to '%s'", FormatType, typeVal)
56 }
57 }
58
59 func (d Format) Equal(other ref.Val) ref.Val {
60 otherDur, ok := other.(Format)
61 if !ok {
62 return types.MaybeNoSuchOverloadErr(other)
63 }
64 return types.Bool(d.Name == otherDur.Name)
65 }
66
67 func (d Format) Type() ref.Type {
68 return FormatType
69 }
70
71 func (d Format) Value() interface{} {
72 return d
73 }