Hey All... Is this a bug? it appears that elvis op...
# lucee
c
Hey All... Is this a bug? it appears that elvis operator can't be used directly on http request headers? https://trycf.com/gist/0d632599e530676c6e7668f3b5284c2f/lucee5
z
Depends on the type? That's coming from java land
d
The same code (if you change all the
dump
functions to
writeDump
) works fine under ACF, so if compatibility is a bug, then I would say it's a bug. 😁
I will say, this one of those examples that shows the shortcomings of these operator shortcuts. You can't even use a safe navigator, since it doesn't work with brackets. It would have been nice if the Elvis/null coalescing operator just treated non-existant and null values the same way, so if anything on the left of the operator could not be evaluated for any reason, it just used the right-hand side evaluation.
s
Hmm, interesting… I never knew you could directly use a variable like that in an elvis expression ( like ā€œvariable?:othervariableā€) to begin with. I always thought you had to have a condition like ā€œcondition ? valueiftrue : valueiffalseā€ So I would have done this: dump(structkeyexists(getHTTPRequestData().headers, key) ? getHTTPRequestData().headers[key] : "has left the building"); Now I’m going to play with that… I’m always looking for ways to write less code. 😜
i
If you set the headers to a variable first, elvis works as expected.. https://trycf.com/gist/82dd468dc48f17f3e9ac7c2dd607b72d/lucee5?theme=monokai
z
the problem is the function call doesn't get wrapped into an elvis
Elvis.operate(paramPageContext, 0.0D, new String[] { "HEADERS", Caster.toString(<http://paramPageContext.us|paramPageContext.us>().get(KeyConstants._KEY)) }) ? paramPageContext.get(<http://paramPageContext.us|paramPageContext.us>().getCollection(this.keys[0]), KeyImpl.intern(Caster.toString(<http://paramPageContext.us|paramPageContext.us>().get(KeyConstants._KEY)))) : "has left the building";
vs
Object object;
((object = paramPageContext.get(paramPageContext.getCollection(GetHTTPRequestData.call(paramPageContext), this.keys[0]), KeyImpl.intern(Caster.toString(<http://paramPageContext.us|paramPageContext.us>().get(KeyConstants._KEY))))) == null) ? "has left the building" : object;