-
Notifications
You must be signed in to change notification settings - Fork 44
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
Support for Guava Optional #79
Conversation
* @throws Exception | ||
*/ | ||
@Test | ||
public void testShouldNotGenerateGuavaOptionalsForOptionalMembers() throws Exception { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point :-)
Thank you for this! |
Assume in the following that a request may be authenticated or anonymous (no uid/pwd). It would have taken a dozen lines to do this if nothing was using Optional: public static final Optional<String> jsonPath( String json, String path );
new RequestBuilder()
.withUserId(jsonPath(input, "$.owner.uid")) // Invisibly passes the Optional through
.withPassword(jsonPath(input, "$.owner.pwd").transform(decryptionFunction)) // ignores absent passwords
.build(); |
Marc, I just pushed an alternative variant of this to https://github.com/mkarneim/pojobuilder/tree/drekbour-optional-guava. Essentially I removed the OptionalSupport enum. Instead you can use the Optional class itself. See this example (from your test case): package net.karneim.pojobuilder.processor.with.optionals;
import net.karneim.pojobuilder.GeneratePojoBuilder;
import com.google.common.base.Optional;
@GeneratePojoBuilder(withOptionalProperties = Optional.class)
public class PojoWithGuavaOptional {
public int primitiveInt;
public Integer boxedInt;
public int[] array;
} In order to use Optional from Java 8 just replace the import: package net.karneim.pojobuilder.processor.with.optionals;
import net.karneim.pojobuilder.GeneratePojoBuilder;
import java.util.Optional;
@GeneratePojoBuilder(withOptionalProperties = Optional.class)
public class PojoWithGuavaOptional {
public int primitiveInt;
public Integer boxedInt;
public int[] array;
} Any comments? |
I'd considered doing exactly that so am quite happy you found the same solution. It is also nice as it guarantees your chosen Optional is indeed available at compile time. I think we're done here. |
Resolution for #66
Please don't accept or reject this PR as it is not done (unless you are going to append the fix for the below after accepting!).
It is 99% complete (with test) except that, inside the processor the
withOptionals
directive is exposed as some horrible compiler symbol not the original enum. I remap bytoString
back into theOptionalSupport
enum but it feels very wrong like that.Please advise on Directives.java:35