I'm trying to substitute two variables in a string using Fn::Sub. The problem is the variable value is based on conditional statement. How can this be written correctly?
"Fn::Sub":["https://${QS}.${CUR}.website.com,
{"QS": {"Fn::If": ["condition1", ${abc}-${AWS::Region}, ${abcq}]}},
{"CUR": {"Fn::If": ["condition1", ${AWS::Region}, ${abcq}]}}
]
Here, condition1 is one of the Conditions, and abc and abcq are Parameters.
You can try the following. It may need some adjustment as I'm not able to run the actual code to verify it.
I think the following should be the way to do what you want to achieve:
"Fn::Sub":
[
"https://${QS}.${CUR}.website.com,
{"QS":
{
"Fn::If": [
"condition1",
{"Fn::Sub": "${abc}-${AWS::Region}"},
{"Fn::Ref": "abcq"}
]
}
},
{"CUR":
{
"Fn::If": [
"condition1",
{"Fn::Ref": "AWS::Region"},
{"Fn::Ref": "abcq"}
]
}
}
]
Related
I'm playing with vs code extension development, and I'm trying to create a viewsWelcome element.
content accepts a string with markdown annotations, the following works as expected
"viewsWelcome": [
{
"view": "view-id",
"contents": "[Clone Repository](command:git.clone)"
}
]
However, when trying to replace command:git.clone with command:vscode.open which accepts an uri as parameter, the parameter is not working.
"viewsWelcome": [
{
"view": "view-id",
"contents": "[Open URL](command:vscode.open?[https://foobar.com])"
}
]
I haven't found documents about the syntax, except for some examples here, which is not working as well
Use the following code to generate the correct arguments string:
const args = [
vscode.Uri.parse("https://foobar.com")
];
const encodedArgs = encodeURIComponent(
JSON.stringify(args)
);
console.log(encodedArgs);
// %5B%7B%22%24mid%22%3A1%2C%22path%22%3A%22%2F%22%2C%22scheme%22%3A%22https%22%2C%22authority%22%3A%22foobar.com%22%7D%5D
Then add the encoded arguments to the command: URI:
"viewsWelcome": [
{
"view": "view-id",
"contents": "[Open URL](command:vscode.open?%5B%7B%22%24mid%22%3A1%2C%22path%22%3A%22%2F%22%2C%22scheme%22%3A%22https%22%2C%22authority%22%3A%22foobar.com%22%7D%5D)"
}
]
As you can see it is not sufficient to just pass the URL as a plain string like you did. It has to be the JSON representation of a vscode.Uri.
Additionally, you have to properly URL-encode the resulting JSON before adding it to the command: URI.
Btw, it was the "command:git.stage" example in the VS Code API docs that got me on the right track.
My problem is that I have a remote API giving me a JSON with 10 levels deep nesting and I need to add another array/hashtable/PSCustomObject (whatever you want to call it) to one of them.
The JSON looks like this:
{
"result": [
"modules": [
"data": {
"segments": [
{
"routes": {
"static": [
{
"destination": "10.0.0.0",
"netmask": "255.0.0.0"
},
{
"destination": "172.16.0.0",
"netmask": "255.240.0.0"
}
]
}
}
]
}
]
]
}
The return object is a Hashtable, however when I try to access the values in dot notation, it turns into PSCustomObject:
$statics = $result.result.modules.data.segments.routes.static
It won't let me use the other notation:
$statics = $result['result']['modules']['data']['segments']['routes']['static']
Error: Cannot index into a null array
The real tricky part (for me) is to to append a new hastable to the "static" hashtable in such a way that the rest of the hashtable remains intact.
$newroute = #{
destination = "1.1.1.1."
netmask = "255.255.255.255"
}
I would have used PHP or Python but for this project I must use Powershell and I'm running into all sorts of things where PS behaves different from what I expect.
Any help is greatly appreciated!
static is an array, you can add new items to it:
$newStaticRoute = [pscustomobject]#{
destination = '192.168.0.0'
netmask = '255.255.0.0'
}
$result.result.modules.data.segments.routes.static += $newStaticRoute
I hate to risk asking a duplicate question, but perhaps this is different from Passing Variables to a MongoDB View which didn't have any clear solution.
Below is a query to find the country for IP Address 16778237. (Outside the scope of this query, there is a formula that turns an IPV4 address into a number.)
I was wondering if we could abstract away this query out of NodeJS code, and make a view, so the view could be called from NodeJS. But the fields ipFrom and ipTo are indexed to get the query to run fast against millions of documents in the collection, so we can't return all the rows to NodeJS and filter there.
In MSSQL maybe this would have to be a stored procedure, instead of a view. Just trying to learn what is possible in MongoDB. I know there are functions, which are written in JavaScript. Is that where I need to look?
db['ip2Locations'].aggregate(
{
$match:
{
$and: [
{
"ipFrom": {
$lte: 16778237
}
},
{
"ipTo": {
$gte: 16778237
}
},
{
"active": true
}
],
$comment: "where 16778237 between startIPRange and stopIPRange and the row is 'Active',sort by createdDateTime, limit to the top 1 row, and return the country"
}
},
{
$sort:
{
'createdDateTime': - 1
}
},
{
$project:
{
'countryCode': 1
}
},
{
$limit: 1
}
)
Part 2 - after more research and experimenting, I found this is possible and runs with success, but then see trying to make a view below this query.
var ipaddr = 16778237
db['ip2Locations'].aggregate(
{
$match:
{
$and: [
{
"ipFrom": {
$lte: ipaddr
}
},
{
"ipTo": {
$gte: ipaddr
}
},
{
"active": true
}
],
$comment: "where 16778237 between startIPRange and stopIPRange and the row is 'Active',sort by createdDateTime, limit to the top 1 row, and return the country"
}
},
{
$sort:
{
'createdDateTime': - 1
}
},
{
$project:
{
'countryCode': 1
}
},
{
$limit: 1
}
)
If I try to create a view with a "var" in it, like this;
db.createView("ip2Locations_vw-lookupcountryfromip","ip2Locations",[
var ipaddr = 16778237
db['ip2Locations'].aggregate(
I get error:
[Error] SyntaxError: expected expression, got keyword 'var'
In the link I provided above, I think the guy was trying to figure how the $$user-variables work (no example here: https://docs.mongodb.com/manual/reference/aggregation-variables/). That page refers to $let, but never shows how the two work together. I found one example here: https://www.tutorialspoint.com/mongodb-query-to-set-user-defined-variable-into-query on variables, but not $$variables. I'm
db.createView("ip2Locations_vw-lookupcountryfromip","ip2Locations",[
db['ip2Locations'].aggregate(
...etc...
"ipFrom": {
$lte: $$ipaddr
}
I tried ipaddr, $ipaddr, and $$ipaddr, and they all give a variation of this error:
[Error] ReferenceError: $ipaddr is not defined
In a perfect world, one would be able to do something like:
get['ip2Locations_vw-lookupcountryfromip'].find({$let: {'ipaddr': 16778237})
or similar.
I'm getting that it's possible with Javascript stored in MongoDB (How to use variables in MongoDB query?), but I'll have to re-read that; seems like some blogs were warning against it.
I have yet to find a working example using $$user-variables, still looking.
Interpretation
You want to query a view from some server side code, passing a variable to it.
Context
Can we use an external variable to recompute a View? Take the following pipeline:
var pipeline = [{ $group:{ _id:null, useless:{ $push:"$$NOW" } } }]
We can pass system variables using $$. We can define user variables too, but the user defined variables are made out of:
Collection Data
System Variables.
Also, respect to your Part2:
A variable var variable="what" will be computed only once. Redefine variable="whatever" makes no difference in the view, it uses "what".
Conclusion
Views can only be re-computed with system variables, or user variables dependant on those system variables or collection data.
Added an answer to the post you link too.
how do i accomplish multiple value inside query 'contains' ?
eg.
{
name: {
contains: [
'west',
'lob'
]
},
address : {
contains: 'york'
}
}
Having checked up on it via the gitter channel you need to do...
If you wanna use contains then you need to use LIKE! if you want a straight match then use a normal query.
Model.query("SELECT whatever FROM MyTable WHERE someFielding LIKE '%THEVALUE%'", function(error, myRows) {
// do some stuff
});
http://sailsjs.org/documentation/reference/waterline-orm/models/query
I can't figure how add tables to codemirror. I have sql-hint.js included, and work for keywords but don't understand how add tables and columns...
Sadly, this does not appear to be documented anywhere.
With some trial and error I was able to figure out that you can pass in a structure of table and column names as options when invoking the hinter, like this:
CodeMirror.commands.autocomplete = function(cm) {
CodeMirror.showHint(cm, CodeMirror.hint.sql, {
tables: {
"table1": [ "col_A", "col_B", "col_C" ],
"table2": [ "other_columns1", "other_columns2" ]
}
} );
}
I know that this question is somewhat old but..I found an interesting way, valid in 4.3 release (I don't know anything about older releases): Just add the "CodeMirror.hint.sql" value (without quotes, as its a function) as "hint" option and add the "tables" object as a sub-object defined in "hintOptions" object.
Something like:
CodeMirror.fromTextArea(document.getElementsByTagName("textarea")[0], {
mode: "text/x-sql",
extraKeys: {"Ctrl-Space": "autocomplete"}, // To invoke the auto complete
hint: CodeMirror.hint.sql,
hintOptions: {
tables: {
"table1": [ "col_A", "col_B", "col_C" ],
"table2": [ "other_columns1", "other_columns2" ]
}
}
});
That's it. Note that the "extraKeys" is absolutely not needed, but I found it to be great to test the autocomplete more easily. =)
Good luck. :)