Dataweave2.0 in mule 4 - mule

notes=(payload.examples if payload.examples != null else “ “) ++ (\n\n**column1:\ntest: “ ++ vars.name) ++ ( “\n column2: “ ++ vars.date) ++ (“\n column3: “ ++ attributes.headers.speech)
This is DataWeave 2.0 but error said invalid input. Anyone knows how to fix this in mule 4?

As per your comment code of DataWeave 1.0. Please find the below corresponding DataWeave 2.o code.
%dw 2.0
output application/json
---
{
(payload mapObject (value, key) -> {
((key): value) if (key as String != "notes")
}),
notes:
if (payload.examples != null)
payload.examples
else
"" ++ ("\n\n**column1:\ntest: " ++ vars.name) ++ ("\n column2: " ++ vars.date) ++ ("\n column3: " ++ attributes.headers.speech)
}

the part where you are performing the condition check with if, the '=!' should be '!='.Please let me know if this has helped you.

Related

How do I read all of standard input in Idris2?

I'm trying to figure out how to do something very simple: read all of standard input into a string (or a list of strings would be fine too).
Prelude has getLine : HasIO io => io String, which can give me one line, but it doesn't give me a way to know that I've read it all. If there is no more input, it just gives me an empty string, which means if the input contains empty lines, I can't tell that apart from the end of the input.
Consider the following program:
module Example
main : IO ()
module Solve
main : IO ()
main = do
putStrLn ("'" ++ !getLine ++ "'")
putStrLn ("'" ++ !getLine ++ "'")
putStrLn ("'" ++ !getLine ++ "'")
putStrLn ("'" ++ !getLine ++ "'")
putStrLn ("'" ++ !getLine ++ "'")
putStrLn ("'" ++ !getLine ++ "'")
This program will print six lines, each wrapped in single quotes, taking contents from standard input. If I compile it to ./example and run with three lines of input, and a blank line in the middle, here's the output:
$ ./example <<EOF
foo
bar
EOF
'foo'
''
'bar'
''
''
''
Note that it keeps printing out lines after the standard input stream is exhausted. This means if I put this in some recursive function to get me all of the input, I don't have a reasonable stop condition.
What is the idiomatic way to read all of standard input in Idris2, without knowing at build-time how many lines of input there will be?
You can use fEOF.
You can see an example here.

What is the correct way to scan "Quoted String" in ragel?

I m trying learn ragel with go, but i am not able to find a proper way to scan a Quoted-string
This is what i have defined
dquote = '"';
quoted_string = dquote (any*?) dquote ;
main := |*
quoted_string =>
{
current_token = QUOTED_STRING;
yylval.stringValue = string(lex.m_unScannedData[lex.m_ts:lex.m_te]);
fmt.Println("quoted string : ", yylval.stringValue)
fbreak;
};
The following expression with single quoted string works fine
if abc == "xyz.123" {
pp
}
If i scan the above condition then i get this printf
quoted string : "xyz.123"
But if i have 2 quoted string as shown below, it fails
if abc == "0003" {
if xyz == "5003" {
pp
}
}
it scans both the quoted string
quoted string : "0003" {
if xyz == "5003"
Can someone please help me with this ? If there is a better alternative
I am using below version
# ragel -v
Ragel State Machine Compiler version 6.10 March 2017
Copyright (c) 2001-2009 by Adrian Thurston
This did the trick
quoted_string = dquote (any - newline)* dquote ;

Grammar for string interpolation where malformed interpolations are treated as normal strings

Here is a subset of the language I want to parse:
A program consists of statements
A statement is an assignment: A = "b"
Assignment's left side is an identifier (all caps)
Assignment's right side is a string enclosed by quotation marks
A string supports string interpolation by inserting a bracket-enclosed identifier (A = "b[C]d")
So far this is straight forward enough. Here is what works:
Lexer:
lexer grammar string_testLexer;
STRING_START: '"' -> pushMode(STRING);
WS: [ \t\r\n]+ -> skip ;
ID: [A-Z]+;
EQ: '=';
mode STRING;
VAR_START: '[' -> pushMode(INTERPOLATION);
DOUBLE_QUOTE_INSIDE: '"' -> popMode;
REGULAR_STRING_INSIDE: ~('"'|'[')+;
mode INTERPOLATION;
ID_INSIDE: [A-Z]+;
CLOSE_BRACKET_INSIDE: ']' -> popMode;
Parser:
parser grammar string_testParser;
options { tokenVocab=string_testLexer; }
mainz: stat *;
stat: ID EQ string;
string: STRING_START string_part* DOUBLE_QUOTE_INSIDE;
string_part: interpolated_var | REGULAR_STRING_INSIDE;
interpolated_var: VAR_START ID_INSIDE CLOSE_BRACKET_INSIDE;
So far so good. However there is one more language feature:
if there is no valid identifier (that is all caps) in the brackets, treat as normal string.
Eg:
A = "hello" => "hello"
B = "h[A]a" => "h", A, "a"
C="h [A] a" => "h ", A, " a"
D="h [A][V] a" => "h ", A, V, " a"
E = "h [A] [V] a" => "h ", A, " ", V, " a"
F = "h [aVd] a" => "h [aVd] a"
G = "h [Va][VC] a" => "h [Va]", VC, " a"
H = "h [V][][ff[Z]" => "h ", V, "[][ff", Z
I tried to replace REGULAR_STRING_INSIDE: ~('"'|'[')+; With just REGULAR_STRING_INSIDE: ~('"')+;, but that does not work in ANTLR. It results in matching all the lines above as strings.
Since in ANTLR4 there is no backtracking to enable I'm not sure how to overcome this and tell ANTLR that if it did not match the interpolated_var rule it should go ahead and match REGULAR_STRING_INSIDE instead, it seems to always chose the latter.
I read that lexer always matches the longest token, so I tried to lift REGULAR_STRING_INSIDE and VAR_START as a parser rules, hoping that alternatives order in the parser will be honoured:
r: REGULAR_STRING_INSIDE
v: VAR_START
string: STRING_START string_part* DOUBLE_QUOTE_INSIDE;
string_part: v ID_INSIDE CLOSE_BRACKET_INSIDE | r;
That did not seem to make any difference at all.
I also read that antlr4 semantic predicates could help. But I have troubles coming up with the ones that needs to be applied in this case.
How do I modify this grammar above so that it can match both interpolated bits, or treat them as strings if they are malformed?
Test input:
A = "hello"
B = "h[A]a"
C="h [A] a"
D="h [A][V] a"
E = "h [A] [V] a"
F = "h [aVd] a"
G = "h [Va][VC] a"
H = "h [V][][ff[Z]"
How I compile / test:
antlr4 string_testLexer.g4
antlr4 string_testParser.g4
javac *.java
grun string_test mainz st.txt -tree
I tried to replace REGULAR_STRING_INSIDE: ~('"'|'[')+; With just REGULAR_STRING_INSIDE: ~('"')+;, but that does not work in ANTLR. It results in matching all the lines above as strings.
Correct, ANTLR tries to match as much as possible. So ~('"')+ will be far too greedy.
I also read that antlr4 semantic predicates could help.
Only use predicates as a last resort. It introduces target specific code in your grammar. If it's not needed (which in this case it isn't), then don't use them.
Try something like this:
REGULAR_STRING_INSIDE
: ( ~( '"' | '[' )+
| '[' [A-Z]* ~( ']' | [A-Z] )
| '[]'
)+
;
The rule above would read as:
match any char other than " or [ once or more
OR match a [ followed by zero or more capitals, followed by any char other than ] or a capital (your [Va and [aVd cases)
OR match an empty block, []
And match one of these 3 alternatives above once or more to create a single REGULAR_STRING_INSIDE.
And if a string can end with one or mote [, you may also want to do this:
DOUBLE_QUOTE_INSIDE
: '['* '"' -> popMode
;

How to use ascii characters in MuleESB Dataweave strings, £ © ¥ etc

Seems like the editor is unicode aware..but the validation throws a problem
%dw 1.0
%output application/java
---
funds map {
prize_us: "\$" ++ $ as :string,
prize_uk: "£" ++ ($ * 0.81) as :string
}
Error in DW script: Invalid input """, expected typeOf, using, unaryOp, not or value
note the """ instead of the £ symbol.
Removing the £ the problem goes away and the project can compile.
Try adding the encoding also.
%dw 1.0
%input in0 application/json
%output application/java encoding="UNICODE"

awk function does not allow to return

i have functions in my awk script to parse lines contains certain words/characters
function tohyphen (o) {
split ($0,a,"to[-_]")
split (a[2],b,"-")
k=b[1]
p=b[2]
return k
}
function tospace (o) {
split ($0,a,"to ")
split (a[2],b,"-")
k=b[1]
p=b[2]
return k
}
funtion pipe (o) {
split ($0,a,"|")
split (a[2],b,"-")
x=b[1]
y=b[2]
return x
#return x
}
{
#if (match ($0, /to[-_]/))
if ($0 ~ /to[-_]/)
print "found to- for interface ", $1, " is ", tohyphen($0), "is ",p, " is ", $1="",$0
else if ($0 ~ /to /)
#(match ($0, /to /))
print "found to for interface", $1, " is ", tospace($0), " is ", p, " is ", $1="",$0
else if ($0 ~ /\|/)
# pipe($0)
print "found to for interface", $1, " is ",topipe($0), " is ", y, " is ", $1="",$0
else
print $1, $1="",$0
}
in the third function which just searches for a match to pipe it does not allow me return anything, giving me the error return is outside of function.
any idea what could be the problem or any other way for me to run this.
Try spelling function correctly! You have funtion not function. Are you using an editor with syntax highlighting for awk? You can see from stackoverflows markup that funtion is not a reserved word.