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

Function ProtoAsDeclaration

cel/decls.go:417–458  ·  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

415
416// ProtoAsDeclaration converts a canonical celpb.Decl value describing a variable or function into an EnvOption.
417func 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}

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