I am using the dynamic block like below for s3 encryption. I wanted the values based on 'true' for sse-algorithm under the rule. If it's true, I need to assign the value "aws:kms" else I need to assign to "AES256". Any help on this is appreciated?
dynamic "server_side_encryption_configuration" {
for_each = local.encryption == "SSE-KMS" ? [true] : []
content {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "aws:kms"
kms_master_key_id = local.kmsKey
}
}
}
}
You are rather close and merely need to remove the conditional dynamic block, and refactor the ternary logic to the sse_algorithm argument value:
rule {
apply_server_side_encryption_by_default {
sse_algorithm = local.encryption == "SSE-KMS" ? "aws:kms" : "AES256"
kms_master_key_id = local.encryption == "SSE-KMS" ? local.kmsKey : null
}
}
Related
how to use ternary if else with two or more condition using "OR" and "AND" like
if(foo == 1 || foo == 2)
{
do something
}
{
else do something
}
i want to use it like
foo == 1 || foo == 2 ? doSomething : doSomething
If you're referring to else if statements in dart, then this ternary operator:
(foo==1)? something1():(foo==2)? something2(): something3();
is equivalent to this:
if(foo == 1){
something1();
}
elseif(foo == 2){
something2();
}
else something3();
For three conditions use:
value: (i == 1) ? 1 : (i == 2) ? 2 : 0
Try below
(2 > 3)?print("It is more than 3"):print("It is less than 3");
////Prints It is less than 3 to the console
For AND try this,
// here both or multiple conditions needs to satisfy
if (primaryImageUploaded == true && signatureImageUploaded == true) {
// status bool condition in true
} else {
// if false
}
For OR try this,
// here need ONLY any one condition to satisfy
if (primaryImageUploaded == true || signatureImageUploaded == true) {
// status bool condition in true
} else {
// if false
}
Another Dart Syntax
if (100 > 50) {
print("100 is greater than 50");
}
it is easy,
if(foo == 1 || foo == 2)
{
do something
}
{
else do something
}
it can be written thus for OR statement
foo==1 || foo==2 ? do something : else do something
it can be written thus for AND statement
foo==1 && foo==2 ? do something : else do something
both will work perfectly
EDITED
The original answer has run a little bit of from the question asked. Below is my edited answer.
To use ternary operator
(foo == 1 || foo == 2) ? doSomething() : doSomethingElse();
For my cleaner approach
{1, 2}.contains(foo) ? doSomething() : doSomethingElse();
ORIGINAL
The cleaner way for me is
if ({1, 2}.contains(foo)) {
//do something
} else {
//do something else
}
Here is an example of the same
Text((managerStatus == "pending")
? "Requested"
: (adminStatus == "confirm")
? "Amount credited"
: "Admin Pending")
Try this:
foo == 1 ? doSomething1 : (foo == 2 ? doSomething1 : doSomething2)
If you have to include multiple conditions then you should use parantheses
Simple Multiple check in one condition
if(in_array($foo, [1,2,'other'])) {
//do something
}
else {
//else do something
}
void main(){
var a,b,c,d;
a = 7;
b = 9;
c = 11;
d = 15;
print((a>b)?((a>c)?((a>d)?a:d):c):(b>c)?((b>d)?b:d):(c>d)?c:d);
}
I'm trying to filter an array in swift, it works great when I'm just trying to filter a few things but when I add to the list I get this error:
Expression was too complex to be solved in reasonable time; consider
breaking up the expression into distinct sub-expressions
Here is my code with the above error:
filteredArray = workArray.filter { $0.stateName.localizedCaseInsensitiveContainsString(searchString!) || $0.firstName.localizedCaseInsensitiveContainsString(searchString!) || $0.lastName.localizedCaseInsensitiveContainsString(searchString!) || $0.countyName.localizedCaseInsensitiveContainsString(searchString!) || $0.cityName.localizedCaseInsensitiveContainsString(searchString!) || $0.communityName.localizedCaseInsensitiveContainsString(searchString!) || $0.sectionName.localizedCaseInsensitiveContainsString(searchString!) || $0.notes.localizedCaseInsensitiveContainsString(searchString!) || $0.email1.localizedCaseInsensitiveContainsString(searchString!) || $0.email2.localizedCaseInsensitiveContainsString(searchString!) || $0.email3.localizedCaseInsensitiveContainsString(searchString!) || $0.title.localizedCaseInsensitiveContainsString(searchString!) || $0.jobsiteID.localizedCaseInsensitiveContainsString(searchString!)}
I have tried to split this process up like this
filteredArray = workArray.filter { $0.stateName.localizedCaseInsensitiveContainsString(searchString!) || $0.firstName.localizedCaseInsensitiveContainsString(searchString!) || $0.lastName.localizedCaseInsensitiveContainsString(searchString!) || $0.countyName.localizedCaseInsensitiveContainsString(searchString!) || $0.cityName.localizedCaseInsensitiveContainsString(searchString!) || $0.communityName.localizedCaseInsensitiveContainsString(searchString!) || $0.sectionName.localizedCaseInsensitiveContainsString(searchString!) || $0.notes.localizedCaseInsensitiveContainsString(searchString!) || $0.email1.localizedCaseInsensitiveContainsString(searchString!)}
and
filteredArray.appendContentsOf(workArray.filter { $0.email2.localizedCaseInsensitiveContainsString(searchString!) || $0.email3.localizedCaseInsensitiveContainsString(searchString!) || $0.title.localizedCaseInsensitiveContainsString(searchString!) || $0.jobsiteID.localizedCaseInsensitiveContainsString(searchString!)})
But I am getting duplicate objects in the array.
I could write something else that would then look for and delete duplicate objects but I would rather not. My question is how should I be filtering all this items.
Thank you for all the help
Factor that behemoth of an expression out to a method on your datatype.
extension MyDataThingy {
func anyFieldContains(searchTerm term: String) -> Bool {
let fieldValues = [self.stateName, self.firstName, /* etc. */]
for value in fieldValues {
if value.localizedCaseInsensitiveContainsString(term) {
return true
}
}
return false
}
}
Then:
filteredArray = workArray.filter { $0.anyFieldContains(searchTerm: searchTerm) }
This will fix the timeout error from the type inference engine. It is also more readable, more understandable, and more maintainable.
Extended syntax
Try the extended syntax
workArray.filter { elm -> Bool in
// put your conditions here
}
this way you are helping the compiler to understand that the closure receive an element of the type of your array and returns a Bool value.
I am new to programming and trying to find a simpler way to do this:
if state[0] != 0 && state[1] != 0 && state[2] != 0 && state[3] != 0 && state[4] != 0 && state[5] != 0 && state[6] != 0 && state[7] != 0 && state[8] != 0 {
gameOverLabel.text = "It's a tie."
gameOverLabel.hidden = false
active = false
}
I tried the code below but it reacted like a OR rather than a AND.
if state[0&1&2&3&4&5&6&7&8] != 0 {
gameOverLabel.text = "It's a tie."
gameOverLabel.hidden = false
active = false
}
Thanks for any help!
Assuming that your intention is to check if all array elements
are different from zero, the easiest approach would be
if !state.contains(0) { ... }
Your code
if state[0&1&2&3&4&5&6&7&8] != 0 { ... }
does not work
as intended because here the bitwise AND 0&1&2&3&4&5&6&7&8
is computed first (with result zero), so that is equivalent to
if state[0] != 0 { ... }
let state = [0,1,2,0,4,5,6,7]
if state.filter{$0 != 0}.count > 0 {
gameOverLabel.text = "It's a tie."
gameOverLabel.hidden = false
active = false
}
Try this one
The general case with an Array of indices
The short version
let indices = [4,2,9,6,5,3,8,0]
if !indices.contains({ state[$0] == 0 }) {
// ...
}
A second version which only shows the possibilities of Swift:
if !indices.lazy.map{ state[$0] }.contains(0) {
// ...
}
The lazy is used since otherwise map would apply the closure to all indices whereas lazy applies it on average only to half the elements (contains determines the number of executions of the closure).
Note: There is probably no performance improvement for a small amount of indices. The first version is certainly (ever so slightly) faster.
A specific range of indices
Just use the approach above with
let indices = 5...9
Or directly operate on the sub sequence of Array which is ArraySlice.
Side note: Even though it seems that ArraySlice (return type of Array[Range<Int>]) is a value type, internally it is a reference type (like Array) which only copies itself on mutation. In addition ArraySlice is only a view into the array (as long none of them is mutated). So it could be even faster than the first approach.
if !state[24...42].contains(0) {
// ...
}
I am using umbraco version 7.2.6 . I want to add macro parameter of type Dropdownlist .
How can I set source (data come from database ) of dropdownlist ??
thanks
Had the same situation, I am aware that the 'proper' way to do it would be like described in http://www.richardsoeteman.net/2010/01/04/createacustommacroparametertype.aspx, but for my purpose this would have been too much fuss. What I suggest here instead is not elegant, but it's easy to implement.
Create a macro parameter type Numeric and explain in the description which number stands for which result. In the Macro Partial View assign the number to the respective result.
Example
Description of the Macro parameter:
Alias: dimension
Description: 1: 300x225 2:400x300 3:600x450 4:800x600
Type: Numeric
Code in the Macro Partial View:
var defaultdim = "medium";
if (Model.MacroParameters["dimension"] != null)
{
var dim = Convert.ToInt32( Model.MacroParameters["dimension"] );
if(dim == 1) { defaultdim = "small"; }
else if(dim == 2) { defaultdim = "medium"; }
else if(dim == 3) { defaultdim = "large"; }
else if(dim == 4) { defaultdim = "xlarge"; }
}
"small", "medium" ... are crop names and stand for the dimensions shown in the parameter description.
I am using xerces c++ to manipulate an xml file? but getNodeValue() and setNodeValue() are not working but getNodeName() is working. Do anyone has any suggestions?
if( currentNode->getNodeType() && currentNode->getNodeType() == DOMNode::ELEMENT_NODE )
{
// Found node which is an Element. Re-cast node as element
DOMElement* currentElement= dynamic_cast< xercesc::DOMElement* >( currentNode );
if( XMLString::equals(currentElement->getTagName(), TAG_ApplicationSettings))
{
// Already tested node as type element and of name "ApplicationSettings".
// Read attributes of element "ApplicationSettings".
const XMLCh* xmlch_OptionA = currentElement->getAttribute(ATTR_OptionA);
m_OptionA = XMLString::transcode(xmlch_OptionA);
XMLCh* t,*s;
//s= XMLString::transcode("manish");
//currentNode->setElementText(s);
t=(XMLCh*)currentNode->getNodeName();
s=(XMLCh*)currentNode->getNodeValue();
cout<getNodeValue()) << "\n";
A DOMElement may contain a collection of other DOMElements or a DOMText. To get the text value of an element you need to call the method getTextContent(), getNodeValue will always return NULL.
The is another better way conceptually, as the DOMText is a child of the DOMElement we can traverse through the child node and get the value.
Below is the logic in the form of a method:
string getElementValue(const DOMElement& parent)
{
DOMNode *child;
string strVal;
for (child = parent.getFirstChild();child != NULL ; child = child->getNextSibling())
{
if(DOMNode::TEXT_NODE == child->getNodeType())
{
DOMText* data = dynamic_cast<DOMText*>(child);
const XMLCh* val = data->getWholeText();
strVal += XMLString::transcode(val);
}
else
{
throw "ERROR : Non Text Node";
}
}
return strVal;
}
Hope this helps :)
getNodeValue() will always return an empty string, because the "value" of an element node is in its child. In our case it is text node child. Either way is to iterate through child nodes
or use getTextContent.
First check for child nodes in a node using hasChildNodes() then use methods like getFirstChild() etc. . Afterwards use getNodeValue().
DOMNode* ptrDomNode = SomeNode;
if(ptrDomNode->hasChildNodes())
{
DOMNode* dTextNode = ptrDomNode->getFirstChild();
char* string = XMLString::transcode(dTextNode->getNodeValue());
}