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

Function functionDeclToExprDecl

common/decls/decls.go:1030–1070  ·  view source on GitHub ↗

functionDeclToExprDecl converts a go-native function declaration into a protobuf-typed function declaration.

(f *FunctionDecl)

Source from the content-addressed store, hash-verified

1028
1029// functionDeclToExprDecl converts a go-native function declaration into a protobuf-typed function declaration.
1030func functionDeclToExprDecl(f *FunctionDecl) (*exprpb.Decl, error) {
1031 overloads := make([]*exprpb.Decl_FunctionDecl_Overload, len(f.overloads))
1032 for i, oID := range f.overloadOrdinals {
1033 o := f.overloads[oID]
1034 paramNames := map[string]struct{}{}
1035 argTypes := make([]*exprpb.Type, len(o.ArgTypes()))
1036 for j, a := range o.ArgTypes() {
1037 collectParamNames(paramNames, a)
1038 at, err := types.TypeToExprType(a)
1039 if err != nil {
1040 return nil, err
1041 }
1042 argTypes[j] = at
1043 }
1044 collectParamNames(paramNames, o.ResultType())
1045 resultType, err := types.TypeToExprType(o.ResultType())
1046 if err != nil {
1047 return nil, err
1048 }
1049 if len(paramNames) == 0 {
1050 if o.IsMemberFunction() {
1051 overloads[i] = chkdecls.NewInstanceOverload(oID, argTypes, resultType)
1052 } else {
1053 overloads[i] = chkdecls.NewOverload(oID, argTypes, resultType)
1054 }
1055 } else {
1056 params := []string{}
1057 for pn := range paramNames {
1058 params = append(params, pn)
1059 }
1060 if o.IsMemberFunction() {
1061 overloads[i] = chkdecls.NewParameterizedInstanceOverload(oID, argTypes, resultType, params)
1062 } else {
1063 overloads[i] = chkdecls.NewParameterizedOverload(oID, argTypes, resultType, params)
1064 }
1065 }
1066 doc := common.MultilineDescription(o.Examples()...)
1067 overloads[i].Doc = doc
1068 }
1069 return chkdecls.NewFunctionWithDoc(f.Name(), f.Description(), overloads...), nil
1070}
1071
1072func collectParamNames(paramNames map[string]struct{}, arg *types.Type) {
1073 if arg.Kind() == types.TypeParamKind {

Callers 1

FunctionDeclToExprDeclFunction · 0.85

Calls 9

TypeToExprTypeFunction · 0.92
MultilineDescriptionFunction · 0.92
collectParamNamesFunction · 0.85
ArgTypesMethod · 0.80
ExamplesMethod · 0.80
IsMemberFunctionMethod · 0.65
NameMethod · 0.65
DescriptionMethod · 0.65
ResultTypeMethod · 0.45

Tested by

no test coverage detected