Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created December 4, 2019 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/51733eae50e310c86dd2b57b876af3c4 to your computer and use it in GitHub Desktop.
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
<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>
<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