Skip to content
This repository has been archived by the owner on Oct 15, 2019. It is now read-only.

Commit

Permalink
Revert adding Builder subclasses to unions
Browse files Browse the repository at this point in the history
Using builder subclasses in unions in Swift-generated code causes
separate errors to be triggered:

Error: Inject method com.facebook.swift.foo is not allowed on struct class, since struct has a builder
Attempted to resolve a recursive reference to type 'com.facebook.swift.foo' before the referenced type was cached (most likely a recursive type support bug)
  • Loading branch information
Ryan Wilson committed Oct 7, 2016
1 parent 58d06fd commit 732bfac
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 84 deletions.
Expand Up @@ -152,6 +152,22 @@ public void testUnionConstructor()

unionConstructor = new UnionConstructor(Fruit.APPLE);
testRoundTripSerialize(unionConstructor, new TCompactProtocol.Factory());

unionConstructor = new UnionConstructor();
testRoundTripSerialize(unionConstructor, new TCompactProtocol.Factory());
}

@Test
public void testUnionConstructorDuplicateTypes()
throws Exception
{
UnionConstructorDuplicateTypes unionConstructor = new UnionConstructorDuplicateTypes();
unionConstructor.setFirstIntValue(1);
testRoundTripSerialize(unionConstructor, new TCompactProtocol.Factory());

unionConstructor = new UnionConstructorDuplicateTypes();
unionConstructor.setSecondIntValue(2);
testRoundTripSerialize(unionConstructor, new TCompactProtocol.Factory());
}

@Test
Expand Down
Expand Up @@ -15,65 +15,78 @@
*/
package com.facebook.swift.codec;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Objects;
import java.util.Arrays;
import java.util.Objects;

import static com.google.common.base.MoreObjects.toStringHelper;

@ThriftUnion("Union")
public final class UnionConstructor
{
private final Object value;
private final short type;
private Object value;
private short id = -1;
private String name;

@ThriftConstructor
public UnionConstructor() {}

@ThriftConstructor
public UnionConstructor(String stringValue)
{
this.value = stringValue;
this.type = 1;
this.id = 1;
this.name = "stringValue";
}

@ThriftConstructor
public UnionConstructor(Long longValue)
{
this.value = longValue;
this.type = 2;
this.id = 2;
this.name = "longValue";
}

@ThriftConstructor
public UnionConstructor(Fruit fruitValue)
{
this.value = fruitValue;
this.type = 3;
this.id = 3;
this.name = "fruitValue";
}

@ThriftUnionId
public short getType()
public short getThriftId()
{
return this.id;
}

public String getThriftName()
{
return type;
return this.name;
}

@ThriftField(1)
@ThriftField(value = 1, name = "stringValue")
public String getStringValue()
{
if (type != 1) {
if (id != 1) {
throw new IllegalStateException("not a stringValue");
}
return (String) value;
}

@ThriftField(2)
@ThriftField(value = 2, name = "longValue")
public Long getLongValue()
{
if (type != 2) {
if (id != 2) {
throw new IllegalStateException("not a longValue");
}
return (Long) value;
}

@ThriftField(3)
@ThriftField(value = 3, name = "fruitValue")
public Fruit getFruitValue()
{
if (type != 3) {
if (id != 3) {
throw new IllegalStateException("not a fruitValue");
}
return (Fruit) value;
Expand All @@ -82,7 +95,11 @@ public Fruit getFruitValue()
@Override
public int hashCode()
{
return Objects.hashCode(value, type);
return Arrays.deepHashCode(new Object[] {
id,
value,
name
});
}

@Override
Expand All @@ -96,24 +113,19 @@ else if (obj == null || getClass() != obj.getClass()) {
}

UnionConstructor that = (UnionConstructor) obj;
return Objects.equal(this.type, that.type)
&& Objects.equal(this.value, that.value);
return Objects.equals(this.id, that.id)
&& Objects.equals(this.value, that.value)
&& Objects.equals(this.name, that.name);
}

@Override
public String toString()
{
ToStringHelper helper = MoreObjects.toStringHelper(this);

if (type == 1) {
helper.add("stringValue", (String) value);
}
else if (type == 2) {
helper.add("longValue", (Long) value);
}
else if (type == 3) {
helper.add("fruitValue", (Fruit) value);
}
return helper.toString();
return toStringHelper(this)
.add("value", value)
.add("id", id)
.add("name", name)
.add("type", value == null ? "<null>" : value.getClass().getSimpleName())
.toString();
}
}
@@ -0,0 +1,115 @@
/*
* Copyright (C) 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.facebook.swift.codec;

import com.facebook.swift.codec.ThriftField.Requiredness;

import java.util.Arrays;
import java.util.Objects;

import static com.google.common.base.MoreObjects.toStringHelper;

@ThriftUnion("UnionConstructorDuplicateTypes")
public class UnionConstructorDuplicateTypes {
private Object value;
private short id = -1;
private String name;

@ThriftConstructor
public UnionConstructorDuplicateTypes() {
}

@ThriftField
public void setFirstIntValue(final int firstIntValue) {
this.value = firstIntValue;
this.id = 1;
this.name = "firstIntValue";
}

@ThriftField
public void setSecondIntValue(final int secondIntValue) {
this.value = secondIntValue;
this.id = 2;
this.name = "secondIntValue";
}

@ThriftField(value = 1, name = "firstIntValue", requiredness = Requiredness.NONE)
public int getFirstIntValue() {
if (this.id != 1) {
throw new IllegalStateException("Not a firstIntValue element!");
}
return (int) value;
}

public boolean isSetFirstIntValue() {
return this.id == 1;
}

@ThriftField(value = 2, name = "secondIntValue", requiredness = Requiredness.NONE)
public int getSecondIntValue() {
if (this.id != 2) {
throw new IllegalStateException("Not a secondIntValue element!");
}
return (int) value;
}

public boolean isSetSecondIntValue() {
return this.id == 2;
}

@ThriftUnionId
public short getThriftId() {
return this.id;
}

public String getThriftName() {
return this.name;
}

@Override
public int hashCode()
{
return Arrays.deepHashCode(new Object[] {
id,
value,
name
});
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj == null || getClass() != obj.getClass()) {
return false;
}

UnionConstructorDuplicateTypes that = (UnionConstructorDuplicateTypes) obj;
return Objects.equals(this.id, that.id)
&& Objects.equals(this.value, that.value)
&& Objects.equals(this.name, that.name);
}

@Override
public String toString() {
return toStringHelper(this)
.add("value", value)
.add("id", id)
.add("name", name)
.add("type", value == null ? "<null>" : value.getClass().getSimpleName())
.toString();
}
}

0 comments on commit 732bfac

Please sign in to comment.