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

Function functionDeclToExprDecl

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

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