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

Function ProtoAsDeclaration

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

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

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