Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more helper methods for dealing with union types #373

Open
berler opened this issue May 8, 2019 · 1 comment
Open

Add more helper methods for dealing with union types #373

berler opened this issue May 8, 2019 · 1 comment

Comments

@berler
Copy link
Contributor

berler commented May 8, 2019

What happened?

We have a lot of data structures that are unions with many possible value types (sometimes over a dozen). Working with these can be very difficult since code needs to implement a visitor for all possible types, even if the code wants to ignore most of those types, or in the cases of tests assert that the object has a specific type (in tests it isn't very obvious how you would even construct a Matcher for these without implementing a visitor that defines every single method -- even the ones you don't want to match against). Using just object equals in tests is often not an option since the test might only want to assert on parts of the value, or some fields might be things like timestamps that change (in the case of integration tests etc). The current workaround is often for testing purposes to define a visitor that throws in every method, and then in tests extend that base visitor and @Override the one (or several) methods you want to handle in the test.

What did you want to happen?

Consider adding generated methods to union types that allow you to easily test if a union object has a specific type, and retrieve the value from it. These would work by allowing the user to create a java.util.function.Consumer that accepts that type.

As a concrete example, consider this (partial) conjure object definition:

MyData:
  union:
    foo: FooType
    # ... many other subtypes follow

Methods like this could be generated on MyData:

public void ifFoo(Consumer<FooType> consumer) {
    if (value instanceof FooWrapper) {
        consumer.accept((FooWrapper) value).value);
    }
}

Similarly we could generate a method like this to throw if the object does not have that type:

public void requireFoo(Consumer<FooType> consumer) {
    if (value instanceof FooWrapper) {
        consumer.accept((FooWrapper) value).value);
    } else {
        throw new IllegalStateException(...);
    }
}

Another alternative is to auto generate an abstract visitor that throws in every method, so that you can extend that and @Override the specific methods you want.

@abless
Copy link

abless commented Dec 3, 2019

+1, though I'd rather have a Optional<FooType> getFoo() method instead.

@iamdanfox, are there any plans on supporting this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants