Incomplete FLWOR expression: expecting 'return' - if-statement

I am having some trouble with the if-then-else command of the XQuery.
Currently I am using BaseX to edit XQuery (if that matters!)
if ($item/pf3:Current/pf3:Name) then (
let $Name := "None"
) else (
let $Name := data($item/pf3:Current/pf3:Name)
)
This piece throws an error saying:
[XPST0003] Incomplete FLWOR expression: expecting 'return'.

There is a small problem with your xquery. Here is the corrected version -
let $Name :=
if ($item/pf3:Current/pf3:Name)
then "None"
else data($item/pf3:Current/pf3:Name)
If there are no return statements following the above assignment statement, you can append the following return statement after the above statements -
return $Name

Related

FreeRadius Configuration (radiusd.conf) - Regex-Problem unlang

I am currently facing a problem within my Radius configuration and wanted to ask you for help.
I'am using the FreeRadius-Version 3.0.23
Within the authorize section in radiusd.conf I am trying to create the following unlang expression.
I have users in the following format:
super1
super2
...
user1
user2
NAS-Identifier:
SUP-A
SUP-B
SUP-C
SUP-D
I want to extract something from the NAS identifier using a regex and append to the user.
=> super1-A
=> super2-D
However, it doesn't work with the following expression because the extracted value is no longer available in the second IF statement.
if ( NAS-Identifier =~ /^SUP\\-([ABCD])/ ) {
=> The extracted value is only available at this point
=> Or is it possible to define a local variable with the value?
if ( "%{User-Name}" !~ /^super\\d+/ )
update request {
User-Name := "%{User-Name}-%{1}" (However, I need the value here)
}
}
}
This is my workaround:
if ( "%{User-Name}" !~ /^super\\d+/ && NAS-Identifier =~ /^SUP\\-([ABCD])/ ) {
update request {
User-Name := "%{User-Name}-%{1}"
}
}
I would really appreciate your help as I couldn't find anything in the FreeRadius documentation.
Thanks in advance.
According to the FreeRadius documentation:
Every time a regular expression is evaluated, whether it matches or not, the capture group values will be cleared.
So, in your case, you can reverse the order of conditions:
if ( "%{User-Name}" !~ /^super\\d+/ ) {
if ( NAS-Identifier =~ /^SUP-([ABCD])/ ) {
update request {
User-Name := "%{User-Name}-%{1}"
}
}
}
Note that the - char is not any special regex metacharacter when used outside character classes, no need to escape it here.

Raku grammar action throwing "Cannot bind attributes in a Nil type object. Did you forget a '.new'?" error when using "make"

I have this method in a class that's throwing a Cannot bind attributes in a Nil type object. Did you forget a '.new'?
method parse() {
grammar FindHeaders {
token TOP { [<not-header> | <header>]+ $ }
token not-header { ^^ <![#]> \N* \n }
token header { ^^ '#'{ 1 .. 6 } <content> \n }
token content { \N+ }
}
class HeaderActions {
method content($match) {
return if $match ~~ m/^^\#\s+<[A..Z]>+e*s/ || $match !~~ m/<[a..z]>/;
return if $match ~~ m/\|/ && ( $match ~~ m:i/project/ || $match ~~ m:i/\+\w/ );
my $tc = Lingua::EN::Titlecase.new($match);
my $new_title = $tc.title;
make($new_title);
}
}
my $t = $!content;
FindHeaders.parse($t, actions => HeaderActions.new);
}
As far as I can tell, this code matches what's in the official documentation. So not sure why I'm getting this error. I have no idea what attribute or Nil object the compiler is referring to. If I comment out the line with the make method, everything works fine.
method content($match) {
There's a reason that action methods typically use $/ as the argument name: because the make function looks for $/ in order to associate the provided object to it. You can use $match, but then need to call the make method on that instead:
$match.make($new_title);
The mention of Nil is because the failed match earlier in the action method resulted in $/ being set to Nil.
I guess you avoided the more idiomatic $/ as the parameter of the action method because it gets in the way of doing further matching in the action method. Doing further matching in action methods means that the text is being parsed twice (once in the grammar and again the action), which is not so efficient, and usually best avoided (by moving the parsing work into the grammar).
As a final style point, declaring grammars and action classes in a method is neat encapsulation if they are only used there, but it would be wise to my scope them (my grammar FindHeaders { ... }), otherwise they shall end up installed in the nearest enclosing package anyway.
Err - bit of a guess here but looks like this error is generated during creation of a new object. That points to the line my $tc = Lingua::EN::Titlecase.new($match). I wonder if you want to pass a Str into this function call e.g. with "$match" or ~$match...

Debugging Nim source filter

Can someone point out what's syntactically wrong with this source filter (as documented here - https://nim-lang.org/docs/filters.html) as it refuses to compile with an "invalid indentation" error message
#? stdtmpl | standard
#proc greet(name = "world"): string =
# result = ""
<h1>Hello $name</h1>
#end proc
echo greet()
Since echo greet() is Nim code, you need to prefix it with #. Also, be aware that you may not have empty lines outside the proc, because Nim would then try to append them to a result variable, which does not exist outside the proc.
#? stdtmpl | standard
#proc greet(name = "world"): string =
# result = ""
<h1>Hello $name</h1>
#end proc
#echo greet()

awk/regex: parsing error logs not always returned error description

I recently asked for help to parse out Java error stacks from a group of log files and got a very nice solution at the link below (using awk).
Pull out Java error stacks from log files
I marked the question answered and after some debugging and studying I found a few potential issues and since they are unrelated to my initial question but rather due to my limited understanding of awk and regular expressions, I thought it might be better to ask a new question.
Here is the solution:
BEGIN{ OFS="," }
/[[:space:]]+*<Error / {
split("",n2v)
while ( match($0,/[^[:space:]]+="[^"]+/) ) {
name = value = substr($0,RSTART,RLENGTH)
sub(/=.*/,"",name)
sub(/^[^=]+="/,"",value)
$0 = substr($0,RSTART+RLENGTH)
n2v[name] = value
print name value
}
code = n2v["ErrorCode"]
desc[code] = n2v["ErrorDescription"]
count[code]++
if (!seen[code,FILENAME]++) {
fnames[code] = (code in fnames ? fnames[code] ", " : "") FILENAME
}
}
END {
print "Count", "ErrorCode", "ErrorDescription", "Files"
for (code in desc) {
print count[code], code, desc[code], fnames[code]
}
}
One issue I am having with it is that not all ErrorDescriptions are being captured. For example, this error description appears in the output of this script:
ErrorDescription="Database Error."
But this error description does not appear in the results (description copied from actual log file):
ErrorDescription="Operation not allowed for reason code "7" on table "SCHEMA.TABLE".. SQLCODE=-668, SQLSTATE=57016, DRIVER=4.13.127"
Nor does this one:
ErrorDescription="Cannot Find Person For Given Order."
It seems that most error descriptions are not being returned by this script but do exist in the log file. I don't see why some error descriptions would appear and some not. Does anyone have any ideas?
EDIT 1:
Here is a sample of the XML I am parsing:
<Errors>
<Error ErrorCode="ERR_0139"
ErrorDescription="Cannot Find Person For Given Order." ErrorMoreInfo="">
...
...
</Error>
</Errors>
The pattern in the script will not match your data:
/[[:space:]]+*<Error / {
Details:
The "+" tells it to match at least one space.
The space after "Error" tells it to match another space - but your data has no space before the "=".
The "<" is unnecessary (but not part of the problem).
This would be a better pattern:
/^[[:space:]]*ErrorDescription[[:space:]]*=[[:space:]]*".*"/
This regex would only match the error description.
ErrorDescription="(.+?)"
It uses a capturing group to remember your error description.
Demo here. (Tested against a combination of your edit and your previous question error log.)

How can I use EMPTY to display an error when fetched array is empty (beginner)

I am a beginner, and I think there is a detail which I am not seeing that stops my code from functioning. I have been trying to understand what is going on for hours. I would like that if my $data is empty, an error message be displayed. It doesn't happen. I am at a loss to understand what is wrong with this code.
try {$database = new PDO('mysql:host=localhost;dbname=miniblog', 'root', '');}
catch(Exception $e) {die('horror : '.$e->getmessage());}
$answer = $database->query('SELECT id, content FROM blog WHERE id= ' . $_GET['id'] . ' ' ) ;
// Instruct display conditions
while ($data = $answer->fetch())
{
if (empty($data))
{echo "ERROR there is no such article" ;}
else
{echo '<h3 class="news">' . ($data['content']) . '</h3>';}
}
Problem can be solved using if statement. The while statement failed to produce a error in case it was false, because while statements won't even run if it's false.
Using empty is also unecessary if you use if:
favorite
I am a beginner, and I think there is a detail which I am not seeing that stops my code from functioning. I have been trying to understand what is going on for hours. I would like that if my $data is empty, an error message be displayed. It doesn't happen. I am at a loss to understand what is wrong with this code.
try {$database = new PDO('mysql:host=localhost;dbname=miniblog', 'root', '');}
catch(Exception $e) {die('horror : '.$e->getmessage());}
$answer = $database->query('SELECT id, content FROM blog WHERE id= ' . $_GET['id'] . ' ' ) ;
// Instruct display conditions
if ($data = $answer->fetch())
{echo ' Article ' . $_GET['id'] . '' . ($data['content']) . '';}
else
{echo "ERROR there is no such article" ;}
// close loop
$answer->CloseCursor();
Problem Solved :D