-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert #22. Since it was originally just an experiment and it caused #44, I hope its ok. I am keen to open up the discussion on how implement unions, especially if we have some numbers around the performance of one approach vs. another
- Loading branch information
Showing
14 changed files
with
1,070 additions
and
753 deletions.
There are no files selected for viewing
199 changes: 199 additions & 0 deletions
199
conjure-java-core/src/integrationInput/java/com/palantir/product/SingleUnion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
package com.palantir.product; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAnyGetter; | ||
import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import javax.annotation.Generated; | ||
|
||
@Generated("com.palantir.conjure.java.types.UnionGenerator") | ||
public final class SingleUnion { | ||
private final Base value; | ||
|
||
@JsonCreator | ||
private SingleUnion(Base value) { | ||
this.value = value; | ||
} | ||
|
||
@JsonValue | ||
private Base getValue() { | ||
return value; | ||
} | ||
|
||
public static SingleUnion foo(String value) { | ||
return new SingleUnion(new FooWrapper(value)); | ||
} | ||
|
||
public <T> T accept(Visitor<T> visitor) { | ||
if (value instanceof FooWrapper) { | ||
return visitor.visitFoo(((FooWrapper) value).value); | ||
} else if (value instanceof UnknownWrapper) { | ||
return visitor.visitUnknown(((UnknownWrapper) value).getType()); | ||
} | ||
throw new IllegalStateException( | ||
String.format("Could not identify type %s", value.getClass())); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return this == other | ||
|| (other instanceof SingleUnion && equalTo((SingleUnion) other)) | ||
|| (other instanceof String | ||
&& value instanceof FooWrapper | ||
&& Objects.equals(((FooWrapper) value).value, other)); | ||
} | ||
|
||
private boolean equalTo(SingleUnion other) { | ||
return this.value.equals(other.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(value); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringBuilder("SingleUnion") | ||
.append("{") | ||
.append("value") | ||
.append(": ") | ||
.append(value) | ||
.append("}") | ||
.toString(); | ||
} | ||
|
||
public interface Visitor<T> { | ||
T visitFoo(String value); | ||
|
||
T visitUnknown(String unknownType); | ||
} | ||
|
||
@JsonTypeInfo( | ||
use = JsonTypeInfo.Id.NAME, | ||
property = "type", | ||
visible = true, | ||
defaultImpl = UnknownWrapper.class | ||
) | ||
@JsonSubTypes(@JsonSubTypes.Type(FooWrapper.class)) | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
private interface Base {} | ||
|
||
@JsonTypeName("foo") | ||
private static class FooWrapper implements Base { | ||
private final String value; | ||
|
||
@JsonCreator | ||
private FooWrapper(@JsonProperty("foo") String value) { | ||
Objects.requireNonNull(value, "foo cannot be null"); | ||
this.value = value; | ||
} | ||
|
||
@JsonProperty("foo") | ||
private String getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return this == other || (other instanceof FooWrapper && equalTo((FooWrapper) other)); | ||
} | ||
|
||
private boolean equalTo(FooWrapper other) { | ||
return this.value.equals(other.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(value); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringBuilder("FooWrapper") | ||
.append("{") | ||
.append("value") | ||
.append(": ") | ||
.append(value) | ||
.append("}") | ||
.toString(); | ||
} | ||
} | ||
|
||
@JsonTypeInfo( | ||
use = JsonTypeInfo.Id.NAME, | ||
include = JsonTypeInfo.As.EXISTING_PROPERTY, | ||
property = "type", | ||
visible = true | ||
) | ||
private static class UnknownWrapper implements Base { | ||
private final String type; | ||
|
||
private final Map<String, Object> value; | ||
|
||
@JsonCreator | ||
private UnknownWrapper(@JsonProperty("type") String type) { | ||
this(type, new HashMap<String, Object>()); | ||
} | ||
|
||
private UnknownWrapper(String type, Map<String, Object> value) { | ||
Objects.requireNonNull(type, "type cannot be null"); | ||
Objects.requireNonNull(value, "value cannot be null"); | ||
this.type = type; | ||
this.value = value; | ||
} | ||
|
||
@JsonProperty | ||
private String getType() { | ||
return type; | ||
} | ||
|
||
@JsonAnyGetter | ||
private Map<String, Object> getValue() { | ||
return value; | ||
} | ||
|
||
@JsonAnySetter | ||
private void put(String key, Object val) { | ||
value.put(key, val); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return this == other | ||
|| (other instanceof UnknownWrapper && equalTo((UnknownWrapper) other)); | ||
} | ||
|
||
private boolean equalTo(UnknownWrapper other) { | ||
return this.type.equals(other.type) && this.value.equals(other.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(type, value); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new StringBuilder("UnknownWrapper") | ||
.append("{") | ||
.append("type") | ||
.append(": ") | ||
.append(type) | ||
.append(", ") | ||
.append("value") | ||
.append(": ") | ||
.append(value) | ||
.append("}") | ||
.toString(); | ||
} | ||
} | ||
} |
Oops, something went wrong.