How to check if variable is not exist - nginx

I can't find in docs how I can do negative statement like one below:
if !($some_var) {
... enter here if $some_var doesn't exist or empty
}
I know that I can check that the variable is exits using the if statement:
if ($some_var) {
...
}
But I can't find how to do if not statement
Is it possible for NGINX?

Try this:
if($var = false) {
....
}

Related

Passing variable between blocks in Openresty/Lua with ngx.ctx

Trying to pass a variable between two lua blocks. Supposedly, this should work with ngx.ctx, like this:
header_filter_by_lua_block {
ngx.ctx.myvar = ngx.header["X-fetch"];
}
access_by_lua_block {
ngx.header["X-send"] = ngx.ctx.myvar;
}
but it doesn't. What's wrong?
P.S. Testing with both in one block does work (basically duplicating the existing header, but this is just for illustration)
header_filter_by_lua_block {
ngx.ctx.myvar = ngx.header["X-fetch"];
ngx.header["X-send"] = ngx.ctx.myvar;
}
That's because access_by_lua_block runs before header_filter_by_lua_block.
Take a look at https://openresty-reference.readthedocs.io/en/latest/Directives/

How can I get the current region id inside the 'hook_preprocess_block'?

How can I get the current region ID inside hook_preprocess_block()?
I tried the following code, but it seems something is wrong.
function ThemeName_preprocess_block(&$variables) {
if ($variables['elements']['#region'] == "nav") {
$variables['attributes']['class'][] = 'SomeStyle';
}
}
$region = $variables['elements']['#configuration']['region'];

How to make a test fail with the testthat package?

I have a test for which if the prerequisites are not met (e.g., missing file or something) I would like to make it fail.
Just for clarification, here's an example I'd like to do:
test_that("...", {
if ( ... precondition to execute the test is not met... ) {
expect_true(FALSE) # Make it fail without going further
}
expect_that( ... real test here ...)
})
Now my question is: Is there any fail()-like expectation in the testthat package or I have to write expect_true(FALSE) all the time?
There isn't a fail function in testthat at the moment. I think you want something like
fail <- function(message = "Failure has been forced.", info = NULL, label = NULL)
{
expect_that(
NULL,
function(message)
{
expectation(FALSE, message)
},
info,
label
)
}
Usage is, for example,
test_that("!!!", fail())
Failure is not an option...
Try using stop:
test_that("testingsomething", {
if(file.exists("foo.txt")){
stop("foo.txt already exists")
}
foo = writeFreshVersion("foo.txt")
expect_true(file.exists("foo.txt"))
}
)

Using jsonPath looking for a string

I'm trying to use jsonPath and the pick function to determine if a rule needs to run or not based on the current domain. A simplified version of what I'm doing is here:
global
{
dataset shopscotchMerchants <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
}
rule checkdataset is active
{
select when pageview ".*" setting ()
pre
{
merchantData = shopscotchMerchants.pick("$.merchants[?(#.merchant=='Telefora')]");
}
emit
<|
console.log(merchantData);
|>
}
The console output I expect is the telefora object, instead I get all three objects from the json file.
If instead of merchant=='Telefora' I use merchantID==16 then it works great. I thought jsonPath could do matches to strings as well. Although the example above isn't searching against the merchantDomain part of the json, I'm experiencing the same problem with that.
Your problem comes from the fact that, as stated in the documentation, the string equality operators are eq, neq, and like. == is only for numbers. In your case, you want to test if one string is equal to another string, which is the job of the eq string equality operator.
Simply swap == for eq in you JSONpath filter expression and you will be good to go:
global
{
dataset shopscotchMerchants <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
}
rule checkdataset is active
{
select when pageview ".*" setting ()
pre
{
merchantData = shopscotchMerchants.pick("$.merchants[?(#.merchant eq 'Telefora')]"); // replace == with eq
}
emit
<|
console.log(merchantData);
|>
}
I put this to the test in my own test ruleset, the source for which is below:
ruleset a369x175 {
meta {
name "test-json-filtering"
description <<
>>
author "AKO"
logging on
}
dispatch {
domain "exampley.com"
}
global {
dataset merchant_dataset <- "https://s3.amazonaws.com/app-files/dev/merchantJson.json" cachable for 2 seconds
}
rule filter_some_delicous_json {
select when pageview "exampley.com"
pre {
merchant_data = merchant_dataset.pick("$.merchants[?(#.merchant eq 'Telefora')]");
}
{
emit <|
try { console.log(merchant_data); } catch(e) { }
|>;
}
}
}

assigning value in razor difficulty

I am having the following pice of code which is firing error
Error 1 Invalid expression term '='
#{
int Interest;
}
<td>#if (#item.interest.HasValue)
{
#Interest= #item.interest.Value.ToString("F2");
}
When declaring a variable, this variable needs to be assigned:
#{
string Interest = "";
}
and then:
#if (item.interest.HasValue)
{
Interest = item.interest.Value.ToString("F2");
}
This being said doing something like this in a view is a very bad design. I mean things like declaring and assigning variables based on some condition is not a logic that should be placed in a view. The view is there to display data. This logic should go to your controller or view model.
Inside your #if block you can address variables without the # sign.
#if (#item.interest.value) {
#item= #item.interest.Value
}
Is interpreted as:
#if (#item.interest.value) {
Write(item=);
Write(#item.interest.Value);
}
As you can see Write(item=) is not valid C# code.
You should use:
#if (item.interest.value) {
item = item.interest....
}
The reason your if (#item....) statement compiles, with the # sign. Is because you can prefix an identifier with the # to use reserved words as identifier names.
Try this:
#{
string Interest;
}
<td>#if (#item.interest.HasValue)
{
Interest= #item.interest.Value.ToString("F2");
}
By the way you are trying to assign a string (the result of ToString()) to an integer. This will not work.

Resources