bsloane1650 commented on a change in pull request #273: WIP: Add User Defined Functions Capability URL: https://github.com/apache/incubator-daffodil/pull/273#discussion_r332236798
########## 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 + Review comment: A UDF is pure within the context of a single parse. Across parses, I see no requirement that they behave the same. One example might be embedding version information in the parsed document, along the lines of: <xs:element type="xs:string" name="processorVersion" dfdl:inputValueCalc="ns:getProcessorVersion()" /> This version string might include what version of the user UDF library is being used and, depending on the user setup, could reasonably include what version of Daffodil and Daffodil schema is being used, git commit of the current branch of the user project, etc. Under a reasonable implementation, this value wouldn't change in the middle of a parse, so there should be no concerns with regards to it changing after backtracking. The "variable" I was referring to is DFDL variables, which are actually more stateful than what I am suggesting. Really what I am suggesting is analogous the the usecase of DFDL variables where they are supplied at the command-line/api level and effectively a constant during a parse. ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services