Invalid coercion: () as xs:string in xdmp:xslt-eval - xslt-2.0

I am making use of MarkLogic's ability to call XQuery functions in the XSL transform.
Let's say I have an XQuery library with a function whose signature looks like the following. This is for illustrative purposes only.
declare function my-func:ex-join($first as xs:string, $last as xs:string) as xs:string
{
fn:concat($first, '-', $last)
}
From XQuery, I can call this function with empty sequence as parameters, with no issues, i.e.
ex-join((), '1244')
The function will just return an empty sequence, but I don't get any errors. If I try to all this function from with in my XSL transform,as in:
<xsl:value-of select="my-func:ex-join(//node/value/text(), 'something')"/>
If the /node/value does not exist, and an empty sequence is passed in, I get the coercion error.
Does anyone have suggestions to work around the coercion problem, outside of checking the values in XSL prior to the select? These are auto-generated XSL templates, which would mean a lot of coded checks.
Thanks,
-tj

Attempts to invoke that function in XQuery would fail too. It is probably due to function mapping that you don't notice this though. Put the following at the top of your XQuery code:
declare option xdmp:mapping "false";
Next to this, you only need to change the signature of your function to accept empty-sequences. Replace as xs:string with as xs:string?:
declare function my-func:ex-join($first as xs:string?, $last as xs:string?) as xs:string
fn:concat will accept empty sequences as arguments, so no further changes required to make it work..
HTH!

Related

Find function/method body explicit dependency types using Dart analyzer package

I would like to understand how can I analyze methods / functions body to find types that are explicitly referenced from it. I have success analyzing method declaration (return type, parameter types, etc..), however I have no idea how to do that for body.
Assuming following function:
String someFunction(int param) {
final list = <String>['a', 'b', 'c']; // -> DartTypes: String, List<String>
final myClass = MyClass<Arg>(); // -> DartTypes: Arg, MyClass<Arg>
final functionCall = anotherFunction<FunctionArg<Arg>>(); // -> DartTypes: Arg, FunctionArg<Arg>
return 'result';
}
// At is point I would like to know that my function depends on
// String, List<String>, Arg, MyClass<Arg>, FunctionArg<Arg>
// in term of DartType instances with proper typeArguments.
I tried getting AstNode for method element described here: https://stackoverflow.com/a/57043177/2033394
However I could not get elements from nodes to figure out their types. Their declaredElement values are always null. So I can not get back to Element API from AST API.
If you've used the exact snippet from the answer you've referenced, the problem is likely in getParsedLibraryByElement(). This method only parses the referenced library - meaning that you'll get an AST that doesn't necessarily have semantic references (like the declaredElement of AST nodes) set.
Instead, you'll want to use getResolvedLibraryByElement. The AST returned by that method will have its types and references fully resolved.
With the resolved AST, you could visit the body of the method with a custom visitor to find type references. Your definition of "referenced types" isn't really exact - but perhaps you can collect types in visitNamedType for type references and visitVariableDeclaration to collect the types of variables.

`Error: request to generate code for .compileTime proc`

I have a small helper proc that is supposed to tell me at compile-time whether a type is an object-type or not.
func isObject*[T](val: typedesc[T]): bool {.compileTime.} = T is (object or ref object)
However, when I call this proc with a simple echo to see whether it works, I receive an error:
type A = object
echo isObject(A)
Error: request to generate code for .compileTime proc: isObject
Why is that? It should be perfectly valid to just call this, isObject should just compile to true and in the end what's written there is echo true, why does this cause this cryptic error?
The problem here is that runtime code (The echo call) is trying to work with a compiletime proc.
That is not valid, as the compiler would not replace the function-call with its result, but try to actually call the function at runtime instead. The compiler knows this is invalid behaviour and thus prohibits it by throwing an error, albeit one that isn't that useful.
The only way this can be allowed is if you store the result of the compile-time proc in a compile-time variable, aka a const. These are allowed to be used at runtime.
So the calling code would look more like this instead:
type A = object
const x = isObject(A)
echo x
EDIT:
As Elegantbeef pointed out on nim's discord:
Another alternative is to just do what I thought would happen initially and have that isObject(A) call evaluate fully at compile-time, so that at runtime it goes away and all that's left is it's result, true.
To do so, just use static:
type A = object
echo static(isObject(A))

Can't query baseX using an Xquery with "&"

I'm trying to use the baseX REST API with python's requests post method, using a saved .xq file which contains a query with an &.
When running this saved query directly on baseX, there's no problem.
The request as presented in the response also includes the & as it is and not as an &, but I still get the following error (response code is 400):
" Stopped at C:/Program Files (x86)/BaseX/webapp, 37/37:\n[XPST0003] Invalid entity: '&&", "||", "!")) the...'.' "
The relevant part of the request's body is:
<rest:query xmlns:rest="http://basex.org/rest"> <rest:text>declare function local:enrich_node($attr, $supertype) {
$attr, attribute {"supertype"} {$supertype}
};
declare function local:enrich($n as node()) as node() {
typeswitch($n)
...
case $e as element(operator)
return
...
else if ($e/text()=("&&", "||", "!")) then
element {name($e)}
{local:enrich_node($e/#*, "boolop"), for $c in $e/(* | text())
return local:enrich($c) }
else
...
};
declare variable $assign_id as xs:string external;
declare variable $submission_id as xs:string external;
for $node in db:open($assign_id, $submission_id)
return local:enrich($node)</rest:text><variable name="assign_id" value="val1"/><variable name="submission_id" value="val2"/></rest:query>
When I remove the && part from the query it works.
I tried to look for relevant questions but didn't find anything, other then a suggestion to "escape" it with another & which I tried but then the returned error was with 4 &s.
Any ideas?
As the content of rest:text has to be evaluated as XQuery code but should not be parsed as XML it should help to wrap the XQuery code inside of rest:text in a CDATA section.

JQuery UI spinner spin event not as expected in Scala JS

When I define a spinner in ScalaJS and handle the spin value I am not able to get the new spin value in the event as I would have expected. According to the JQuery UI documentation the second parameter to the spin event is the ui object that contains a value attribute. So I defined a trait:
trait Number extends js.Object {
val value: Int = js.native
}
And then handle my spin event thus:
jQuery("#mySpinner").spinner(js.Dynamic.literal(spin = { (e: HTMLInputElement, ui: Number) =>
log("Change: " + ui.value)
}: js.ThisFunction1[HTMLInputElement, Number, Any]))
But the "value" attribute does not seem to be a member of the ui object as I get the exception below in my log statement. Can someone tell me what I am doing wrong?
uncaught exception: scala.scalajs.runtime.UndefinedBehaviorError: An
undefined behavior was detected: undefined is not an instance of
java.lang.Integer
You say e: HTMLInputElement but it should be e: Event
I suspect the problem is a combination of the previous comments. You are correct that, since you're using ThisFunction, the first element should be an Element of some sort. (Although, is it really an HTMLInputElement? That's a slightly unusual element type to put a spinner on.)
But that Element gets prepended to the function parameters, whereas you've got it replacing one.
In other words, you have
(e: HTMLInputElement, ui: Number)
but it needs to be
(elem: HTMLInputElement, e:Event, ui: Number)
in order to match the expected signature. So in practice, the system is trying to cast the value member of an Event, which of course doesn't exist, to Integer. It finds that value is undefined, tries to cast it to Integer, and boom.
I can't say I'm 100% certain (and IMO that ui parameter is just plain weird to begin with -- I'm a little suspicious of the jQueryUI documentation there), but that's my guess. Try fixing the signature of your call, and see if the error goes away...

Why does io\open not work in moonscript?

Moonscript uses \ to call methods so can someone explain to me why the code below does not work:
> file = io\open("mix.exs", "rb")
[string "tmp"]:1: calling 'open' on bad self (string expected, got table)
but when you call it to read the file it does ?
> file\read!
"Code.ensure_loaded?(Hex) and Hex.start
The io.open function expects to get a string as the first argument but io\open (like io:open in lua itself) is actually passing the io table as the first argument. That is it is a method call.
io\open("file", "mode")/io:open("file", "mode") are syntactic sugar for io.open(io, "file", "mode").
This is why file\read! works without an explicit argument because file gets passed as the first argument to the read("file", "format") function.
Moonscript uses \ to call methods
to call member methods. as in a\b c, ... translates to a.b(a,c,...).
this doesn't work here because io.open is a static function (io.open(what,how)), not a member (io.open(self,what,how)).
you couldn't call io:open in Lua either. the only place where io functions allow for being called as members is when you want to read/write stdio.
but when you call it to read the file it does ?
because now it's a member method of the file. you're actually still using io.read there, but the file object has io as a metatable index, therefore allowing you to access the same function via file.read, and since file\read! translates to file.read(file) it's the same thing.
so essentially the answer boils down to "because io:open doesn't work in Lua".

Resources