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

Function functionDeclToExprDecl

common/decls/decls.go:1214–1254  ·  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

1212
1213// functionDeclToExprDecl converts a go-native function declaration into a protobuf-typed function declaration.
1214func functionDeclToExprDecl(f *FunctionDecl) (*exprpb.Decl, error) {
1215 overloads := make([]*exprpb.Decl_FunctionDecl_Overload, len(f.overloads))
1216 for i, oID := range f.overloadOrdinals {
1217 o := f.overloads[oID]
1218 paramNames := map[string]struct{}{}
1219 argTypes := make([]*exprpb.Type, len(o.ArgTypes()))
1220 for j, a := range o.ArgTypes() {
1221 collectParamNames(paramNames, a)
1222 at, err := types.TypeToExprType(a)
1223 if err != nil {
1224 return nil, err
1225 }
1226 argTypes[j] = at
1227 }
1228 collectParamNames(paramNames, o.ResultType())
1229 resultType, err := types.TypeToExprType(o.ResultType())
1230 if err != nil {
1231 return nil, err
1232 }
1233 if len(paramNames) == 0 {
1234 if o.IsMemberFunction() {
1235 overloads[i] = chkdecls.NewInstanceOverload(oID, argTypes, resultType)
1236 } else {
1237 overloads[i] = chkdecls.NewOverload(oID, argTypes, resultType)
1238 }
1239 } else {
1240 params := []string{}
1241 for pn := range paramNames {
1242 params = append(params, pn)
1243 }
1244 if o.IsMemberFunction() {
1245 overloads[i] = chkdecls.NewParameterizedInstanceOverload(oID, argTypes, resultType, params)
1246 } else {
1247 overloads[i] = chkdecls.NewParameterizedOverload(oID, argTypes, resultType, params)
1248 }
1249 }
1250 doc := common.MultilineDescription(o.Examples()...)
1251 overloads[i].Doc = doc
1252 }
1253 return chkdecls.NewFunctionWithDoc(f.Name(), f.Description(), overloads...), nil
1254}
1255
1256func collectParamNames(paramNames map[string]struct{}, arg *types.Type) {
1257 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