websolete
09/06/2022, 2:13 PMvar env = server?.system?.environment ?: {};
if( env.keyExists(arguments.name) ) { ... }
to this:
var env = server?.system?.environment?[arguments.name] ?: "";
or something similar. it's not hugely important to do the second way, just curious if there's a square bracket version of safe navigationthisOldDave
09/06/2022, 2:19 PMwebsolete
09/06/2022, 2:21 PMthisOldDave
09/06/2022, 2:22 PMYou can use the safe navigation operator to access members of a Struct or values of an object.
Instead of ".", using the safe operator ensures that exception is not thrown when the variable is not defined.
websolete
09/06/2022, 2:24 PMvar myValue = evaluate("server?.system?.environment?.#arguments.name# ?: ''");
assuming that worksthisOldDave
09/06/2022, 2:44 PMreturn server.system.properties[ settingName ] ?: server.system.environment[ settingName ] ?: defaultValue;
Dave Merrill
09/06/2022, 2:52 PMthisOldDave
09/06/2022, 2:54 PMDave Merrill
09/06/2022, 3:00 PMwebsolete
09/06/2022, 3:57 PM/**
* Returns the specified environment variable
*
* @name string the name of the env variable in the server.system.environment scope
*
* @throws InvalidEnvVariable thrown when the specified variable does not exist
*
* @return any
*/
private any function getEnvVar( required string name ) {
var env = server?.system?.environment ?: {};
if( len(arguments.name) && env.keyExists(arguments.name) ) {
return env[arguments.name];
}
else {
throw( type="InvalidEnvVariable", message="The specified environment variable [#arguments.name#] does not exist" );
}
}
websolete
09/06/2022, 3:57 PMwebsolete
09/06/2022, 4:00 PMAdam Cameron
st = {
"DOES_EXIST" = "some value"
}
writeoutput(st?["DOES_NOT_EXIST"]) // undefined
writeoutput(st?["DOES_EXIST"]) // some value
key = "DOES_NOT_EXIST"
//writeoutput(st?[key]) // Error: Invalid CFML construct
Note that it only seems to work with string literals. Not with variables. Someone who cares should perhaps raise a bug for that (ping @Mark Takata (Adobe))
Doesn't work at all on Lucee. See above re "someone who cares ~"Adam Cameron
Adam Cameron