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

Function ProtoAsDeclaration

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

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

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