ProtoAsDeclaration converts a canonical celpb.Decl value describing a variable or function into an EnvOption.
(d *celpb.Decl)
| 415 | |
| 416 | // ProtoAsDeclaration converts a canonical celpb.Decl value describing a variable or function into an EnvOption. |
| 417 | func ProtoAsDeclaration(d *celpb.Decl) (EnvOption, error) { |
| 418 | switch d.GetDeclKind().(type) { |
| 419 | case *celpb.Decl_Function: |
| 420 | overloads := d.GetFunction().GetOverloads() |
| 421 | opts := make([]FunctionOpt, len(overloads)) |
| 422 | for i, o := range overloads { |
| 423 | args := make([]*Type, len(o.GetParams())) |
| 424 | for j, p := range o.GetParams() { |
| 425 | a, err := types.ProtoAsType(p) |
| 426 | if err != nil { |
| 427 | return nil, err |
| 428 | } |
| 429 | args[j] = a |
| 430 | } |
| 431 | res, err := types.ProtoAsType(o.GetResultType()) |
| 432 | if err != nil { |
| 433 | return nil, err |
| 434 | } |
| 435 | if o.IsInstanceFunction { |
| 436 | opts[i] = decls.MemberOverload(o.GetOverloadId(), args, res) |
| 437 | } else { |
| 438 | opts[i] = decls.Overload(o.GetOverloadId(), args, res) |
| 439 | } |
| 440 | } |
| 441 | return Function(d.GetName(), opts...), nil |
| 442 | case *celpb.Decl_Ident: |
| 443 | t, err := types.ProtoAsType(d.GetIdent().GetType()) |
| 444 | if err != nil { |
| 445 | return nil, err |
| 446 | } |
| 447 | if d.GetIdent().GetValue() == nil { |
| 448 | return Variable(d.GetName(), t), nil |
| 449 | } |
| 450 | val, err := ast.ProtoConstantAsVal(d.GetIdent().GetValue()) |
| 451 | if err != nil { |
| 452 | return nil, err |
| 453 | } |
| 454 | return Constant(d.GetName(), t, val), nil |
| 455 | default: |
| 456 | return nil, fmt.Errorf("unsupported decl: %v", d) |
| 457 | } |
| 458 | } |