Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10223

Add support for Optional to DefaultTypeTransformation.asCollection()

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Trivial
    • Resolution: Fixed
    • None
    • 5.0.0-alpha-1
    • groovy-jdk

    Description

      The JDK recently added support for Optional::stream() which returns a stream containing either the unwrapped value or an empty stream if the Optional is empty.

      In the groovy-jdk, using iteration however will call

      DefaultTypeTransformation.asCollection()

      which does not specifically check for type Optional, instead it will just wrap the Optional itself in a List rather than the unwrapped value as the default behavior.

      Adding an if else clause will allow using the Optional as either a single or empty list.

      if (value instanceOf Optional) {
         return ((Optional)value).map(List::of).orElse(List.of())
      }

       Alternatively, add an asList() to the Optional class.

       (edit: 2021-09-13, missing bracket in code example)

       

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              stephenns Stephen Smith
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: