Skip to content

Commit

Permalink
Handle lists with mix of Ints & Longs by making them a List<Long> fix…
Browse files Browse the repository at this point in the history
…es issue #18
  • Loading branch information
superfell committed Oct 31, 2015
1 parent 352c97f commit 8609119
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/ApexPrimitive.java
Expand Up @@ -26,6 +26,10 @@ public int hashCode() {
return type.hashCode() + 1;
}

boolean canBePromotedTo(ApexPrimitive other) {
return (this.equals(INT)) && (other.equals(LONG));
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
Expand Down
8 changes: 8 additions & 0 deletions app/models/TypeFactory.java
Expand Up @@ -94,6 +94,14 @@ ApexType typeOfCollection(String propertyName, Collection<?> col) {
apexClass.mergeFields(thisApexClass);

classes.remove(thisApexClass.toString());
} else if (itemType instanceof ApexPrimitive && thisItemType instanceof ApexPrimitive) {
ApexPrimitive a = (ApexPrimitive)itemType;
ApexPrimitive b = (ApexPrimitive)thisItemType;
if (a.canBePromotedTo(b)) {
itemType = b;
} else if (b.canBePromotedTo(a)) {
continue;
}
} else {
throw new RuntimeException("Can't add an " + o.getClass() + " to a collection of " + itemType.getClass());
}
Expand Down
26 changes: 26 additions & 0 deletions test/models/ListsTest.java
Expand Up @@ -22,5 +22,31 @@ public void testArrayPrimitives() {
assertEquals("ApexList ItemType is wrong", ApexPrimitive.INT, al.itemType);
}

@Test
public void testArrayPromotionIntToLong() {
List<Object> l = new ArrayList<Object>();
l.add(1);
l.add(3L);
TypeFactory factory = new TypeFactory();
ApexType root = factory.typeOfObject("Root", l);
if (!(root instanceof ApexList)) {
fail("Created ApexType should be a list, but is " + root);
}
ApexList al = (ApexList)root;
assertEquals("ApexList ItemType is wrong", ApexPrimitive.LONG, al.itemType);
}

@Test
public void testArrayPromotionLongToInt() {
List<Object> l = new ArrayList<Object>();
l.add(3L);
l.add(1);
TypeFactory factory = new TypeFactory();
ApexType root = factory.typeOfObject("Root", l);
if (!(root instanceof ApexList)) {
fail("Created ApexType should be a list, but is " + root);
}
ApexList al = (ApexList)root;
assertEquals("ApexList ItemType is wrong", ApexPrimitive.LONG, al.itemType);
}
}

0 comments on commit 8609119

Please sign in to comment.