stevedlawrence commented on a change in pull request #273: WIP: Add User
Defined Functions Capability
URL: https://github.com/apache/incubator-daffodil/pull/273#discussion_r331682771
##########
File path:
daffodil-core/src/main/scala/org/apache/daffodil/dpath/Expression.scala
##########
@@ -1870,6 +1871,47 @@ case class FunctionCallExpression(functionQNameString:
String, expressions: List
case (RefQName(_, "unsignedByte", XSD), args) =>
XSConverterExpr(functionQNameString, functionQName, args,
NodeInfo.UnsignedByte)
+ case (RefQName(Some(_), _, _), args) => {
+ val namespace = functionQName.namespace.toString()
+ val fName = functionQName.local
+
+ lazy val udfservice = {
+ val a = UDFService
+ a.warnings.map { w => SDW(WarnID.UserDefinedFunction, w) }
+ val allErrors = a.errors.mkString("\n\n")
+ SDE(s"Function unknown: fname[${fName}]
fnamespace[${namespace}].\n$allErrors")
+ a
+ }
+
+ val fcObject = udfservice.udfs.lookupFunctionClass(namespace, fName)
+
+ if (fcObject == null) {
+ SDE("Function not found: fname[%s] fnamespace[%s]. Currently
registered UDFs:\n%s", fName, namespace, udfservice.allFunctionClasses)
+ }
+
+ val fcClassType = fcObject.getClass
+
+ val paramTypesReturnTypeTuple: Array[(Array[Class[_]], Class[_])] =
fcClassType.getMethods.collect {
+ case p if p.getName == "evaluate" => (p.getParameterTypes,
p.getReturnType)
+ }
+
+ if (paramTypesReturnTypeTuple.isEmpty) {
+ SDE("Missing evaluate method for function provided: name[%s]
namespace[%s]", fName, namespace)
+ }
+
+ if (paramTypesReturnTypeTuple.length > 1) {
+ SDE("Only one evaluate method allowed per function class: name[%s]
namespace[%s]", fName, namespace)
+ }
+
+ val paramTypes: Array[Class[_]] = paramTypesReturnTypeTuple.head._1
+ val retType: Class[_] = paramTypesReturnTypeTuple.head._2
+
+ val evaluateParamTypes: List[NodeInfo.Kind] = paramTypes.map { c =>
NodeInfo.fromClassTypeName(c.getTypeName) }.toList
+ val evaluateReturnType =
NodeInfo.fromClassTypeName(retType.getTypeName)
+
+ UDFunctionCallExpr(functionQNameString, functionQName, args,
evaluateParamTypes, evaluateReturnType, UDFunctionCall(_, fcObject))
+ }
+
case _ => SDE("Unsupported function: %s", functionQName)
Review comment:
I think we should just require UDF's to be called with a namespace but not
necessarily require a prefix. For example, we could have something like this::
```xsd
<xs:schema xmlns="http://foo.com" xmlns:xs="http://www.w3.org/2001/XMLSchem">
<xs:element name="foo" dfdl:inputValueCalc="{ myfunc() }" />
</xs:schema>
```
In this example, we define the default namespace using ``xmlns=``, so
anything without a prefix will be in that namespace. Since ``myfunc()`` is not
prefixed it will be in the ``http://foo.com`` namespace.
Note that the functions we normally see are either in the ``dfdl`` or ``fn``
namespace. And since it never really makes sense to set the default namespace
to one of these the prefix is always needed. But schemas can be designed so the
prefix isn't required.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services