Is the if / else logic valid in a BizTalk Message Assignment shape?
I'm getting some event log errors regarding ErrorReport.FailedTime having no value, so I thought I'd put a guard clause in the
if (ErrorReport.FailureTime exists Msg_Failed)
{
Var_FailureTime = Msg_Failed(ErrorReport.FailureTime);
}
else
{
Var_FailureTime = System.DateTime.Now;
}
... rest of code constructing the error report message ...
But the compiler fails with ...
error X2254: unexpected keyword: 'if'
That is the expected behavior.
'If' is not supported in the Message Assignment Shape but it is supported in the Expression Shape. so, you will have to do this test/assigment before the Construct Shape.
Related
Map<String,bool> map= { "key1":true, "key2":false };
/*
* Flags following compilation error:
* A nullable expression can't be used as a condition.
* Try checking that the value isn't 'null' before using it as a condition.
*/
if(map["key1"]) {
//do sth
}
/*So I try checking value isn't null as specified in error
*Still flags same compilation error
*/
if(map!=null && map["key1"]) {
//do sth
}
//This works
if(map["key1"] == true) {
//do sth
}
}
Based on the following snippet, may I know why both the 1st and 2nd if blocks fail but not the 3rd?
You misunderstood the error message.
A nullable expression can't be used as a condition.
means that you can't do:
bool? condition;
if (condition) {
...
}
Map<K, V>'s operator[] returns a V?. It returns a nullable type as a way of indicating failure when the key isn't found, and you need to check that the returned value isn't null, not that map itself is not null. For example:
if (map["key"] ?? false) {
...
}
Your third approach (which checks == true) works because it will perform a null == true equality check if the lookup returns null. However, you should prefer using ?? false since it conveys the intent better, and equality checks against true or false are usually a code smell.
The [] operator on Map can return null which makes it nullable which is explained in details here: https://dart.dev/null-safety/understanding-null-safety#the-map-index-operator-is-nullable
So your first example is invalid since null is not a bool. So you cannot directly use the value from the [] operator for a Map.
Your second example is invalid for the same reason since map["key1"] is bool?.
Third example works since null == true is always false. So it is fully valid to make a comparison which involves something which can be null.
I have C code that uses Tcl/Tk library. I then create a library using this C code which is then linked to R language package. I have a function in the C code:
int doSomething(){
if(TRUE){
return TCL_OK;
else{
TCL_RESULT3("Error")
return TCL_OK;
}
Currently, I use TCL_RESULT3("Error") in the C code, and then in R check if result <- tclvalue(tcl(...)) #calls doSomething() returns a string with Error:
if (startsWith(result, "Error"))
{
return(FALSE)
}
return(TRUE)
and base further actions in R on the returned value.
However, even though there is an error, I still call TCL_OK because TCL_ERROR produces something that R cannot seem to handle (at least it is not straightforward). My question is if it is possible to restructure my function as:
int doSomething(){
if(TRUE){
return TCL_OK;
else{
return TCL_ERROR;
}
and have R understand what is being returned. Currently, if TCL_OK is returned, then result will be an empty string "". If TCL_ERROR is returned, then result will yield: Error in structure(.External(.C_dotTclObjv, objv), class = "tclObj") : [tcl] . which R does not know how to handle.
Is it possible to have R handle such an error (i.e. return a value of FALSE if this error pops up) or is checking for an error message using TCL_RESULT3() in conjunction with TCL_OK the best method for such a process?
When your C code returns TCL_ERROR (instead of TCL_OK) that's an error condition, which is logically like an exception in other languages. Can R handle those? Yes (apparently; I know very little R). Here's one way (there are others, of course; the right choice depends on what you're doing and the likely cause of the exception).
result <- tryCatch(
{
# This is where you put in the code to call into Tcl
tclvalue(tcl(...)) # calls doSomething()
},
error = function(err) {
message(paste("error occurred in Tcl code:", err))
return(NaN) # Need an alternative result value here
}
)
Note that if you're calling an arbitrary Tcl command, you pretty much have to handle exceptions. Failures are a fact of life, and you need to allow for them in robust code.
I'm trying to pass a cursor to a function like this:
.create-or-alter function GetLatestValues(cursor:string) {
Logs | where cursor_after(cursor)
}
But I get a Error cursor_after(): argument #1 is invalid response. Is there something I'm missing? Is string the wrong data type to use?
This approach works properly for lambda functions:
let GetLatestValues = (cursor:string) {
Logs | where cursor_after(cursor)
};
GetLatestValues('') | take 1
By default, defining a function in Kusto validates stored function, and in case you're using cursor the system will attempt to do it with empty parameter, leading to failure.
You can override this behavior using skipvalidation=true parameter (see docs at:
https://learn.microsoft.com/en-us/azure/kusto/management/functions#create-function)
.create-or-alter function with (skipvalidation = "true")
GetLatestValues(cursor:string)
{
Logs | where cursor_after(cursor)
}
I am getting the error Fatal error: Unexpectedly found nil while unwrapping an optional value at my line
let pictureRef = self.actStorage.child("\(user.uid).jpg")
I know that this is caused by implicitly trying to unwrap an optional which is my
var actStorage: StorageReference!
my question is how would I use optional binding or a guard statement so that it is no longer nil?
If Swift can compile the code without inserting an implicit unwrap, it does so. You can change the code so it doesn't need an implicit unwrap:
guard let pictureRef = self.actStorage?.child("\(user.uid).jpg") else {
return
}
However, it might be better to change the type of actStorage to an explicit optional (StorageReference?) instead, if you can change the type.
This will do:
if let pictureRef = self.actStorage?.child("\(user.uid).jpg") {
// pictureRef exists
} else {
// picutreRef nill
}
I have a task that involves using an Orchestration to de-batch a multi-record XML file and then sort it based on one field's value. The first expression outside the loop gets the record count:
recordCount = System.Convert.ToInt32(xpath(CustFile,("count/*[local-name()='Root' and namespace-uri()='']/*[local-name()='People' and namespace-uri()='']/*[local-name()='Customer' and namespace-uri()='']")));
counter = 0;
recordNumber = 0;
Next Expression inside the loop sets the Xpath value:
sXPath = System.String.Format("/*[local-name()='Root' and namespace-uri()='']/*[local-name()='People' and namespace-uri()='']/*[local-name()='Customer' and namespace-uri()='']", recordNumber);
The Next Expression defines the final message:
InternalCust = xpath(CustFile,sXPath);
The final expression increments the record counter for the loop to back and start again with the next record:
counter = counter + 1;
I think I can manage the sorting of the output message, but when I try to run it as is I get the following error in the console:
xlang/s engine event log entry: Uncaught exception (see the 'inner exception' below) has suspended an instance of service 'BizTalk_SelfStudy_Week_4_Project.BizTalk_Orchestration1(ae65e0c4-9db7-6f19-1e08-6f4fbe08affe)'.
The service instance will remain suspended until administratively resumed or terminated.
If resumed the instance will continue from its last persisted state and may re-throw the same unexpected exception.
InstanceId: 4a2d7256-4882-4853-8f7c-6e6054e78c4c
Shape name: Debatch Message
ShapeId: 6ee14c8d-e55b-408b-be63-e5d83fa412a6
Exception thrown from: segment 1, progress 19
Inner exception: The part 'part' of message 'InternalCust' contained a null value at the end of the construct block.
Exception type: NullPartException
Source: Microsoft.XLANGs.Engine
Target Site: Void ConstructionCompleteEvent(Boolean)
The following is a stack trace that identifies the location where the exception occured
at Microsoft.XLANGs.Core.Part.ConstructionCompleteEvent(Boolean fKillUnderlyingPart)
at Microsoft.XLANGs.Core.XMessage.ConstructionCompleteEvent(Boolean killUnderlyingPartWhenDirty)
at BizTalk_SelfStudy_Week_4_Project.BizTalk_Orchestration1.segment1(StopConditions stopOn)
at Microsoft.XLANGs.Core.SegmentScheduler.RunASegment(Segment s, StopConditions stopCond, Exception& exp)
I am at a loss, as I've tried to validate the xpath and all I get is the invalid token message on the validator. Ideas anyone?
As Johns-305 has pointed out, your construction of the XPath is wrong
sXPath = System.String.Format("/*[local-name()='Root' and namespace-uri()='']/*[local-name()='People' and namespace-uri()='']/*[local-name()='Customer' and namespace-uri()='']", recordNumber);
It is missing a placeholder e.g. '{0}' which it would substitute the recordNumber into.
It probably should look like the below, which tells it which instance of Customer to select.
sXPath = System.String.Format("/*[local-name()='Root' and namespace-uri()='']/*[local-name()='People' and namespace-uri()='']/*[local-name()='Customer' and namespace-uri()=''][{0}]", recordNumber);
It also helps to Debug an Orchestration which would have allowed you to see that the XPath didn't include the Record Number and to test the Xpaths produced, a tool useful for this is Dan Sharps XML Viewer