Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.5.4, 1.5.5, 1.5.6, 1.6-beta-1
    • 1.5.7, 1.6-beta-2
    • groovy-jdk
    • None

    Description

      Add leftShift operators to map that receive a map or a String presentation of a map-entry

      Attachments

        Activity

          bartrobeyns Bart Robeyns added a comment -

          Following a discussion on groovy-user ('Strings to Map.Elements'), this seemed interesting.
          Improvement should enable

          m = [:]
          m << "a:b"
          m << [c:'d']
          assert m == [a:'b',c:'d']

          Adding following two methods to DefaultGroovyMethods might do the trick:

          public static Map leftShift(Map self, Map other)

          { self.putAll(other); return self; }

          public static Map leftShift(Map self, String other)

          { String[] parts = other.split(":"); self.put(parts[0],parts[1]); return self; }
          bartrobeyns Bart Robeyns added a comment - Following a discussion on groovy-user ('Strings to Map.Elements'), this seemed interesting. Improvement should enable m = [:] m << "a:b" m << [c:'d'] assert m == [a:'b',c:'d'] Adding following two methods to DefaultGroovyMethods might do the trick: public static Map leftShift(Map self, Map other) { self.putAll(other); return self; } public static Map leftShift(Map self, String other) { String[] parts = other.split(":"); self.put(parts[0],parts[1]); return self; }

          only m << [c:'d'] is in question, to align it with map.plus like with list.plus and list.leftShift

          blackdrag Jochen Theodorou added a comment - only m << [c:'d'] is in question, to align it with map.plus like with list.plus and list.leftShift
          paulk Paul King added a comment -

          I assume what is being asked for is a way to add Map.Entry values to a map, e.g.:

              /**
               * Overloads the left shift operator to provide an easy way to append
               * Map.Entry values to a Map.
               *
               * @param self  a Map
               * @param entry a Map.Entry to be added to the Map.
               * @return same map, after the value has been added to it.
               */
              public static Map leftShift(Map self, Map.Entry entry) {
                  self.put(entry.getKey(), entry.getValue());
                  return self;
              }
          
          paulk Paul King added a comment - I assume what is being asked for is a way to add Map.Entry values to a map, e.g.: /** * Overloads the left shift operator to provide an easy way to append * Map.Entry values to a Map. * * @param self a Map * @param entry a Map.Entry to be added to the Map. * @ return same map, after the value has been added to it. */ public static Map leftShift(Map self, Map.Entry entry) { self.put(entry.getKey(), entry.getValue()); return self; }
          paulk Paul King added a comment -

          This has been added. If we want to do more with String notations, I would suggest considering first something like:

          "abc:def".toEntry()
          

          It would be great to see a bunch of use cases first though.

          We could later consider whether to overload leftShift for maps to take a string and automatically call the toEntry.

          paulk Paul King added a comment - This has been added. If we want to do more with String notations, I would suggest considering first something like: "abc:def" .toEntry() It would be great to see a bunch of use cases first though. We could later consider whether to overload leftShift for maps to take a string and automatically call the toEntry .
          sjtai Tai Siew Joon added a comment -

          If we use the String notation, "abc:def:ghi" can represent one of the two possibilities:

          1. abc:def is the key and ghi is the value
          2. abc is the key and def:ghi is the value
          sjtai Tai Siew Joon added a comment - If we use the String notation, "abc:def:ghi" can represent one of the two possibilities: abc:def is the key and ghi is the value abc is the key and def:ghi is the value
          paulk Paul King added a comment -

          Re: "... one of two possibilities ..."

          Indeed, that is why I have not progressed that option within this issue. We need to think through all of the cases and usage scenarios and then map out how we would go forward. What if someone wants "abc:new Date()". Should we allow that too? Would the value become the current date or the string "new Date()"?

          paulk Paul King added a comment - Re: "... one of two possibilities ..." Indeed, that is why I have not progressed that option within this issue. We need to think through all of the cases and usage scenarios and then map out how we would go forward. What if someone wants "abc:new Date()". Should we allow that too? Would the value become the current date or the string "new Date()"?

          People

            paulk Paul King
            bartrobeyns Bart Robeyns
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: