Created
December 4, 2019 11:26
-
-
Save bennadel/51733eae50e310c86dd2b57b876af3c4 to your computer and use it in GitHub Desktop.
The Elvis Operator Can Be Chained Multiple Times In A Single Expression In Lucee CFML 5.3.3.62
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
apiResponse = { | |
errors: { | |
transaction: { | |
key: "info.number" | |
} | |
} | |
}; | |
// I had an API response object that could report several different types of errors | |
// and I didn't want to build a lot of logic about picking the response apart. So, | |
// I used the safe-navigation operator in conjunction with the Elvis operator to | |
// try and locate the known structures. | |
error = ( apiResponse?.errors?.validation?.token ) | |
?: apiResponse?.errors?.validation?.field | |
?: apiResponse?.errors?.transaction?.key | |
?: "unknown" | |
; | |
echo( "Error: #error#" ); | |
</cfscript> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
// The Elvis operator can be chained several times in a single expression. | |
result = ( nullValue() ) | |
?: nullValue() | |
?: nullValue() | |
?: nullValue() | |
?: "fall-back value" | |
; | |
echo( "Result: #result#" ); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment