Why are local variables variable is not respected? - scope

In this code snippet, fields-types in the end is modified by the to-camel-case function, vs. being passed as a local variable to the parent function:
fields-types: ["First Name" "string" "Last Name" "string" "Age" "int"]
to-camel-case: function [name] [
name/1: lowercase name/1
replace/all name space ""
]
fill-template-body: func [
field-labels-types [block!] /local vars fields-names-types
] [
vars: [member-name member-type]
field-names-types: copy []
foreach [field-label field-type] field-labels-types [
append field-names-types to-camel-case field-label
append field-names-types field-type
]
]
fill-template-body fields-types
Execution gives:
>> fill-template-body fields-types
== ["firstName" "string" "lastName" "string" "age" "int"]
>> fields-types
== ["firstName" "string" "lastName" "string" "age" "int"]
>>
Whereas I would want that fields-types to stay invariant:
fields-types: ["First Name" "string" "Last Name" "string" "Age" "int"]
Of course I can try to circumvent this by modifying to-camel-case to use a copy of name, but that is not something I think I should have to do.
Is there something like the var and val keywords in Scala?

Variable is an ugly word in REBOL, everything - even words - are values. This isn't some semantic newspeak, it's helpful in understanding the way in which REBOL flows.
I think of values as being contained within one giant array in memory, where REBOL (the language) uses words and their contexts to reference and interact with the values. Most REBOL functions operate without duplicating these values:
head lowercase next uppercase str: "abcd"
remove back tail str
This is one of REBOL's most efficient features - you don't need copies for intermediary processes, to require such is wasteful. Think of that array growing where every time you use replace, uppercase or to-camel-case a value is duplicated. Whole processes can be built on the premise of modification rather than duplication - indeed a context can be built without necessarily having to return a value:
remove-spaces: use [space mark][
space: charset " ^-"
[any [mark: space (remove mark) | skip]]
]
parse/all str: "Should Be No Spaces" remove-spaces
The trick then becomes knowing where to copy values, and I think happens to intersect with REBOL's gift for concise expression:
parse/all link: copy title: "An Article on REBOL" remove-spaces
print ["(" link ")" title]
to-camel-case copy field-label
And of course, modification has its limitations. Sometimes it is cleaner building from scratch.

Your camel case function operates on the original value so if you want to preserve the original value, you need to copy it, and return the altered value. Since your function acts on a template it needs to copy the template right??
So, something like this should work:
fill-template-body: func[ labels [block!] /local field-labels-types vars fields-names-types][
field-labels-types: copy labels
..

Related

Remove a map item in terraform

If I use the following yamldecode, I get the following output:
output "product" {
value = distinct(yamldecode(file("resource/lab/abc.yaml"))["account_list"]["resource_tags"][*]["TAG:product"])
}
Output:
+ product = [
+ "fargate",
+ "CRM",
]
I want fargate to be removed from my output and the expected output is this:
+ product = [
+ "CRM"
]
Please let me know how I can do this.
output "product" {
value = compact([for x in distinct(yamldecode(file("resource/lab/abc.yaml"))["account_list"]["resource_tags"][*]["TAG:product"]) : x == "fargate" ? "" : x])
}
I get this output:
test = [
"enter",
]
compact function solved the problem.
The Terraform language is based on functional principles rather than imperative principles and so it does not support directly modifying an existing data structure. Instead, the best way to think about goals like this is how to define a new value which differs from your existing value in some particular way.
For collection and structural types, the most common way to derive a new value with different elements is to use a for expression, which describes a rule for creating a new collection based on the elements of an existing collection. For each element in the source collection we can decide whether to use it in the result at all, and then if we do decide to use it we can also describe how to calculate a new element value based on the input element value.
In your case you seem to want to create a new list with fewer elements than an existing list. (Your question title mentions maps, but the question text shows a tuple which is behaving as a list.)
To produce a new collection with fewer elements we use the optional if clause of a for expression. For example:
[for x in var.example : x if x != "fargate"]
In the above expression, x refers to each element of var.example in turn. Terraform first evaluates the if clause, and expects it to return true if the element should be used. If so, it will then evaluate the expression immediately after the colon, which in this case is just x and so the result element is identical to the input element.
Your example expression also includes some unrelated work to decode a YAML string and then traverse to the list inside it. That expression replaces var.example in the above expression, as follows:
output "products" {
value = toset([
for x in yamldecode(file("${path.module}/resource/lab/abc.yaml"))["account_list"]["resource_tags"][*]["TAG:product"]: x
if x != "fargate"
])
}
I made some other small changes to the above compared to your example:
I named the output value "products" instead of "product", because it's returning a collection and it's Terraform language idiom to use plural names for collection values.
I wrapped the expression in toset instead of distinct, because from your description it seems like this is an unordered collection of unique strings rather than an ordered sequence of strings.
I added path.module to the front of the file path so that this will look for the file in the current module directory. If your output value is currently in a root module then this doesn't really matter because the module directory will always be the current working directory, but it's good practice to include this so that it's clear that the file belongs to the module.
Therefore returning a set is more appropriate than returning a list because it communicates to the caller of the module that they may not rely on the order of these items, and therefore allows the order to potentially change in future without it being a breaking change to your module.

Get a value associated with a key in an array of JSON objects in a Logic App expression

Given this JSON hash:
{
"id": 55555,
"name": "111111",
"custom_data_field": [
{
"id": 1,
"label": "Vehicle Type",
"value": "Coach"
},
{
"id": 2,
"label": "Vendor",
"value": 1
}
]
}
I need the value associated with each label.
I'm able to get the value using the array's index:
#item()?['custom_data_field'][0]['value'] # Coach
#item()?['custom_data_field'][1]['value'] # 1
But this is a bit fragile.
This syntax doesn't work:
#item()?['custom_data_field'][#label=='Vehicle Type']['value'] # Coach
#item()?['custom_data_field'][#label=='Vendor']['value'] # 1
Is there a way to do this reliably?
According to the description of your question, it seems the data {"id": 55555, "name": "111111"....} you provided is an item of a array list because your expression begins with item() (I guess you use this expression in a "For each" or something else loop action). And custom_data_field is array under the item, you want to do filter/select operation to get the value which its label equals Vehicle Type just by the expression. I don't think we can do it just in one expression (because the label and value are not key-value map, we can not do the filter/select in expression easily).
To meet the requirement, we need to use a more sophisticated approach such as "Filter array" action mentioned by Scott in comments. We need to set the array custom_data_field to the input box("From" box) of the "Filter array" action.
And then add the filter condition.
After running the logic app, it will filter the items by the filter condition.
As the filter action don't know how many items match the condition, so the output will always be a array but not an item or a record even if there is only one item matches the condition(label equals "Vehicle Type") in your custom_data_field list.
So if you want to get the value, you need to get it by writing an expression as below screenshot.
Hope it helps~

How can I convert a list to a string in Terraform?

join works BUT i want to keep the double quotes join gives me this
[ben,linda,john]
BUT i want this
["ben", "linda", "john"]
this is getting crazy, spent over 2 hours trying to fix this
i want to pass in a list as a string variable
why can't terraform just take in my list as a string? why is this so difficult?
so i have
name = ["ben", "linda", "john"]
and i want to pass this to variable used in terrform
var.name
why can't terrform take this as is?
i get the error saying epxtected a string and i can not find a solution online after sarching everywhere
i have been able to get
[ ben,linda,john ] using join(",", var.name) but i want ["ben", "linda", "john"]
$ terraform --version
Terraform v0.12.18
+ provider.aws v2.42.0
+ provider.template v2.1.2
Conversion from list to string always requires an explicit decision about how the result will be formatted: which character (if any) will delimit the individual items, which delimiters (if any) will mark each item, which markers will be included at the start and end (if any) to explicitly mark the result as a list.
The syntax example you showed looks like JSON. If that is your goal then the easiest answer is to use jsonencode to convert the list directly to JSON syntax:
jsonencode(var.names)
This function produces compact JSON, so the result would be the following:
["ben","linda","john"]
Terraform provides a ready-to-use function for JSON because its a common need. If you need more control over the above decisions then you'd need to use more complex techniques to describe to Terraform what you need. For example, to produce a string where each input string is in quotes, the items are separated by commas, and the entire result is delimited by [ and ] markers, there are three steps:
Transform the list to add the quotes: [for s in var.names : format("%q", s)]
Join that result using , as the delimiter: join(", ", [for s in var.names : format("%q", s)])
Add the leading and trailing markers: "[ ${join(",", [for s in var.names : format("%q", s)])} ]"
The above makes the same decisions as the JSON encoding of a list, so there's no real reason to do exactly what I've shown above, but I'm showing the individual steps here as an example so that those who want to produce a different list serialization have a starting point to work from.
For example, if the spaces after the commas were important then you could adjust the first argument to join in the above to include a space:
"[ ${join(", ", [for s in var.names : format("%q", s)])} ]"

How can I search the special characters in Solr

I'm used Solr 6.6.2
I need to search the special characters and highlight it in Solr,
But it does not work,
my data :
[
{
"id" : "test1",
"title" : "test1# title C# ",
"dynamic_s": 5
},
{
"id" : "test2",
"title" : "test2 title C#",
"dynamic_s": 10
},
{
"id" : "test3",
"title" : "test3 title",
"dynamic_s": 0
}
]
When I search "C#",
Then it will just response like this "test1# title C# ",
It just highlights "C" this word...and "#" will not searching and highlight.
How can I make the search and highlight work for special characters?
The StandardTokenizer splits tokens on special characters, meaning that # will split the content into separate tokens - the first token will be C - and that's what's being highlighted. You'll probably get the exact same result if you just search for C.
The tokenization process will make your tokens end up being test2 title C .
Using a field type with a WhitespaceTokenizer that only splits on whitespace will probably be a better choice for this exact use case, but it's impossible to say if that'll be a good match for your regular search behavior (i.e. if you actually want to match 'C' to `C-99' etc., splitting by those characters can be needed). But - you can use a specific field for highlighting, and that fields analysis chain will be used to determine what to highlight. And you can ask for both the original and the more specific field to be highlighted, and then use the best result in your frontend application.

Tcl: default if variable is empty?

I've "inherited" some Tcl code, and while I worked through some tutorials and can make sense out of the language, my own Tcl constructs lack a certain... finesse.
For example, I have this code:
puts "Column 'name': [ $queryRs getString name ]"
$queryRs is the result set of a SQL query. The [ $queryRs getString name ] construct retrieves the content of the table column "name" from the current row in the result set. If the database field is NULL, the puts does not print anything.
I would like to print a "default" string instead, i.e. if [ $queryRs getString name ] results in nothing, I'd like to replace it with "--".
Now, I could do something like this:
set nameVar "[ $queryRs getString name ]"
if { [ string length $nameVar ] == 0 } {
set nameVar "--"
}
puts "Column 'name': $nameVar"
But there has to be a more compact solution, something that can be done inline instead of adding four lines and a temporary variable. Help, please?
Two things.
First, Tcl doesn't have a notion of NULL (nil, undefined or whatever) value, and when you want to simulate such a value you have to either use a variable and test for its existence or use an entry in an array of dictionary and test for its existence, too. If such a variable/entry exists, then the value is defined, otherwise it's not.
Unfortunately, the creator of your inherited code apparently did not care about the case where a variable can be NULL so NULLs are indistinguishable from variables having default values (an empty string).
Next, you can use a helper procedure to do what you need:
proc ValueOrDef {val {def --}} {
expr {$val ne "" ? $val : $def}
}
and then go like this:
puts [ValueOrDef [$queryRs getString name]]
puts [ValueOrDef [$queryRs getString name] "some other default"]
You could use the x?y:z construct of the expr command. x is the condition, y is the alternative if the condition is met and z is the alternative if x is not met.
E.g. (but still with a temporary variable):
set nameVar [ $queryRs getString name ]
puts "Column 'name': [expr {[string length $nameVar]>0 ? $nameVar : "--"}]"

Resources