I have a requirement to evaluate certain expressions, and based on the evaluation choose the next steps.
I am trying to use JInt javscript interpreter for this.
For basic evaluations from json, it is working fine. But when I am trying to add additional operations on array, that's where the issue is coming.
My approach,
set value for base objects => write a function to evaluate expressions => invoke this function and pass the expression to evaluate => function will use eval to evaluate passed string. Initial objects are already set in previous steps.
Something like this,
inputStart.Reference.CompanyCode == \"ABC\"
In this, inputStart is the base object which I have already set using
engine.SetValue("inputStart", JsonConvert.DeserializeObject(inputStart));
inputStart is json text. Fiddle has the string as well for reference.
And finally the function,
engine.Execute("function evaluateExpression(expression) { " +
"return eval(expression); " +
"}")
.GetValue("evaluateExpression");
var test = engine.Invoke("evaluateExpression", "inputStart.Reference.CompanyCode == \"ABC\"");
This code works correctly. Now the variation is, I have another json text which has array in it.
And I am trying to use filter or find to locate specific array item, and then have some operation on it.
Something like this,
inputQuestions.Questions.filter((x) => x.Code == \"INIT_IMP\")[0].Answer.Answer.Code
This gives me below error,
Unhandled exception. System.ArgumentException: Accessed JArray values
with invalid key value: "filter". Int32 array index expected.
My complete program for reference,
string inputStart = "{\n\t\"PurposeCode\": \"START\",\n\t\"Reference\": {\n\t\t\"CompanyCode\": \"ABC\"\t\t\n\t}\n}";
string inputQuestions = "{\n\t\"QuestionSetCode\": \"QUES_SET_1\",\n\t\"Version\": 1,\n\t\"Questions\": [{\n\t\t\"Code\": \"INIT_IMP\",\n\t\t\"Text\": \"Impact ?\",\n\t\t\"AnswerType\": 3,\n\t\t\"SequenceNumber\": 1,\n\t\t\"Answer\": {\n\t\t\t\"Answers\": [{\n\t\t\t\t\"Code\": \"INIT_IMP_DO\",\n\t\t\t\t\"Text\": \"A\",\n\t\t\t\t\"SequenceNumber\": 1\n\t\t\t}, {\n\t\t\t\t\"Code\": \"INIT_IMP_DA\",\n\t\t\t\t\"Text\": \"B\",\n\t\t\t\t\"SequenceNumber\": 2\n\t\t\t}, {\n\t\t\t\t\"Code\": \"INIT_IMP_AO\",\n\t\t\t\t\"Text\": \"C\",\n\t\t\t\t\"SequenceNumber\": 3\n\t\t\t}],\n\t\t\t\"Answer\": {\n\t\t\t\t\"Code\": \"INIT_IMP_DO\",\n\t\t\t\t\"Text\": \"A\",\n\t\t\t\t\"SequenceNumber\": 1\n\t\t\t},\n\t\t\t\"Type\": \"ListOfValuesAnswer\"\n\t\t},\n\t\t\"Applicable\": true\n\t}, {\n\t\t\"Code\": \"ENT_ACC_LIQ\",\n\t\t\"Text\": \"Exposure ?\",\n\t\t\"AnswerType\": 3,\n\t\t\"SequenceNumber\": 2,\n\t\t\"Answer\": {\n\t\t\t\"Answers\": [{\n\t\t\t\t\"Code\": \"ENT_ACC_LIQ_Y\",\n\t\t\t\t\"Text\": \"Yes\",\n\t\t\t\t\"SequenceNumber\": 1\n\t\t\t}, {\n\t\t\t\t\"Code\": \"ENT_ACC_LIQ_N\",\n\t\t\t\t\"Text\": \"No\",\n\t\t\t\t\"SequenceNumber\": 2\n\t\t\t}],\n\t\t\t\"Answer\": {\n\t\t\t\t\"Code\": \"ENT_ACC_LIQ_N\",\n\t\t\t\t\"Text\": \"No\",\n\t\t\t\t\"SequenceNumber\": 2\n\t\t\t},\n\t\t\t\"Type\": \"ListOfValuesAnswer\"\n\t\t},\n\t\t\"Applicable\": true\n\t}],\n\t\"ClientId\": null\n}";
Engine engine = new Engine();
engine.SetValue("inputStart", JsonConvert.DeserializeObject(inputStart));
engine.SetValue("inputQuestions", JsonConvert.DeserializeObject(inputQuestions));
engine.Execute("function evaluateExpression(expression) { " +
"return eval(expression); " +
"}")
.GetValue("evaluateExpression");
var test = engine.Invoke("evaluateExpression", "inputQuestions.Questions.filter((x) => x.Code == \"INIT_IMP\")[0].Answer.Answer.Code == \"INIT_IMP_DO\" && inputStart.Reference.CompanyCode == \"ABC\"");
Console.WriteLine(test);
Here is the complete fiddle for what I am trying to do, https://dotnetfiddle.net/GtamM8
If anyone has come across such issue, or can suggest a solution to this, it will be very helpful.
Because you are using the new language constructs, you should be using 3.x preview release from NuGet. 3.x series contains support for lambda expressions and array filtering against them.
Related
I've been asked to optimize the speed of my query. I currently have this regex in my query, which is checking for a pattern and returning substring within that pattern. To clarify I have a table with multiple columns that I have to look through to check for this value: [v= and return the numbers within that list.
This is looking through several 'name..' columns that look something like this: xyzzy [v=123] but I only want to return 123, the below works:
COALESCE(REGEXP_SUBSTR(NAME, '[[]v=([0-9]+)', 1, 1, 'ie'),
REGEXP_SUBSTR(NAME_5, '[[]v=([0-9]+)', 1, 1, 'ie'),
REGEXP_SUBSTR(NAME_4, '[[]v=([0-9]+)', 1, 1, 'ie')) as display_vertical_code
but to optimize this, I thought of maybe creating a function unfortunately I don't know javascript :/ and I don't know if the formatting is correct I'm having some difficulties creating it, this is what I've tried, can someone tell me if I'm missing something?
CREATE OR REPLACE FUNCTION dfp.regex(NAME VARCHAR)
RETURNS OBJECT
LANGUAGE javascript
STRICT AS '
return new RegExp(NAME,"[[]v=([0-9]+)",1 ,1,"ie")
';
When I try to use the above function in my below query:
COALESCE(
GET(DFP.REGEX(NAME)),
GET(DFP.REGEX(NAME_5)),
GET(DFP.REGEX(NAME_4)),
GET(DFP.REGEX(NAME_3)),
GET(DFP.REGEX(NAME_2)),
GET(DFP.REGEX(NAME_1)),
GET(DFP.REGEX(NAME_0))
) as display_vertical_code
I see this error:
error line 3 at position 8 not enough arguments for function
[GET(REGEX(Tablename.NAME))], expected 2, got 1
This should do it.
CREATE OR REPLACE FUNCTION regex(NAME VARCHAR)
RETURNS string
LANGUAGE javascript
STRICT IMMUTABLE AS
$$
const regex = /[[]\s{0,5}v\s{0,5}=\s{0,5}([0-9]+)/i;
let s = NAME.match(regex);
if (s != null) {
return s[0].split('=')[1].trim();
} else {
return null;
}
$$;
select regex('xyzzy [v=123]');
-- Alternate permutation
select regex('xyzzy [ v = 123]');
You want to return a string, not an object. Adding the IMMUTABLE option tells Snowflake that the same input results in the same output every time. It can sometimes help with performance.
Edit... This one's a bit more fault tolerant and allows whitespace (if that could be a problem). If you want to get rid of allowing whitespace, delete the \s{0,5} expressions.
Currently I am using the following to evaluate variables that are placed in strings at runtime:
newVal = eval("`" + newVal + "`");
So if I have the string:
"Hello from channel: ${erpVars["CommandChannel"]["name"]}"
And erpVars["CommandChannel"]["name"] has value home, then the resulting string is:
Hello from channel: home
There are other objects than just erpVars that could be holding matching values for the string, but this is just one example. It's also important to note that each string could have more than one variable that needs replacing.
I am trying to achieve the same thing without using eval(), as some of the variable values come from user input.
Your case sounds super nasty (you should never ever use eval in JS! It poses a major security threat! also it looks weird that you want to replace this sort of a string) and perhaps if you told me more about where you get your inputs from and in what form, then maybe we could find together a much better solution for this. On that note, this is how I would solve your issue in its current form.
const newVal = 'Hello from channel: ${erpVars["CommandChannel"]["name"]}';
const strings = {
erpVars: {
CommandChannel: {
name: "home"
}
}
};
const vars = newVal.match(/\$\{.+?\}/g);
let result = newVal;
vars.forEach(v => {
let valuePath = '${erpVars["CommandChannel"]["name"]}'.match(/[\w\d]+/g).join('.');
result = result.replace(v, _.get(strings, valuePath));
});
console.log(result);
Note that I'm skipping here the edge scenarios, like getting a null result from the newVal.match when there are no variables in the newVal, but that's easy to handle.
Also note that over here i'm using the lodash library in _.get() (https://lodash.com/docs/4.17.15#get). It's super popular for this kind of small tasks. Of course there are really a lot of other tools that allow you to extract a value based on a property path like erpVars.CommandChannel.name that is stored in the valuePath variable, including a crazy amount of instructions that tell you how to do it yourself.
I am new on this website, and I am also new at Javascript.
What I would do is get a value from a link, eg:
http://bricks.couponmicrosite.net/JavaBricksWeb/LandingPage.aspx?O=107905&C=MF&CPT=qwbc74g7HdLtKVVQ1oNe&P=test&tqnm=td3ffdn764156741
I need to take this value: O=107905, if it is only one code I need to run a file (file A),
if it look like this: O=107905~107906, then I need to run a different file (file B).
What I thought is create a function with javascript where if it is (~) is missing, then will be used the file A, if there is one or more than one of (~) then the file B will start to work.
Many thanks in advance!
Well. We really do encourage you to provide your own code first before providing solutions, but this is a fairly simple problem in javascript. Here's my take on it.
The first problem you have to solve is actually getting the query string parameters in a meaningful format. Here I create an object that uses the query string keys as the object keys (IE 9+ only because of the forEach)
var tmpObject = {}
location.search.substr(1).split('&').forEach(function(item){
var o = item.split('=');
tmpObject[o.shift()] = o.shift();
});
Then it's just a matter of making sure the query string contained the target object (in this case "O") and determine if there is more than one.
if(tmpObject.hasOwnProperty('O') && tmpObject.O.split('~').length > 1) {
console.log('return multiple files');
} else {
console.log('return one file');
}
Try this
var url = "http://bricks.couponmicrosite.net/JavaBricksWeb/LandingPage.aspx?O=107905&C=MF&CPT=qwbc74g7HdLtKVVQ1oNe&P=test&tqnm=td3ffdn764156741";
var search = url.substring(url.indexOf("?")+1);
var map={};
search.forEach(function(val){var items=val.split("="); map[items[0]]=items[1];});
if (map["O"].indexOf("~") != -1)
{
//O value has ~ in it
}
else
{
//O has no ~ in it
}
Possible solution to get the paramter would be there : How to get the value from the GET parameters?
Once you have the parameter value, you can for sure look if you find '~' with String.prototype.indexOf.
String.prototype.indexOf returns the position of the string in the other string. If not found, it will return -1.
I have seen commas to concatenate primitive data types in Javascript and was wondering whether there was any difference in using a comma over say the + operator as well as the .concat() function?
So an example the following statement gives me abc
var value1 = a, value2 = b, value3 = c;
document.write(value1,value2,value3);
Apples and oranges. Nothing is being concatenated in your example; you are simply specifying 3 arguments to the write() function.
document.write(exp1, exp2, exp3, ...) accepts multiple parameters, and when given multiple parameters it will iterate through them all as if you called write() on each one individually.
However, the comma does have a use when evaluating expressions where it is used to process multiple expressions and returns the last one. To see that in action you need to wrap your parameters in a set of parenthesis so that it forms a single parameter:
document.write("a","b","c") // abc
document.write( ("a", "b", "c") ) // c
alert("a","b","c") // a
alert( ("a","b","c") ) // c
alert( (x=2, ++x) ) // 3
Since string concatenation is one of the haviest operations on computing, using document.write with various parameters would perform better.
See this test (it sometimes hangs in IE, so use other browser please) http://jsperf.com/document-write-vs-concatenation
Explaination:
document.write("val1", "val2", "val3");
is equivalent to
document.write("val1");
document.write("val2");
document.write("val3");
Thus, being much faster, since it doesn't concatenates the strings.
In most browsers (IE <= 8, Gecko) string concatenation using the + operator has horrible performance.
In my company, for example, we have to compose a large HTML fragment piece by piece. In this case we have something like this:
var id = 123;
var html = [];
html.push('<div id="', id, '">hello</div>');
var result = html.join('');
In the case where you have LOTS of concatenations, this is MUCH better than the following:
var id = 123;
result = '<div id="' + id + '">hello</div>';
So to answer your question - it really depends on the situation. If you are concatenating many strings together you should never use the + operator due to poor performance. But it should be fine 99% of the time where the performance gain will be so little it is just painful to try anything else. But in cases where you actually have the option to comma separate (like in the case of array.push, or document.write) then you should definately take advantage of it.
I'm building a node server that needs to execute code that might be unsafe. In order to achieve this I'm using a Sandbox API that blocks attacks and returns the result and output from a script. It uses a modified global object to keep access hidden from the Node global object (and the use of require... etc).
My specific need right now is to take an object that is defined by a user (this is all trusted, nothing from random users on the internet so security isn't the biggest concern at the moment, right now it's to get it working) and create a dynamic bit of code that will "transfer" the object along with their code to a child Node process for safe execution (the security here is so that any errors don't crash the main process).
My current goal is to take an object, like the following:
obj = {
defaultName: "Unnamed",
hello: function(name) {
if (typeof name === "undefined" || name === null)
name = this.defaultName;
echo("Hello, " + name + "!");
}
}
(This is very simplistic, it's for testing)
I'm using FJSON to serialize the functions for transfer as well. My attempt at serializing this for transfer with the code is as follows:
// "code" is the users code
// "obj" is the object above
// "Extend" is a function defined by the Child process
var str = FJSON.funkify(obj);
code = "var temp = FJSON.unfunkify(\"" + str + "\"); Extend(this, temp); temp = undefined; " + code;
After doing this, and attempting to write it to the child I get weird (and cryptic errors) like: "Unexpected token {" or (rarely and more cryptic) "Unexpected token ILLEGAL '" (which, this is confusing because I've verified that nowhere in the code am I inserting a ' and there are none in the test code).
The funkified string is {"defaultName": "Unnamed","hello":{"FUNCTION":true,"params":["name"],"body":"\n\r if (typeof name === \"undefined\" || name === null)\n\r name = this.defaultName;\n\r echo(\"Hello, \" + name + \"!\");\n\r "}}
And finally, for the sake of testing, I've tried serializing a simple object (without functions using JSON, and with functions using FJSON) and then attempting to run eval on the string in the Node REPL but I keep getting ... when I try eval(JSON.stringify(objWithoutFunctions)); and the same with the FJSON.
I've struggled with this problem for several hours now and can't think of any other things to try/check. Any suggestions are appreciated.
UPDATE
I still have been unable to determine the most efficient way to do this, as stringifying the object and transferring it along with code was not working and I was unable to get it to work nicely I've reverted to converting the object into code, essentially looping through the properties and assigning the variables manually. To provide example:
The object:
obj = {
prop: "ItsValue",
otherProp: true
};
Would become:
this.prop = "ItsValue"; this.otherProp = true;
I found a workaround as listed in the Update, I just converted the object into code. It could have been issues with the FJSON library which I've fixed since then. This is no longer an issue but I still welcome any answers that may be able to address the original problem.