MCPcopy Create free account
hub / github.com/cel-expr/cel-go / ProtoAsDeclaration

Function ProtoAsDeclaration

cel/decls.go:413–454  ·  view source on GitHub ↗

ProtoAsDeclaration converts a canonical celpb.Decl value describing a variable or function into an EnvOption.

(d *celpb.Decl)

Source from the content-addressed store, hash-verified

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

Callers 2

conformanceTestFunction · 0.92
AlphaProtoAsDeclarationFunction · 0.85

Calls 11

ProtoAsTypeFunction · 0.92
MemberOverloadFunction · 0.92
OverloadFunction · 0.92
ProtoConstantAsValFunction · 0.92
ConstantFunction · 0.85
GetValueMethod · 0.80
FunctionFunction · 0.70
VariableFunction · 0.70
GetParamsMethod · 0.65
GetNameMethod · 0.45
GetTypeMethod · 0.45

Tested by 1

conformanceTestFunction · 0.74