groovy
  1. groovy
  2. GROOVY-5134

@ToString optionally exclude fields with null values

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Trivial Trivial
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.0-beta-3
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      Description

      Particularly on objects with a high number of fields, having the ability to exclude fields which are null from the output of the generated toString would be a nice to have.

        Activity

        Hide
        Paul King added a comment -

        There is a bit of a hack which might work for you now as a workaround depending on your circumstances (shown for Immutable):

        import groovy.transform.Immutable
        @Immutable class Name {
          String title, firstname, surname
          String toString() {
            _toString().replaceAll(/(, )?[a-z]*:null(, )?/, '')
          }
        }
        
        println new Name(firstname: 'Cher')
        
        Show
        Paul King added a comment - There is a bit of a hack which might work for you now as a workaround depending on your circumstances (shown for Immutable): import groovy.transform.Immutable @Immutable class Name { String title, firstname, surname String toString() { _toString().replaceAll(/(, )?[a-z]*: null (, )?/, '') } } println new Name(firstname: 'Cher')
        Hide
        Andre Steingress added a comment -

        Is someone working on this issue? Otherwise I'd be willing to contribute

        Show
        Andre Steingress added a comment - Is someone working on this issue? Otherwise I'd be willing to contribute
        Hide
        Andre Steingress added a comment -
        Show
        Andre Steingress added a comment - issued a pull request: https://github.com/groovy/groovy-core/pull/26
        Hide
        Paul King added a comment -

        Wondering whether "ignoreNulls" is good enough instead of "ignoreNullValues"?

        Show
        Paul King added a comment - Wondering whether "ignoreNulls" is good enough instead of "ignoreNullValues"?
        Hide
        Paul King added a comment -

        Thanks Andre, applied your patch with a few tweaks (I noticed that some other earlier changes that were supposed to be there for 2.0 hadn't made it - think I had them on a different branch that wasn't committed at some point - anyway fixed now - but the earlier change was the main cause of the tweaks)

        Show
        Paul King added a comment - Thanks Andre, applied your patch with a few tweaks (I noticed that some other earlier changes that were supposed to be there for 2.0 hadn't made it - think I had them on a different branch that wasn't committed at some point - anyway fixed now - but the earlier change was the main cause of the tweaks)

          People

          • Assignee:
            Paul King
            Reporter:
            Michael Bishop
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: