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

Function functionDeclToExprDecl

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

1114
1115// functionDeclToExprDecl converts a go-native function declaration into a protobuf-typed function declaration.
1116func functionDeclToExprDecl(f *FunctionDecl) (*exprpb.Decl, error) {
1117 overloads := make([]*exprpb.Decl_FunctionDecl_Overload, len(f.overloads))
1118 for i, oID := range f.overloadOrdinals {
1119 o := f.overloads[oID]
1120 paramNames := map[string]struct{}{}
1121 argTypes := make([]*exprpb.Type, len(o.ArgTypes()))
1122 for j, a := range o.ArgTypes() {
1123 collectParamNames(paramNames, a)
1124 at, err := types.TypeToExprType(a)
1125 if err != nil {
1126 return nil, err
1127 }
1128 argTypes[j] = at
1129 }
1130 collectParamNames(paramNames, o.ResultType())
1131 resultType, err := types.TypeToExprType(o.ResultType())
1132 if err != nil {
1133 return nil, err
1134 }
1135 if len(paramNames) == 0 {
1136 if o.IsMemberFunction() {
1137 overloads[i] = chkdecls.NewInstanceOverload(oID, argTypes, resultType)
1138 } else {
1139 overloads[i] = chkdecls.NewOverload(oID, argTypes, resultType)
1140 }
1141 } else {
1142 params := []string{}
1143 for pn := range paramNames {
1144 params = append(params, pn)
1145 }
1146 if o.IsMemberFunction() {
1147 overloads[i] = chkdecls.NewParameterizedInstanceOverload(oID, argTypes, resultType, params)
1148 } else {
1149 overloads[i] = chkdecls.NewParameterizedOverload(oID, argTypes, resultType, params)
1150 }
1151 }
1152 doc := common.MultilineDescription(o.Examples()...)
1153 overloads[i].Doc = doc
1154 }
1155 return chkdecls.NewFunctionWithDoc(f.Name(), f.Description(), overloads...), nil
1156}
1157
1158func collectParamNames(paramNames map[string]struct{}, arg *types.Type) {
1159 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