ProtoAsDeclaration converts a canonical celpb.Decl value describing a variable or function into an EnvOption.
(d *celpb.Decl)
| 385 | |
| 386 | // ProtoAsDeclaration converts a canonical celpb.Decl value describing a variable or function into an EnvOption. |
| 387 | func ProtoAsDeclaration(d *celpb.Decl) (EnvOption, error) { |
| 388 | switch d.GetDeclKind().(type) { |
| 389 | case *celpb.Decl_Function: |
| 390 | overloads := d.GetFunction().GetOverloads() |
| 391 | opts := make([]FunctionOpt, len(overloads)) |
| 392 | for i, o := range overloads { |
| 393 | args := make([]*Type, len(o.GetParams())) |
| 394 | for j, p := range o.GetParams() { |
| 395 | a, err := types.ProtoAsType(p) |
| 396 | if err != nil { |
| 397 | return nil, err |
| 398 | } |
| 399 | args[j] = a |
| 400 | } |
| 401 | res, err := types.ProtoAsType(o.GetResultType()) |
| 402 | if err != nil { |
| 403 | return nil, err |
| 404 | } |
| 405 | if o.IsInstanceFunction { |
| 406 | opts[i] = decls.MemberOverload(o.GetOverloadId(), args, res) |
| 407 | } else { |
| 408 | opts[i] = decls.Overload(o.GetOverloadId(), args, res) |
| 409 | } |
| 410 | } |
| 411 | return Function(d.GetName(), opts...), nil |
| 412 | case *celpb.Decl_Ident: |
| 413 | t, err := types.ProtoAsType(d.GetIdent().GetType()) |
| 414 | if err != nil { |
| 415 | return nil, err |
| 416 | } |
| 417 | if d.GetIdent().GetValue() == nil { |
| 418 | return Variable(d.GetName(), t), nil |
| 419 | } |
| 420 | val, err := ast.ProtoConstantAsVal(d.GetIdent().GetValue()) |
| 421 | if err != nil { |
| 422 | return nil, err |
| 423 | } |
| 424 | return Constant(d.GetName(), t, val), nil |
| 425 | default: |
| 426 | return nil, fmt.Errorf("unsupported decl: %v", d) |
| 427 | } |
| 428 | } |