[MDSAL-673] Add nonnullFoo() getters for non-semantic objects Created: 23/Jun/21  Updated: 02/Nov/22  Resolved: 02/Nov/22

Status: Resolved
Project: mdsal
Component/s: Binding codegen
Affects Version/s: None
Fix Version/s: 11.0.0

Type: Improvement Priority: Medium
Reporter: Robert Varga Assignee: Oleksandr Panasiuk
Resolution: Done Votes: 0
Labels: pt
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Issue split
split from MDSAL-602 Add non-null getters for leaf objects Resolved

 Description   

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()

Generated at Wed Feb 07 20:10:30 UTC 2024 using Jira 8.20.10#820010-sha1:ace47f9899e9ee25d7157d59aa17ab06aee30d3d.