Skip to content

Commit

Permalink
Fix #1247
Browse files Browse the repository at this point in the history
  • Loading branch information
zero88 committed Oct 28, 2022
1 parent 06dd613 commit e051c86
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 169 deletions.
234 changes: 65 additions & 169 deletions vertx-sql-client/src/main/java/io/vertx/sqlclient/Tuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ static Tuple tuple() {
*
* @return the list wrapped as a tuple
*/
@SuppressWarnings("unchecked")
static <T> Tuple from(List<T> list) {
return wrap(new ArrayList<>(list));
}
Expand All @@ -88,10 +87,8 @@ static <T> Tuple from(List<T> list) {
*/
@GenIgnore(GenIgnore.PERMITTED_TYPE)
static Tuple from(Object[] array) {
ArrayList<Object> list = new ArrayList<>(array.length);
for (Object o : array) {
list.add(o);
}
List<Object> list = new ArrayList<>(array.length);
Collections.addAll(list, array);
return wrap(list);
}

Expand Down Expand Up @@ -205,7 +202,7 @@ static Tuple of(Object elt1, Object elt2, Object elt3, Object elt4, Object elt5)
* Create a tuple of six elements.
*
* @param elt1 the first value
* @param elt2 the second valueg
* @param elt2 the second value
* @param elt3 the third value
* @param elt4 the fourth value
* @param elt5 the fifth value
Expand Down Expand Up @@ -657,20 +654,12 @@ default BigDecimal getBigDecimal(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Boolean[] getArrayOfBooleans(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof Boolean[]) {
return (Boolean[]) val;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
Boolean[] booleanArray = new Boolean[array.length];
for (int i = 0;i < array.length;i++) {
booleanArray[i] = (Boolean) array[i];
}
return booleanArray;
} else {
return (Boolean[]) val; // Throw CCE
if (val == null) return null;
if (val instanceof Boolean[]) return (Boolean[]) val;
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val)).mapToObj(i -> Array.get(val, i)).map(value -> ((Boolean) value)).toArray(Boolean[]::new);
}
throw new ClassCastException("Invalid Boolean array value type " + val.getClass());
}

/**
Expand All @@ -685,22 +674,15 @@ default Boolean[] getArrayOfBooleans(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Short[] getArrayOfShorts(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof Short[]) {
return (Short[]) val;
} else if (val instanceof Number[]) {
Number[] a = (Number[]) val;
int len = a.length;
Short[] arr = new Short[len];
for (int i = 0; i < len; i++) {
Number elt = a[i];
if (elt != null) {
arr[i] = elt.shortValue();
}
}
return arr;
} else if (val instanceof Enum[]) {
if (val == null) return null;
if (val instanceof Short[]) return (Short[]) val;
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val))
.mapToObj(i -> Array.get(val, i))
.map(value -> value == null ? null : ((Number)value).shortValue())
.toArray(Short[]::new);
}
if (val instanceof Enum[]) {
Enum<?>[] a = (Enum<?>[]) val;
int len = a.length;
Short[] arr = new Short[len];
Expand All @@ -711,16 +693,8 @@ default Short[] getArrayOfShorts(int pos) {
}
}
return arr;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
Short[] shortArray = new Short[array.length];
for (int i = 0;i < array.length;i++) {
shortArray[i] = ((Number) array[i]).shortValue();
}
return shortArray;
} else {
return (Short[]) val; // Throw CCE
}
throw new ClassCastException("Invalid Short array value type " + val.getClass());
}

/**
Expand All @@ -735,22 +709,15 @@ default Short[] getArrayOfShorts(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Integer[] getArrayOfIntegers(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof Integer[]) {
return (Integer[]) val;
} else if (val instanceof Number[]) {
Number[] a = (Number[]) val;
int len = a.length;
Integer[] arr = new Integer[len];
for (int i = 0; i < len; i++) {
Number elt = a[i];
if (elt != null) {
arr[i] = elt.intValue();
}
}
return arr;
} else if (val instanceof Enum[]) {
if (val == null) return null;
if (val instanceof Integer[]) return (Integer[]) val;
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val))
.mapToObj(i -> Array.get(val, i))
.map(value -> value == null ? null : ((Number)value).intValue())
.toArray(Integer[]::new);
}
if (val instanceof Enum[]) {
Enum<?>[] a = (Enum<?>[]) val;
int len = a.length;
Integer[] arr = new Integer[len];
Expand All @@ -761,16 +728,8 @@ default Integer[] getArrayOfIntegers(int pos) {
}
}
return arr;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
Integer[] integerArray = new Integer[array.length];
for (int i = 0;i < array.length;i++) {
integerArray[i] = ((Number) array[i]).intValue();
}
return integerArray;
} else {
return (Integer[]) val; // Throw CCE
}
throw new ClassCastException("Invalid Integer array value type " + val.getClass());
}

/**
Expand All @@ -785,22 +744,15 @@ default Integer[] getArrayOfIntegers(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Long[] getArrayOfLongs(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof Long[]) {
return (Long[]) val;
} else if (val instanceof Number[]) {
Number[] a = (Number[]) val;
int len = a.length;
Long[] arr = new Long[len];
for (int i = 0; i < len; i++) {
Number elt = a[i];
if (elt != null) {
arr[i] = elt.longValue();
}
}
return arr;
} else if (val instanceof Enum[]) {
if (val == null) return null;
if (val instanceof Long[]) return (Long[]) val;
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val))
.mapToObj(i -> Array.get(val, i))
.map(value -> value == null ? null : ((Number)value).longValue())
.toArray(Long[]::new);
}
if (val instanceof Enum[]) {
Enum<?>[] a = (Enum<?>[]) val;
int len = a.length;
Long[] arr = new Long[len];
Expand All @@ -811,16 +763,8 @@ default Long[] getArrayOfLongs(int pos) {
}
}
return arr;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
Long[] longArray = new Long[array.length];
for (int i = 0;i < array.length;i++) {
longArray[i] = ((Number) array[i]).longValue();
}
return longArray;
} else {
return (Long[]) val; // Throw CCE
}
throw new ClassCastException("Invalid Long array value type " + val.getClass());
}

/**
Expand All @@ -835,22 +779,15 @@ default Long[] getArrayOfLongs(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Float[] getArrayOfFloats(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof Float[]) {
return (Float[]) val;
} else if (val instanceof Number[]) {
Number[] a = (Number[]) val;
int len = a.length;
Float[] arr = new Float[len];
for (int i = 0; i < len; i++) {
Number elt = a[i];
if (elt != null) {
arr[i] = elt.floatValue();
}
}
return arr;
} else if (val instanceof Enum[]) {
if (val == null) return null;
if (val instanceof Float[]) return (Float[]) val;
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val))
.mapToObj(i -> Array.get(val, i))
.map(value -> value == null ? null : ((Number)value).floatValue())
.toArray(Float[]::new);
}
if (val instanceof Enum[]) {
Enum<?>[] a = (Enum<?>[]) val;
int len = a.length;
Float[] arr = new Float[len];
Expand All @@ -861,16 +798,8 @@ default Float[] getArrayOfFloats(int pos) {
}
}
return arr;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
Float[] floatArray = new Float[array.length];
for (int i = 0;i < array.length;i++) {
floatArray[i] = ((Number) array[i]).floatValue();
}
return floatArray;
} else {
return (Float[]) val; // Throw CCE
}
throw new ClassCastException("Invalid Float array value type " + val.getClass());
}

/**
Expand All @@ -885,22 +814,15 @@ default Float[] getArrayOfFloats(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Double[] getArrayOfDoubles(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof Double[]) {
return (Double[]) val;
} else if (val instanceof Number[]) {
Number[] a = (Number[]) val;
int len = a.length;
Double[] arr = new Double[len];
for (int i = 0; i < len; i++) {
Number elt = a[i];
if (elt != null) {
arr[i] = elt.doubleValue();
}
}
return arr;
} else if (val instanceof Enum[]) {
if (val == null) return null;
if (val instanceof Double[]) return (Double[]) val;
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val))
.mapToObj(i -> Array.get(val, i))
.map(value -> value == null ? null : ((Number)value).doubleValue())
.toArray(Double[]::new);
}
if (val instanceof Enum[]) {
Enum<?>[] a = (Enum<?>[]) val;
int len = a.length;
Double[] arr = new Double[len];
Expand All @@ -911,16 +833,8 @@ default Double[] getArrayOfDoubles(int pos) {
}
}
return arr;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
Double[] doubleArray = new Double[array.length];
for (int i = 0;i < array.length;i++) {
doubleArray[i] = ((Number) array[i]).doubleValue();
}
return doubleArray;
} else {
return (Double[]) val; // Throw CCE
}
throw new ClassCastException("Invalid Double array value type " + val.getClass());
}

/**
Expand All @@ -932,22 +846,12 @@ default Double[] getArrayOfDoubles(int pos) {
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default Numeric[] getArrayOfNumerics(int pos) {
Object val = getValue(pos);
if (val == null) {
return null;
} else if (val instanceof Numeric[]) {
return (Numeric[]) val;
} else if (val instanceof Number[]) {
Number[] a = (Number[]) val;
int len = a.length;
Numeric[] arr = new Numeric[len];
for (int i = 0; i < len; i++) {
Number elt = a[i];
if (elt != null) {
arr[i] = Numeric.create(elt);
}
}
return arr;
} else if (val instanceof Enum[]) {
if (val == null) return null;
if (val instanceof Numeric[]) return (Numeric[]) val;
if (val.getClass().isArray()) {
return IntStream.range(0, Array.getLength(val)).mapToObj(i -> Array.get(val, i)).map(value -> Numeric.create((Number)value)).toArray(Numeric[]::new);
}
if (val instanceof Enum[]) {
Enum<?>[] a = (Enum<?>[]) val;
int len = a.length;
Numeric[] arr = new Numeric[len];
Expand All @@ -958,16 +862,8 @@ default Numeric[] getArrayOfNumerics(int pos) {
}
}
return arr;
} else if (val.getClass() == Object[].class) {
Object[] array = (Object[]) val;
Numeric[] doubleArray = new Numeric[array.length];
for (int i = 0;i < array.length;i++) {
doubleArray[i] = Numeric.create((Number) array[i]);
}
return doubleArray;
} else {
throw new ClassCastException();
}
throw new ClassCastException("Invalid Numeric array value type " + val.getClass());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,15 @@ public void testLongArrayCoercion() {
assertNull(res[1]);
}

@Test
public void testPrimitiveLongArray() {
Tuple tuple = of((Object)new long[]{5, 2});
Long[] res = tuple.getArrayOfLongs(0);
assertEquals(2, res.length);
assertEquals(5, (long)res[0]);
assertEquals(2, (long)res[1]);
}

@Test
public void testFloatArrayCoercion() {
Tuple tuple = of((Object)new Double[]{5D, null, Double.MAX_VALUE});
Expand Down Expand Up @@ -475,6 +484,7 @@ public void testJsonArray() {
public void testGetJsonArrayFromArray() {
Tuple tuple = of((Object) new int[]{1, 2, 3});
assertEquals(JsonArray.of(1, 2, 3), tuple.getJsonArray(0));
assertArrayEquals(new Integer[]{1,2,3}, tuple.getArrayOfIntegers(0));
}

@Test
Expand Down

0 comments on commit e051c86

Please sign in to comment.