| 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 ast |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | |
| 20 | "google.golang.org/protobuf/proto" |
| 21 | |
| 22 | "github.com/google/cel-go/common/types" |
| 23 | "github.com/google/cel-go/common/types/ref" |
| 24 | |
| 25 | celpb "cel.dev/expr" |
| 26 | exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1" |
| 27 | structpb "google.golang.org/protobuf/types/known/structpb" |
| 28 | ) |
| 29 | |
| 30 | // ToProto converts an AST to a CheckedExpr protobouf. |
| 31 | func ToProto(ast *AST) (*exprpb.CheckedExpr, error) { |
| 32 | refMap := make(map[int64]*exprpb.Reference, len(ast.ReferenceMap())) |
| 33 | for id, ref := range ast.ReferenceMap() { |
| 34 | r, err := ReferenceInfoToProto(ref) |
| 35 | if err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | refMap[id] = r |
| 39 | } |
| 40 | typeMap := make(map[int64]*exprpb.Type, len(ast.TypeMap())) |
| 41 | for id, typ := range ast.TypeMap() { |
| 42 | t, err := types.TypeToExprType(typ) |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | typeMap[id] = t |
| 47 | } |
| 48 | e, err := ExprToProto(ast.Expr()) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | info, err := SourceInfoToProto(ast.SourceInfo()) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | return &exprpb.CheckedExpr{ |
| 57 | Expr: e, |
| 58 | SourceInfo: info, |
| 59 | ReferenceMap: refMap, |
| 60 | TypeMap: typeMap, |
| 61 | }, nil |
| 62 | } |
| 63 | |
| 64 | // ToAST converts a CheckedExpr protobuf to an AST instance. |
| 65 | func ToAST(checked *exprpb.CheckedExpr) (*AST, error) { |
| 66 | refMap := make(map[int64]*ReferenceInfo, len(checked.GetReferenceMap())) |
| 67 | for id, ref := range checked.GetReferenceMap() { |
| 68 | r, err := ProtoToReferenceInfo(ref) |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | refMap[id] = r |
| 73 | } |
| 74 | typeMap := make(map[int64]*types.Type, len(checked.GetTypeMap())) |
| 75 | for id, typ := range checked.GetTypeMap() { |
| 76 | t, err := types.ExprTypeToType(typ) |
| 77 | if err != nil { |
| 78 | return nil, err |
| 79 | } |
| 80 | typeMap[id] = t |
| 81 | } |
| 82 | info, err := ProtoToSourceInfo(checked.GetSourceInfo()) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | root, err := ProtoToExpr(checked.GetExpr()) |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | ast := NewCheckedAST(NewAST(root, info), typeMap, refMap) |
| 91 | return ast, nil |
| 92 | } |
| 93 | |
| 94 | // ProtoToExpr converts a protobuf Expr value to an ast.Expr value. |
| 95 | func ProtoToExpr(e *exprpb.Expr) (Expr, error) { |
| 96 | factory := NewExprFactory() |
| 97 | return exprInternal(factory, e) |
| 98 | } |
| 99 | |
| 100 | // ProtoToEntryExpr converts a protobuf struct/map entry to an ast.EntryExpr |
| 101 | func ProtoToEntryExpr(e *exprpb.Expr_CreateStruct_Entry) (EntryExpr, error) { |
| 102 | factory := NewExprFactory() |
| 103 | switch e.GetKeyKind().(type) { |
| 104 | case *exprpb.Expr_CreateStruct_Entry_FieldKey: |
| 105 | return exprStructField(factory, e.GetId(), e) |
| 106 | case *exprpb.Expr_CreateStruct_Entry_MapKey: |
| 107 | return exprMapEntry(factory, e.GetId(), e) |
| 108 | } |
| 109 | return nil, fmt.Errorf("unsupported expr entry kind: %v", e) |
| 110 | } |
| 111 | |
| 112 | func exprInternal(factory ExprFactory, e *exprpb.Expr) (Expr, error) { |
| 113 | id := e.GetId() |
| 114 | switch e.GetExprKind().(type) { |
| 115 | case *exprpb.Expr_CallExpr: |
| 116 | return exprCall(factory, id, e.GetCallExpr()) |
| 117 | case *exprpb.Expr_ComprehensionExpr: |
| 118 | return exprComprehension(factory, id, e.GetComprehensionExpr()) |
| 119 | case *exprpb.Expr_ConstExpr: |
| 120 | return exprLiteral(factory, id, e.GetConstExpr()) |
| 121 | case *exprpb.Expr_IdentExpr: |
| 122 | return exprIdent(factory, id, e.GetIdentExpr()) |
| 123 | case *exprpb.Expr_ListExpr: |
| 124 | return exprList(factory, id, e.GetListExpr()) |
| 125 | case *exprpb.Expr_SelectExpr: |
| 126 | return exprSelect(factory, id, e.GetSelectExpr()) |
| 127 | case *exprpb.Expr_StructExpr: |
| 128 | s := e.GetStructExpr() |
| 129 | if s.GetMessageName() != "" { |
| 130 | return exprStruct(factory, id, s) |
| 131 | } |
| 132 | return exprMap(factory, id, s) |
| 133 | } |
| 134 | return factory.NewUnspecifiedExpr(id), nil |
| 135 | } |
| 136 | |
| 137 | func exprCall(factory ExprFactory, id int64, call *exprpb.Expr_Call) (Expr, error) { |
| 138 | var err error |
| 139 | args := make([]Expr, len(call.GetArgs())) |
| 140 | for i, a := range call.GetArgs() { |
| 141 | args[i], err = exprInternal(factory, a) |
| 142 | if err != nil { |
| 143 | return nil, err |
| 144 | } |
| 145 | } |
| 146 | if call.GetTarget() == nil { |
| 147 | return factory.NewCall(id, call.GetFunction(), args...), nil |
| 148 | } |
| 149 | |
| 150 | target, err := exprInternal(factory, call.GetTarget()) |
| 151 | if err != nil { |
| 152 | return nil, err |
| 153 | } |
| 154 | return factory.NewMemberCall(id, call.GetFunction(), target, args...), nil |
| 155 | } |
| 156 | |
| 157 | func exprComprehension(factory ExprFactory, id int64, comp *exprpb.Expr_Comprehension) (Expr, error) { |
| 158 | iterRange, err := exprInternal(factory, comp.GetIterRange()) |
| 159 | if err != nil { |
| 160 | return nil, err |
| 161 | } |
| 162 | accuInit, err := exprInternal(factory, comp.GetAccuInit()) |
| 163 | if err != nil { |
| 164 | return nil, err |
| 165 | } |
| 166 | loopCond, err := exprInternal(factory, comp.GetLoopCondition()) |
| 167 | if err != nil { |
| 168 | return nil, err |
| 169 | } |
| 170 | loopStep, err := exprInternal(factory, comp.GetLoopStep()) |
| 171 | if err != nil { |
| 172 | return nil, err |
| 173 | } |
| 174 | result, err := exprInternal(factory, comp.GetResult()) |
| 175 | if err != nil { |
| 176 | return nil, err |
| 177 | } |
| 178 | return factory.NewComprehensionTwoVar(id, |
| 179 | iterRange, |
| 180 | comp.GetIterVar(), |
| 181 | comp.GetIterVar2(), |
| 182 | comp.GetAccuVar(), |
| 183 | accuInit, |
| 184 | loopCond, |
| 185 | loopStep, |
| 186 | result), nil |
| 187 | } |
| 188 | |
| 189 | func exprLiteral(factory ExprFactory, id int64, c *exprpb.Constant) (Expr, error) { |
| 190 | val, err := ConstantToVal(c) |
| 191 | if err != nil { |
| 192 | return nil, err |
| 193 | } |
| 194 | return factory.NewLiteral(id, val), nil |
| 195 | } |
| 196 | |
| 197 | func exprIdent(factory ExprFactory, id int64, i *exprpb.Expr_Ident) (Expr, error) { |
| 198 | return factory.NewIdent(id, i.GetName()), nil |
| 199 | } |
| 200 | |
| 201 | func exprList(factory ExprFactory, id int64, l *exprpb.Expr_CreateList) (Expr, error) { |
| 202 | elems := make([]Expr, len(l.GetElements())) |
| 203 | for i, e := range l.GetElements() { |
| 204 | elem, err := exprInternal(factory, e) |
| 205 | if err != nil { |
| 206 | return nil, err |
| 207 | } |
| 208 | elems[i] = elem |
| 209 | } |
| 210 | return factory.NewList(id, elems, l.GetOptionalIndices()), nil |
| 211 | } |
| 212 | |
| 213 | func exprMap(factory ExprFactory, id int64, s *exprpb.Expr_CreateStruct) (Expr, error) { |
| 214 | entries := make([]EntryExpr, len(s.GetEntries())) |
| 215 | var err error |
| 216 | for i, entry := range s.GetEntries() { |
| 217 | entries[i], err = exprMapEntry(factory, entry.GetId(), entry) |
| 218 | if err != nil { |
| 219 | return nil, err |
| 220 | } |
| 221 | } |
| 222 | return factory.NewMap(id, entries), nil |
| 223 | } |
| 224 | |
| 225 | func exprMapEntry(factory ExprFactory, id int64, e *exprpb.Expr_CreateStruct_Entry) (EntryExpr, error) { |
| 226 | k, err := exprInternal(factory, e.GetMapKey()) |
| 227 | if err != nil { |
| 228 | return nil, err |
| 229 | } |
| 230 | v, err := exprInternal(factory, e.GetValue()) |
| 231 | if err != nil { |
| 232 | return nil, err |
| 233 | } |
| 234 | return factory.NewMapEntry(id, k, v, e.GetOptionalEntry()), nil |
| 235 | } |
| 236 | |
| 237 | func exprSelect(factory ExprFactory, id int64, s *exprpb.Expr_Select) (Expr, error) { |
| 238 | op, err := exprInternal(factory, s.GetOperand()) |
| 239 | if err != nil { |
| 240 | return nil, err |
| 241 | } |
| 242 | if s.GetTestOnly() { |
| 243 | return factory.NewPresenceTest(id, op, s.GetField()), nil |
| 244 | } |
| 245 | return factory.NewSelect(id, op, s.GetField()), nil |
| 246 | } |
| 247 | |
| 248 | func exprStruct(factory ExprFactory, id int64, s *exprpb.Expr_CreateStruct) (Expr, error) { |
| 249 | fields := make([]EntryExpr, len(s.GetEntries())) |
| 250 | var err error |
| 251 | for i, field := range s.GetEntries() { |
| 252 | fields[i], err = exprStructField(factory, field.GetId(), field) |
| 253 | if err != nil { |
| 254 | return nil, err |
| 255 | } |
| 256 | } |
| 257 | return factory.NewStruct(id, s.GetMessageName(), fields), nil |
| 258 | } |
| 259 | |
| 260 | func exprStructField(factory ExprFactory, id int64, f *exprpb.Expr_CreateStruct_Entry) (EntryExpr, error) { |
| 261 | v, err := exprInternal(factory, f.GetValue()) |
| 262 | if err != nil { |
| 263 | return nil, err |
| 264 | } |
| 265 | return factory.NewStructField(id, f.GetFieldKey(), v, f.GetOptionalEntry()), nil |
| 266 | } |
| 267 | |
| 268 | // ExprToProto serializes an ast.Expr value to a protobuf Expr representation. |
| 269 | func ExprToProto(e Expr) (*exprpb.Expr, error) { |
| 270 | if e == nil { |
| 271 | return &exprpb.Expr{}, nil |
| 272 | } |
| 273 | switch e.Kind() { |
| 274 | case CallKind: |
| 275 | return protoCall(e.ID(), e.AsCall()) |
| 276 | case ComprehensionKind: |
| 277 | return protoComprehension(e.ID(), e.AsComprehension()) |
| 278 | case IdentKind: |
| 279 | return protoIdent(e.ID(), e.AsIdent()) |
| 280 | case ListKind: |
| 281 | return protoList(e.ID(), e.AsList()) |
| 282 | case LiteralKind: |
| 283 | return protoLiteral(e.ID(), e.AsLiteral()) |
| 284 | case MapKind: |
| 285 | return protoMap(e.ID(), e.AsMap()) |
| 286 | case SelectKind: |
| 287 | return protoSelect(e.ID(), e.AsSelect()) |
| 288 | case StructKind: |
| 289 | return protoStruct(e.ID(), e.AsStruct()) |
| 290 | case UnspecifiedExprKind: |
| 291 | // Handle the case where a macro reference may be getting translated. |
| 292 | // A nested macro 'pointer' is a non-zero expression id with no kind set. |
| 293 | if e.ID() != 0 { |
| 294 | return &exprpb.Expr{Id: e.ID()}, nil |
| 295 | } |
| 296 | return &exprpb.Expr{}, nil |
| 297 | } |
| 298 | return nil, fmt.Errorf("unsupported expr kind: %v", e) |
| 299 | } |
| 300 | |
| 301 | // EntryExprToProto converts an ast.EntryExpr to a protobuf CreateStruct entry |
| 302 | func EntryExprToProto(e EntryExpr) (*exprpb.Expr_CreateStruct_Entry, error) { |
| 303 | switch e.Kind() { |
| 304 | case MapEntryKind: |
| 305 | return protoMapEntry(e.ID(), e.AsMapEntry()) |
| 306 | case StructFieldKind: |
| 307 | return protoStructField(e.ID(), e.AsStructField()) |
| 308 | case UnspecifiedEntryExprKind: |
| 309 | return &exprpb.Expr_CreateStruct_Entry{}, nil |
| 310 | } |
| 311 | return nil, fmt.Errorf("unsupported expr entry kind: %v", e) |
| 312 | } |
| 313 | |
| 314 | func protoCall(id int64, call CallExpr) (*exprpb.Expr, error) { |
| 315 | var err error |
| 316 | var target *exprpb.Expr |
| 317 | if call.IsMemberFunction() { |
| 318 | target, err = ExprToProto(call.Target()) |
| 319 | if err != nil { |
| 320 | return nil, err |
| 321 | } |
| 322 | } |
| 323 | callArgs := call.Args() |
| 324 | args := make([]*exprpb.Expr, len(callArgs)) |
| 325 | for i, a := range callArgs { |
| 326 | args[i], err = ExprToProto(a) |
| 327 | if err != nil { |
| 328 | return nil, err |
| 329 | } |
| 330 | } |
| 331 | return &exprpb.Expr{ |
| 332 | Id: id, |
| 333 | ExprKind: &exprpb.Expr_CallExpr{ |
| 334 | CallExpr: &exprpb.Expr_Call{ |
| 335 | Function: call.FunctionName(), |
| 336 | Target: target, |
| 337 | Args: args, |
| 338 | }, |
| 339 | }, |
| 340 | }, nil |
| 341 | } |
| 342 | |
| 343 | func protoComprehension(id int64, comp ComprehensionExpr) (*exprpb.Expr, error) { |
| 344 | iterRange, err := ExprToProto(comp.IterRange()) |
| 345 | if err != nil { |
| 346 | return nil, err |
| 347 | } |
| 348 | accuInit, err := ExprToProto(comp.AccuInit()) |
| 349 | if err != nil { |
| 350 | return nil, err |
| 351 | } |
| 352 | loopCond, err := ExprToProto(comp.LoopCondition()) |
| 353 | if err != nil { |
| 354 | return nil, err |
| 355 | } |
| 356 | loopStep, err := ExprToProto(comp.LoopStep()) |
| 357 | if err != nil { |
| 358 | return nil, err |
| 359 | } |
| 360 | result, err := ExprToProto(comp.Result()) |
| 361 | if err != nil { |
| 362 | return nil, err |
| 363 | } |
| 364 | return &exprpb.Expr{ |
| 365 | Id: id, |
| 366 | ExprKind: &exprpb.Expr_ComprehensionExpr{ |
| 367 | ComprehensionExpr: &exprpb.Expr_Comprehension{ |
| 368 | IterVar: comp.IterVar(), |
| 369 | IterVar2: comp.IterVar2(), |
| 370 | IterRange: iterRange, |
| 371 | AccuVar: comp.AccuVar(), |
| 372 | AccuInit: accuInit, |
| 373 | LoopCondition: loopCond, |
| 374 | LoopStep: loopStep, |
| 375 | Result: result, |
| 376 | }, |
| 377 | }, |
| 378 | }, nil |
| 379 | } |
| 380 | |
| 381 | func protoIdent(id int64, name string) (*exprpb.Expr, error) { |
| 382 | return &exprpb.Expr{ |
| 383 | Id: id, |
| 384 | ExprKind: &exprpb.Expr_IdentExpr{ |
| 385 | IdentExpr: &exprpb.Expr_Ident{ |
| 386 | Name: name, |
| 387 | }, |
| 388 | }, |
| 389 | }, nil |
| 390 | } |
| 391 | |
| 392 | func protoList(id int64, list ListExpr) (*exprpb.Expr, error) { |
| 393 | var err error |
| 394 | elems := make([]*exprpb.Expr, list.Size()) |
| 395 | for i, e := range list.Elements() { |
| 396 | elems[i], err = ExprToProto(e) |
| 397 | if err != nil { |
| 398 | return nil, err |
| 399 | } |
| 400 | } |
| 401 | return &exprpb.Expr{ |
| 402 | Id: id, |
| 403 | ExprKind: &exprpb.Expr_ListExpr{ |
| 404 | ListExpr: &exprpb.Expr_CreateList{ |
| 405 | Elements: elems, |
| 406 | OptionalIndices: list.OptionalIndices(), |
| 407 | }, |
| 408 | }, |
| 409 | }, nil |
| 410 | } |
| 411 | |
| 412 | func protoLiteral(id int64, val ref.Val) (*exprpb.Expr, error) { |
| 413 | c, err := ValToConstant(val) |
| 414 | if err != nil { |
| 415 | return nil, err |
| 416 | } |
| 417 | return &exprpb.Expr{ |
| 418 | Id: id, |
| 419 | ExprKind: &exprpb.Expr_ConstExpr{ |
| 420 | ConstExpr: c, |
| 421 | }, |
| 422 | }, nil |
| 423 | } |
| 424 | |
| 425 | func protoMap(id int64, m MapExpr) (*exprpb.Expr, error) { |
| 426 | entries := make([]*exprpb.Expr_CreateStruct_Entry, len(m.Entries())) |
| 427 | var err error |
| 428 | for i, e := range m.Entries() { |
| 429 | entries[i], err = EntryExprToProto(e) |
| 430 | if err != nil { |
| 431 | return nil, err |
| 432 | } |
| 433 | } |
| 434 | return &exprpb.Expr{ |
| 435 | Id: id, |
| 436 | ExprKind: &exprpb.Expr_StructExpr{ |
| 437 | StructExpr: &exprpb.Expr_CreateStruct{ |
| 438 | Entries: entries, |
| 439 | }, |
| 440 | }, |
| 441 | }, nil |
| 442 | } |
| 443 | |
| 444 | func protoMapEntry(id int64, e MapEntry) (*exprpb.Expr_CreateStruct_Entry, error) { |
| 445 | k, err := ExprToProto(e.Key()) |
| 446 | if err != nil { |
| 447 | return nil, err |
| 448 | } |
| 449 | v, err := ExprToProto(e.Value()) |
| 450 | if err != nil { |
| 451 | return nil, err |
| 452 | } |
| 453 | return &exprpb.Expr_CreateStruct_Entry{ |
| 454 | Id: id, |
| 455 | KeyKind: &exprpb.Expr_CreateStruct_Entry_MapKey{ |
| 456 | MapKey: k, |
| 457 | }, |
| 458 | Value: v, |
| 459 | OptionalEntry: e.IsOptional(), |
| 460 | }, nil |
| 461 | } |
| 462 | |
| 463 | func protoSelect(id int64, s SelectExpr) (*exprpb.Expr, error) { |
| 464 | op, err := ExprToProto(s.Operand()) |
| 465 | if err != nil { |
| 466 | return nil, err |
| 467 | } |
| 468 | return &exprpb.Expr{ |
| 469 | Id: id, |
| 470 | ExprKind: &exprpb.Expr_SelectExpr{ |
| 471 | SelectExpr: &exprpb.Expr_Select{ |
| 472 | Operand: op, |
| 473 | Field: s.FieldName(), |
| 474 | TestOnly: s.IsTestOnly(), |
| 475 | }, |
| 476 | }, |
| 477 | }, nil |
| 478 | } |
| 479 | |
| 480 | func protoStruct(id int64, s StructExpr) (*exprpb.Expr, error) { |
| 481 | entries := make([]*exprpb.Expr_CreateStruct_Entry, len(s.Fields())) |
| 482 | var err error |
| 483 | for i, e := range s.Fields() { |
| 484 | entries[i], err = EntryExprToProto(e) |
| 485 | if err != nil { |
| 486 | return nil, err |
| 487 | } |
| 488 | } |
| 489 | return &exprpb.Expr{ |
| 490 | Id: id, |
| 491 | ExprKind: &exprpb.Expr_StructExpr{ |
| 492 | StructExpr: &exprpb.Expr_CreateStruct{ |
| 493 | MessageName: s.TypeName(), |
| 494 | Entries: entries, |
| 495 | }, |
| 496 | }, |
| 497 | }, nil |
| 498 | } |
| 499 | |
| 500 | func protoStructField(id int64, f StructField) (*exprpb.Expr_CreateStruct_Entry, error) { |
| 501 | v, err := ExprToProto(f.Value()) |
| 502 | if err != nil { |
| 503 | return nil, err |
| 504 | } |
| 505 | return &exprpb.Expr_CreateStruct_Entry{ |
| 506 | Id: id, |
| 507 | KeyKind: &exprpb.Expr_CreateStruct_Entry_FieldKey{ |
| 508 | FieldKey: f.Name(), |
| 509 | }, |
| 510 | Value: v, |
| 511 | OptionalEntry: f.IsOptional(), |
| 512 | }, nil |
| 513 | } |
| 514 | |
| 515 | // SourceInfoToProto serializes an ast.SourceInfo value to a protobuf SourceInfo object. |
| 516 | func SourceInfoToProto(info *SourceInfo) (*exprpb.SourceInfo, error) { |
| 517 | if info == nil { |
| 518 | return &exprpb.SourceInfo{}, nil |
| 519 | } |
| 520 | sourceInfo := &exprpb.SourceInfo{ |
| 521 | SyntaxVersion: info.SyntaxVersion(), |
| 522 | Location: info.Description(), |
| 523 | LineOffsets: info.LineOffsets(), |
| 524 | Positions: make(map[int64]int32, len(info.OffsetRanges())), |
| 525 | MacroCalls: make(map[int64]*exprpb.Expr, len(info.MacroCalls())), |
| 526 | } |
| 527 | for id, offset := range info.OffsetRanges() { |
| 528 | sourceInfo.Positions[id] = offset.Start |
| 529 | } |
| 530 | for id, e := range info.MacroCalls() { |
| 531 | call, err := ExprToProto(e) |
| 532 | if err != nil { |
| 533 | return nil, err |
| 534 | } |
| 535 | sourceInfo.MacroCalls[id] = call |
| 536 | } |
| 537 | return sourceInfo, nil |
| 538 | } |
| 539 | |
| 540 | // ProtoToSourceInfo deserializes the protobuf into a native SourceInfo value. |
| 541 | func ProtoToSourceInfo(info *exprpb.SourceInfo) (*SourceInfo, error) { |
| 542 | sourceInfo := &SourceInfo{ |
| 543 | syntax: info.GetSyntaxVersion(), |
| 544 | desc: info.GetLocation(), |
| 545 | lines: info.GetLineOffsets(), |
| 546 | offsetRanges: make(map[int64]OffsetRange, len(info.GetPositions())), |
| 547 | macroCalls: make(map[int64]Expr, len(info.GetMacroCalls())), |
| 548 | } |
| 549 | for id, offset := range info.GetPositions() { |
| 550 | sourceInfo.SetOffsetRange(id, OffsetRange{Start: offset, Stop: offset}) |
| 551 | } |
| 552 | for id, e := range info.GetMacroCalls() { |
| 553 | call, err := ProtoToExpr(e) |
| 554 | if err != nil { |
| 555 | return nil, err |
| 556 | } |
| 557 | sourceInfo.SetMacroCall(id, call) |
| 558 | } |
| 559 | return sourceInfo, nil |
| 560 | } |
| 561 | |
| 562 | // ReferenceInfoToProto converts a ReferenceInfo instance to a protobuf Reference suitable for serialization. |
| 563 | func ReferenceInfoToProto(info *ReferenceInfo) (*exprpb.Reference, error) { |
| 564 | c, err := ValToConstant(info.Value) |
| 565 | if err != nil { |
| 566 | return nil, err |
| 567 | } |
| 568 | return &exprpb.Reference{ |
| 569 | Name: info.Name, |
| 570 | OverloadId: info.OverloadIDs, |
| 571 | Value: c, |
| 572 | }, nil |
| 573 | } |
| 574 | |
| 575 | // ProtoToReferenceInfo converts a protobuf Reference into a CEL-native ReferenceInfo instance. |
| 576 | func ProtoToReferenceInfo(ref *exprpb.Reference) (*ReferenceInfo, error) { |
| 577 | v, err := ConstantToVal(ref.GetValue()) |
| 578 | if err != nil { |
| 579 | return nil, err |
| 580 | } |
| 581 | return &ReferenceInfo{ |
| 582 | Name: ref.GetName(), |
| 583 | OverloadIDs: ref.GetOverloadId(), |
| 584 | Value: v, |
| 585 | }, nil |
| 586 | } |
| 587 | |
| 588 | // ValToConstant converts a CEL-native ref.Val to a protobuf Constant. |
| 589 | // |
| 590 | // Only simple scalar types are supported by this method. |
| 591 | func ValToConstant(v ref.Val) (*exprpb.Constant, error) { |
| 592 | if v == nil { |
| 593 | return nil, nil |
| 594 | } |
| 595 | switch v.Type() { |
| 596 | case types.BoolType: |
| 597 | return &exprpb.Constant{ConstantKind: &exprpb.Constant_BoolValue{BoolValue: v.Value().(bool)}}, nil |
| 598 | case types.BytesType: |
| 599 | return &exprpb.Constant{ConstantKind: &exprpb.Constant_BytesValue{BytesValue: v.Value().([]byte)}}, nil |
| 600 | case types.DoubleType: |
| 601 | return &exprpb.Constant{ConstantKind: &exprpb.Constant_DoubleValue{DoubleValue: v.Value().(float64)}}, nil |
| 602 | case types.IntType: |
| 603 | return &exprpb.Constant{ConstantKind: &exprpb.Constant_Int64Value{Int64Value: v.Value().(int64)}}, nil |
| 604 | case types.NullType: |
| 605 | return &exprpb.Constant{ConstantKind: &exprpb.Constant_NullValue{NullValue: structpb.NullValue_NULL_VALUE}}, nil |
| 606 | case types.StringType: |
| 607 | return &exprpb.Constant{ConstantKind: &exprpb.Constant_StringValue{StringValue: v.Value().(string)}}, nil |
| 608 | case types.UintType: |
| 609 | return &exprpb.Constant{ConstantKind: &exprpb.Constant_Uint64Value{Uint64Value: v.Value().(uint64)}}, nil |
| 610 | } |
| 611 | return nil, fmt.Errorf("unsupported constant kind: %v", v.Type()) |
| 612 | } |
| 613 | |
| 614 | // ConstantToVal converts a protobuf Constant to a CEL-native ref.Val. |
| 615 | func ConstantToVal(c *exprpb.Constant) (ref.Val, error) { |
| 616 | return AlphaProtoConstantAsVal(c) |
| 617 | } |
| 618 | |
| 619 | // AlphaProtoConstantAsVal converts a v1alpha1.Constant protobuf to a CEL-native ref.Val. |
| 620 | func AlphaProtoConstantAsVal(c *exprpb.Constant) (ref.Val, error) { |
| 621 | if c == nil { |
| 622 | return nil, nil |
| 623 | } |
| 624 | canonical := &celpb.Constant{} |
| 625 | if err := convertProto(c, canonical); err != nil { |
| 626 | return nil, err |
| 627 | } |
| 628 | return ProtoConstantAsVal(canonical) |
| 629 | } |
| 630 | |
| 631 | // ProtoConstantAsVal converts a canonical celpb.Constant protobuf to a CEL-native ref.Val. |
| 632 | func ProtoConstantAsVal(c *celpb.Constant) (ref.Val, error) { |
| 633 | switch c.GetConstantKind().(type) { |
| 634 | case *celpb.Constant_BoolValue: |
| 635 | return types.Bool(c.GetBoolValue()), nil |
| 636 | case *celpb.Constant_BytesValue: |
| 637 | return types.Bytes(c.GetBytesValue()), nil |
| 638 | case *celpb.Constant_DoubleValue: |
| 639 | return types.Double(c.GetDoubleValue()), nil |
| 640 | case *celpb.Constant_Int64Value: |
| 641 | return types.Int(c.GetInt64Value()), nil |
| 642 | case *celpb.Constant_NullValue: |
| 643 | return types.NullValue, nil |
| 644 | case *celpb.Constant_StringValue: |
| 645 | return types.String(c.GetStringValue()), nil |
| 646 | case *celpb.Constant_Uint64Value: |
| 647 | return types.Uint(c.GetUint64Value()), nil |
| 648 | } |
| 649 | return nil, fmt.Errorf("unsupported constant kind: %v", c.GetConstantKind()) |
| 650 | } |
| 651 | |
| 652 | func convertProto(src, dst proto.Message) error { |
| 653 | pb, err := proto.Marshal(src) |
| 654 | if err != nil { |
| 655 | return err |
| 656 | } |
| 657 | err = proto.Unmarshal(pb, dst) |
| 658 | return err |
| 659 | } |