| 1 |
// Copyright 2023 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package checker |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"strings" |
| 20 |
|
| 21 |
chkdecls "github.com/google/cel-go/checker/decls" |
| 22 |
"github.com/google/cel-go/common/types" |
| 23 |
|
| 24 |
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 25 |
) |
| 26 |
|
| 27 |
const ( |
| 28 |
kindUnknown = iota + 1 |
| 29 |
kindError |
| 30 |
kindFunction |
| 31 |
kindDyn |
| 32 |
kindPrimitive |
| 33 |
kindWellKnown |
| 34 |
kindWrapper |
| 35 |
kindNull |
| 36 |
kindAbstract |
| 37 |
kindType |
| 38 |
kindList |
| 39 |
kindMap |
| 40 |
kindObject |
| 41 |
kindTypeParam |
| 42 |
) |
| 43 |
|
| 44 |
// FormatCheckedType converts a type message into a string representation. |
| 45 |
func FormatCheckedType(t *exprpb.Type) string { |
| 46 |
switch kindOf(t) { |
| 47 |
case kindDyn: |
| 48 |
return "dyn" |
| 49 |
case kindFunction: |
| 50 |
return formatFunctionExprType(t.GetFunction().GetResultType(), |
| 51 |
t.GetFunction().GetArgTypes(), |
| 52 |
false) |
| 53 |
case kindList: |
| 54 |
return fmt.Sprintf("list(%s)", FormatCheckedType(t.GetListType().GetElemType())) |
| 55 |
case kindObject: |
| 56 |
return t.GetMessageType() |
| 57 |
case kindMap: |
| 58 |
return fmt.Sprintf("map(%s, %s)", |
| 59 |
FormatCheckedType(t.GetMapType().GetKeyType()), |
| 60 |
FormatCheckedType(t.GetMapType().GetValueType())) |
| 61 |
case kindNull: |
| 62 |
return "null" |
| 63 |
case kindPrimitive: |
| 64 |
switch t.GetPrimitive() { |
| 65 |
case exprpb.Type_UINT64: |
| 66 |
return "uint" |
| 67 |
case exprpb.Type_INT64: |
| 68 |
return "int" |
| 69 |
} |
| 70 |
return strings.Trim(strings.ToLower(t.GetPrimitive().String()), " ") |
| 71 |
case kindType: |
| 72 |
if t.GetType() == nil || t.GetType().GetTypeKind() == nil { |
| 73 |
return "type" |
| 74 |
} |
| 75 |
return fmt.Sprintf("type(%s)", FormatCheckedType(t.GetType())) |
| 76 |
case kindWellKnown: |
| 77 |
switch t.GetWellKnown() { |
| 78 |
case exprpb.Type_ANY: |
| 79 |
return "any" |
| 80 |
case exprpb.Type_DURATION: |
| 81 |
return "duration" |
| 82 |
case exprpb.Type_TIMESTAMP: |
| 83 |
return "timestamp" |
| 84 |
} |
| 85 |
case kindWrapper: |
| 86 |
return fmt.Sprintf("wrapper(%s)", |
| 87 |
FormatCheckedType(chkdecls.NewPrimitiveType(t.GetWrapper()))) |
| 88 |
case kindError: |
| 89 |
return "!error!" |
| 90 |
case kindTypeParam: |
| 91 |
return t.GetTypeParam() |
| 92 |
case kindAbstract: |
| 93 |
at := t.GetAbstractType() |
| 94 |
params := at.GetParameterTypes() |
| 95 |
paramStrs := make([]string, len(params)) |
| 96 |
for i, p := range params { |
| 97 |
paramStrs[i] = FormatCheckedType(p) |
| 98 |
} |
| 99 |
return fmt.Sprintf("%s(%s)", at.GetName(), strings.Join(paramStrs, ", ")) |
| 100 |
} |
| 101 |
return t.String() |
| 102 |
} |
| 103 |
|
| 104 |
type formatter func(any) string |
| 105 |
|
| 106 |
// FormatCELType formats a types.Type value to a string representation. |
| 107 |
// |
| 108 |
// The type formatting is identical to FormatCheckedType. |
| 109 |
func FormatCELType(t any) string { |
| 110 |
dt := t.(*types.Type) |
| 111 |
switch dt.Kind() { |
| 112 |
case types.AnyKind: |
| 113 |
return "any" |
| 114 |
case types.DurationKind: |
| 115 |
return "duration" |
| 116 |
case types.ErrorKind: |
| 117 |
return "!error!" |
| 118 |
case types.NullTypeKind: |
| 119 |
return "null" |
| 120 |
case types.TimestampKind: |
| 121 |
return "timestamp" |
| 122 |
case types.TypeParamKind: |
| 123 |
return dt.TypeName() |
| 124 |
case types.OpaqueKind: |
| 125 |
if dt.TypeName() == "function" { |
| 126 |
// There is no explicit function type in the new types representation, so information like |
| 127 |
// whether the function is a member function is absent. |
| 128 |
return formatFunctionDeclType(dt.Parameters()[0], dt.Parameters()[1:], false) |
| 129 |
} |
| 130 |
case types.UnspecifiedKind: |
| 131 |
return "" |
| 132 |
} |
| 133 |
if len(dt.Parameters()) == 0 { |
| 134 |
return dt.DeclaredTypeName() |
| 135 |
} |
| 136 |
paramTypeNames := make([]string, 0, len(dt.Parameters())) |
| 137 |
for _, p := range dt.Parameters() { |
| 138 |
paramTypeNames = append(paramTypeNames, FormatCELType(p)) |
| 139 |
} |
| 140 |
return fmt.Sprintf("%s(%s)", dt.TypeName(), strings.Join(paramTypeNames, ", ")) |
| 141 |
} |
| 142 |
|
| 143 |
func formatExprType(t any) string { |
| 144 |
if t == nil { |
| 145 |
return "" |
| 146 |
} |
| 147 |
return FormatCheckedType(t.(*exprpb.Type)) |
| 148 |
} |
| 149 |
|
| 150 |
func formatFunctionExprType(resultType *exprpb.Type, argTypes []*exprpb.Type, isInstance bool) string { |
| 151 |
return formatFunctionInternal[*exprpb.Type](resultType, argTypes, isInstance, formatExprType) |
| 152 |
} |
| 153 |
|
| 154 |
func formatFunctionDeclType(resultType *types.Type, argTypes []*types.Type, isInstance bool) string { |
| 155 |
return formatFunctionInternal[*types.Type](resultType, argTypes, isInstance, FormatCELType) |
| 156 |
} |
| 157 |
|
| 158 |
func formatFunctionInternal[T any](resultType T, argTypes []T, isInstance bool, format formatter) string { |
| 159 |
result := "" |
| 160 |
if isInstance { |
| 161 |
target := argTypes[0] |
| 162 |
argTypes = argTypes[1:] |
| 163 |
result += format(target) |
| 164 |
result += "." |
| 165 |
} |
| 166 |
result += "(" |
| 167 |
for i, arg := range argTypes { |
| 168 |
if i > 0 { |
| 169 |
result += ", " |
| 170 |
} |
| 171 |
result += format(arg) |
| 172 |
} |
| 173 |
result += ")" |
| 174 |
rt := format(resultType) |
| 175 |
if rt != "" { |
| 176 |
result += " -> " |
| 177 |
result += rt |
| 178 |
} |
| 179 |
return result |
| 180 |
} |
| 181 |
|
| 182 |
// kindOf returns the kind of the type as defined in the checked.proto. |
| 183 |
func kindOf(t *exprpb.Type) int { |
| 184 |
if t == nil || t.TypeKind == nil { |
| 185 |
return kindUnknown |
| 186 |
} |
| 187 |
switch t.GetTypeKind().(type) { |
| 188 |
case *exprpb.Type_Error: |
| 189 |
return kindError |
| 190 |
case *exprpb.Type_Function: |
| 191 |
return kindFunction |
| 192 |
case *exprpb.Type_Dyn: |
| 193 |
return kindDyn |
| 194 |
case *exprpb.Type_Primitive: |
| 195 |
return kindPrimitive |
| 196 |
case *exprpb.Type_WellKnown: |
| 197 |
return kindWellKnown |
| 198 |
case *exprpb.Type_Wrapper: |
| 199 |
return kindWrapper |
| 200 |
case *exprpb.Type_Null: |
| 201 |
return kindNull |
| 202 |
case *exprpb.Type_Type: |
| 203 |
return kindType |
| 204 |
case *exprpb.Type_ListType_: |
| 205 |
return kindList |
| 206 |
case *exprpb.Type_MapType_: |
| 207 |
return kindMap |
| 208 |
case *exprpb.Type_MessageType: |
| 209 |
return kindObject |
| 210 |
case *exprpb.Type_TypeParam: |
| 211 |
return kindTypeParam |
| 212 |
case *exprpb.Type_AbstractType_: |
| 213 |
return kindAbstract |
| 214 |
} |
| 215 |
return kindUnknown |
| 216 |
} |