Skip to content

Commit

Permalink
generate defensive name for inner union class (#34)
Browse files Browse the repository at this point in the history
* generate defensive name for inner union class

* add test case

* rename inner class

* single underscore
  • Loading branch information
ferozco authored Jul 9, 2018
1 parent 37e1224 commit 4c49d22
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
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.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
import javax.annotation.Generated;

@Generated("com.palantir.conjure.java.types.UnionGenerator")
public final class Union {
@JsonUnwrapped private final Union_ union;

@JsonCreator
private Union(Union_ union) {
Objects.requireNonNull(union, "union must not be null");
this.union = union;
}

public <T> T accept(Visitor<T> visitor) {
if (union.getFoo().isPresent()) {
return visitor.visitFoo(union.getFoo().get());
} else if (union.getBar().isPresent()) {
return visitor.visitBar(union.getBar().getAsInt());
}
return visitor.visitUnknown(union.getType());
}

@Override
public boolean equals(Object other) {
return this == other || (other instanceof Union && equalTo((Union) other));
}

private boolean equalTo(Union other) {
return this.union.equals(other.union);
}

@Override
public int hashCode() {
return union.hashCode();
}

@Override
public String toString() {
return union.toString();
}

public static Union foo(String value) {
return new Union(Union_.builder().type("foo").foo(value).build());
}

public static Union bar(int value) {
return new Union(Union_.builder().type("bar").bar(value).build());
}

public interface Visitor<T> {
T visitFoo(String value);

T visitBar(int value);

T visitUnknown(String unknownType);
}

@JsonDeserialize(builder = Union_.Builder.class)
@Generated("com.palantir.conjure.java.types.BeanGenerator")
@JsonInclude(JsonInclude.Include.NON_ABSENT)
private static final class Union_ {
private final String type;

private final Optional<String> foo;

private final OptionalInt bar;

private final Map<String, Object> __unknownProperties;

private volatile int memoizedHashCode;

private Union_(
String type,
Optional<String> foo,
OptionalInt bar,
Map<String, Object> __unknownProperties) {
validateFields(type, foo, bar);
this.type = type;
this.foo = foo;
this.bar = bar;
this.__unknownProperties = Collections.unmodifiableMap(__unknownProperties);
}

@JsonProperty("type")
public String getType() {
return this.type;
}

@JsonProperty("foo")
public Optional<String> getFoo() {
return this.foo;
}

@JsonProperty("bar")
public OptionalInt getBar() {
return this.bar;
}

@JsonAnyGetter
Map<String, Object> unknownProperties() {
return __unknownProperties;
}

@Override
public boolean equals(Object other) {
return this == other || (other instanceof Union_ && equalTo((Union_) other));
}

private boolean equalTo(Union_ other) {
return this.type.equals(other.type)
&& this.foo.equals(other.foo)
&& this.bar.equals(other.bar)
&& this.__unknownProperties.equals(other.__unknownProperties);
}

@Override
public int hashCode() {
if (memoizedHashCode == 0) {
memoizedHashCode = Objects.hash(type, foo, bar, __unknownProperties);
}
return memoizedHashCode;
}

@Override
public String toString() {
return new StringBuilder("Union_")
.append("{")
.append("type")
.append(": ")
.append(type)
.append(", ")
.append("foo")
.append(": ")
.append(foo)
.append(", ")
.append("bar")
.append(": ")
.append(bar)
.append("}")
.toString();
}

private static void validateFields(String type, Optional<String> foo, OptionalInt bar) {
List<String> missingFields = null;
missingFields = addFieldIfMissing(missingFields, type, "type");
missingFields = addFieldIfMissing(missingFields, foo, "foo");
missingFields = addFieldIfMissing(missingFields, bar, "bar");
if (missingFields != null) {
throw new IllegalArgumentException(
"Some required fields have not been set: " + missingFields);
}
}

private static List<String> addFieldIfMissing(
List<String> prev, Object fieldValue, String fieldName) {
List<String> missingFields = prev;
if (fieldValue == null) {
if (missingFields == null) {
missingFields = new ArrayList<>(3);
}
missingFields.add(fieldName);
}
return missingFields;
}

public static Builder builder() {
return new Builder();
}

@Generated("com.palantir.conjure.java.types.BeanBuilderGenerator")
@JsonIgnoreProperties(ignoreUnknown = true)
public static final class Builder {
private String type;

private Optional<String> foo = Optional.empty();

private OptionalInt bar = OptionalInt.empty();

Map<String, Object> __unknownProperties = new LinkedHashMap<>();

private Builder() {}

public Builder from(Union_ other) {
type(other.getType());
foo(other.getFoo());
bar(other.getBar());
return this;
}

@JsonSetter("type")
public Builder type(String type) {
this.type = Objects.requireNonNull(type, "type cannot be null");
return this;
}

@JsonSetter("foo")
public Builder foo(Optional<String> foo) {
this.foo = Objects.requireNonNull(foo, "foo cannot be null");
return this;
}

public Builder foo(String foo) {
this.foo = Optional.of(Objects.requireNonNull(foo, "foo cannot be null"));
return this;
}

@JsonSetter("bar")
public Builder bar(OptionalInt bar) {
this.bar = Objects.requireNonNull(bar, "bar cannot be null");
return this;
}

public Builder bar(int bar) {
this.bar = OptionalInt.of(bar);
return this;
}

public Union_ build() {
return new Union_(type, foo, bar, __unknownProperties);
}

@JsonAnySetter
private void setUnknownProperties(String key, Object value) {
__unknownProperties.put(key, value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
/** A type which can either be a StringExample, a set of strings, or an integer. */
@Generated("com.palantir.conjure.java.types.UnionGenerator")
public final class UnionTypeExample {
@JsonUnwrapped private final Union union;
@JsonUnwrapped private final Union_ union;

@JsonCreator
private UnionTypeExample(Union union) {
private UnionTypeExample(Union_ union) {
Objects.requireNonNull(union, "union must not be null");
this.union = union;
}
Expand Down Expand Up @@ -73,33 +73,33 @@ public String toString() {
/** Docs for when UnionTypeExample is of type StringExample. */
public static UnionTypeExample stringExample(StringExample value) {
return new UnionTypeExample(
Union.builder().type("stringExample").stringExample(value).build());
Union_.builder().type("stringExample").stringExample(value).build());
}

public static UnionTypeExample set(Set<String> value) {
return new UnionTypeExample(Union.builder().type("set").set(value).build());
return new UnionTypeExample(Union_.builder().type("set").set(value).build());
}

public static UnionTypeExample thisFieldIsAnInteger(int value) {
return new UnionTypeExample(
Union.builder().type("thisFieldIsAnInteger").thisFieldIsAnInteger(value).build());
Union_.builder().type("thisFieldIsAnInteger").thisFieldIsAnInteger(value).build());
}

public static UnionTypeExample alsoAnInteger(int value) {
return new UnionTypeExample(
Union.builder().type("alsoAnInteger").alsoAnInteger(value).build());
Union_.builder().type("alsoAnInteger").alsoAnInteger(value).build());
}

public static UnionTypeExample if_(int value) {
return new UnionTypeExample(Union.builder().type("if").if_(value).build());
return new UnionTypeExample(Union_.builder().type("if").if_(value).build());
}

public static UnionTypeExample new_(int value) {
return new UnionTypeExample(Union.builder().type("new").new_(value).build());
return new UnionTypeExample(Union_.builder().type("new").new_(value).build());
}

public static UnionTypeExample interface_(int value) {
return new UnionTypeExample(Union.builder().type("interface").interface_(value).build());
return new UnionTypeExample(Union_.builder().type("interface").interface_(value).build());
}

public interface Visitor<T> {
Expand All @@ -120,10 +120,10 @@ public interface Visitor<T> {
T visitUnknown(String unknownType);
}

@JsonDeserialize(builder = Union.Builder.class)
@JsonDeserialize(builder = Union_.Builder.class)
@Generated("com.palantir.conjure.java.types.BeanGenerator")
@JsonInclude(JsonInclude.Include.NON_ABSENT)
private static final class Union {
private static final class Union_ {
private final String type;

private final Optional<StringExample> stringExample;
Expand All @@ -144,7 +144,7 @@ private static final class Union {

private volatile int memoizedHashCode;

private Union(
private Union_(
String type,
Optional<StringExample> stringExample,
Optional<Set<String>> set,
Expand Down Expand Up @@ -221,10 +221,10 @@ Map<String, Object> unknownProperties() {

@Override
public boolean equals(Object other) {
return this == other || (other instanceof Union && equalTo((Union) other));
return this == other || (other instanceof Union_ && equalTo((Union_) other));
}

private boolean equalTo(Union other) {
private boolean equalTo(Union_ other) {
return this.type.equals(other.type)
&& this.stringExample.equals(other.stringExample)
&& this.set.equals(other.set)
Expand Down Expand Up @@ -256,7 +256,7 @@ public int hashCode() {

@Override
public String toString() {
return new StringBuilder("Union")
return new StringBuilder("Union_")
.append("{")
.append("type")
.append(": ")
Expand Down Expand Up @@ -357,7 +357,7 @@ public static final class Builder {

private Builder() {}

public Builder from(Union other) {
public Builder from(Union_ other) {
type(other.getType());
stringExample(other.getStringExample());
set(other.getSet());
Expand Down Expand Up @@ -459,8 +459,8 @@ public Builder interface_(int interface_) {
return this;
}

public Union build() {
return new Union(
public Union_ build() {
return new Union_(
type,
stringExample,
set,
Expand Down
Loading

0 comments on commit 4c49d22

Please sign in to comment.