Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-1614

Always getting a value...

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.8.0
    • java
    • [JAVA] Builders can now hold Builder instances of sub schemas.

    Description

      Sometimes the Avro structure becomes deeply nested.
      If in such a scenario you want to be able to set a specific value deep in this tree you want to do this:

      public void setSomething(String value) {
          myStruct
                  .getFoo()
                  .getBar()
                  .getOne()
                  .getOther()
                  .setSomething(value);
      }
      

      The 'problem' I ran into is that any of the 4 get methods can return a null value so the code I have to write is really huge.
      For every step in this method I have to build null checks and create the underlying instance if it is null.
      I already started writing helper methods to do this for parts of my tree.

      To solve this in a way that makes this code readable I came up with the following which I want to propose to you guys (before I start working on a patch).

      My idea is to generate a new 'get' method in addition to the existing normal get method for the regular instance of the class.

      So in addition to the

      public Foo getFoo() {
          return foo;
      }
      

      I propose to generate something like this as well in the cases where this is a type of structure that you may want to traverse as shown in the example.

      public Foo getAlwaysFoo() {
          if (foo == null) {
              setFoo(Foo.newBuilder().build());
          }
          return foo;
      }
      

      This way the automatically created instance immediately has all the defaults I have defined.

      Assuming this naming my code will be readable because it will look like this:

      public void setSomething(String value) {
          myStruct
                  .getAlwaysFoo()
                  .getAlwaysBar()
                  .getAlwaysOne()
                  .getAlwaysOther()
                  .setSomething(value);
      }
      

      Attachments

        1. AVRO-1614-20141027-v1.patch
          3 kB
          Niels Basjes
        2. AVRO-1614-20141201-v2.patch
          11 kB
          Niels Basjes
        3. AVRO-1614-20141202-v3.patch
          41 kB
          Niels Basjes
        4. AVRO-1614-20141204-v4.patch
          29 kB
          Niels Basjes
        5. AVRO-1614-2014-12-16-v5.patch
          29 kB
          Niels Basjes

        Activity

          People

            nielsbasjes Niels Basjes
            nielsbasjes Niels Basjes
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: