Uploaded image for project: 'mdsal'
  1. mdsal
  2. MDSAL-673

Add nonnullFoo() getters for non-semantic objects

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Done
    • Icon: Medium Medium
    • 11.0.0
    • None
    • Binding codegen

      It is rather common to traverse a container hierarchy in search of a specific leaf, for example:

       

      container foo {
        container bar {
          container baz {
            leaf xyzzy {
              type string;
            }
          }
        }
      }

      we would want to do:
       

      Foo foo;
      String xyzzy = foo.getBar().getBaz().getXyzzy()
      

      but this breaks down on nullness of objects, i.e. if xyxxy does not exist, the datastore can remove it along the entire  container hierarchy.

      We already deal with nullness for the case of YANG lists, by returning an empty list via nonnullFoo(), hence we should be able to generate:

      interface Foo {
        // Normal getter
        @Nullable Bar getBar();
        // Defers to getBar(), or returns a (shared) empty instance
        @NonNull Bar nonnullBar();
      }
      

      Note BarBuilder.EMPTY_INSTANCE should probably not be visible outside the package and may need to be hidden behind a static method for proper laziness of implementation.

      Also this should not happen for containers with a "presence" statement.

      With this, we can do:

      Foo foo;
      String xyzzy = foo.nonnullBar().nonnullBaz().getXyzzy()
      

            opanasiuk Oleksandr Panasiuk
            rovarga Robert Varga
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: