expression-language icon indicating copy to clipboard operation
expression-language copied to clipboard

Support Merge Collection/Map using + Operator

Open hantsy opened this issue 1 year ago • 3 comments

To simplify the operators of concatenating Collections/Maps.

  • aList + aSet returns a new list
  • aMap+ anotherMap returns a new Map.

hantsy avatar Jul 26 '24 03:07 hantsy

This needs to be more precisely defined. I'd lean towards referencing an existing method (addAll(Collection) / putAll(Map)) rather than trying to define our own merge function. Cleaner to write the spec, easier for users to understand and easy to implement.

markt-asf avatar Aug 12 '24 14:08 markt-asf

I have an implementation of this in the current Tomcat 12 development branch. You can download SNAPSHOT build a standard distribution from: https://repository.apache.org/content/groups/snapshots/org/apache/tomcat/tomcat/12.0.0-M1-SNAPSHOT/

Embedded and individual JARs are also available.

If you could try that and and confirm whether or not it works for you that would be great. I can then work on the changes required to the specification document and some TCK tests.

markt-asf avatar Aug 22 '24 10:08 markt-asf

It has been over a month without feedback. The operator works as expected for me so I am going to proceed with the spec updates and TCK tests for 6.1.

markt-asf avatar Sep 20 '24 10:09 markt-asf

Before adding this to the specification, I think there is a choice to make regarding syntax.

String concatenation uses += rather than + to avoid any potential ambiguity due to the coercion rules. For example, ${'5' + '3'} should result in 8 rather than '53'. To get '53' the expression would need to be ${'5' += '3'}.

There are no such ambiguity risks exists for Collections and Maps but from a consistency point of view there is an argument that the concatenation/merge operator for Collections and Maps should be += rather than +.

I am leaning towards using += rather than +. Any objections?

markt-asf avatar Apr 25 '25 11:04 markt-asf

Should we really support ${'5' + '3'} = 8 in future? Maybe we can deprecate + add config

tandraschko avatar Apr 25 '25 11:04 tandraschko

Should we really support ${'5' + '3'} = 8 in future? Maybe we can deprecate + add config

Yes, we should continue to support that. Not doing so means changing the coercion rules for String which would cause massive breakage throughout the ecosystem.

markt-asf avatar Apr 25 '25 11:04 markt-asf