Skip to content

Commit

Permalink
[#94] Add array padding option (#95)
Browse files Browse the repository at this point in the history
Reviewed by @chanhyeong 

* [#94] Add array padding option

* 포매팅 수정 및 누락 변경 반영
  • Loading branch information
IAM20 authored May 17, 2023
1 parent cd12ca1 commit c814ff6
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ConvertibleSqlIdentifierParameterSource extends SqlIdentifierParameterSour
private final JdbcParameterSourceConverter converter;
private final FallbackParameterSource fallbackParameterSource;

private boolean padArray = true;
private boolean paddingIterableParams = false;
private int[] paddingIterableBoundaries = null;

Expand Down Expand Up @@ -72,7 +73,11 @@ public Object getValue(String paramName) throws IllegalArgumentException {

value = this.converter.convert(paramName, value);
if (this.paddingIterableParams) {
value = IterableExpandPadding.expandIfIterable(value, this.paddingIterableBoundaries);
value = IterableExpandPadding.expandIfIterable(
value,
this.padArray,
this.paddingIterableBoundaries
);
}

return value;
Expand All @@ -96,6 +101,15 @@ public void setPaddingIterableParam(boolean padding) {
this.paddingIterableParams = padding;
}

/**
* Sets pad array.
*
* @param padArray the pad array y/n
*/
public void setPadArray(boolean padArray) {
this.padArray = padArray;
}

private boolean isFallback(String paramName) {
return this.fallbackParameterSource != null && this.fallbackParameterSource.isFallback(paramName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class ConvertibleBeanPropertySqlParameterSource extends BeanPropertySqlPa
private final FallbackParameterSource fallbackParameterSource;
private final String prefix;

private boolean padArray = true;

private boolean paddingIterableParams = false;
private int[] paddingIterableBoundaries = null;

Expand Down Expand Up @@ -135,7 +137,11 @@ public Object getValue(String paramName) {

value = this.converter.convert(patchedParamName, value);
if (this.paddingIterableParams) {
value = IterableExpandPadding.expandIfIterable(value, this.paddingIterableBoundaries);
value = IterableExpandPadding.expandIfIterable(
value,
padArray,
this.paddingIterableBoundaries
);
}

return value;
Expand All @@ -159,6 +165,16 @@ public void setPaddingIterableParam(boolean padding) {
this.paddingIterableParams = padding;
}


/**
* Sets pad array.
*
* @param padArray the pad array y/n
*/
public void setPadArray(boolean padArray) {
this.padArray = padArray;
}

private boolean isFallback(String paramName) {
return this.fallbackParameterSource != null && this.fallbackParameterSource.isFallback(paramName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
* The type Convertible map sql parameter source.
*
* @author Myeonghyeon Lee
* @author IAM20
*/
public class ConvertibleMapSqlParameterSource extends MapSqlParameterSource {
private final JdbcParameterSourceConverter converter;
private final FallbackParameterSource fallbackParameterSource;

private boolean padArray = true;
private boolean paddingIterableParams = false;
private int[] paddingIterableBoundaries = null;

Expand Down Expand Up @@ -85,7 +87,11 @@ public Object getValue(String paramName) {

value = this.converter.convert(paramName, value);
if (this.paddingIterableParams) {
value = IterableExpandPadding.expandIfIterable(value, this.paddingIterableBoundaries);
value = IterableExpandPadding.expandIfIterable(
value,
this.padArray,
this.paddingIterableBoundaries
);
}

return value;
Expand All @@ -100,6 +106,15 @@ public void setPaddingIterableBoundaries(int[] setPaddingIterableBoundaries) {
this.paddingIterableBoundaries = setPaddingIterableBoundaries;
}

/**
* Sets pad array.
*
* @param padArray the pad array y/n
*/
public void setPadArray(boolean padArray) {
this.padArray = padArray;
}

/**
* Sets padding iterable param.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class ConvertibleParameterSourceFactory {
private final JdbcParameterSourceConverter converter;
private final FallbackParameterSource fallbackParameterSource;

private boolean padArray = true;
private boolean paddingIterableParams = false;
private int[] paddingIterableBoundaries = null;

Expand Down Expand Up @@ -71,6 +72,7 @@ public BeanPropertySqlParameterSource beanParameterSource(Object bean) {
bean, this.converter, this.fallbackParameterSource);
paramSource.setPaddingIterableParam(this.paddingIterableParams);
paramSource.setPaddingIterableBoundaries(this.paddingIterableBoundaries);
paramSource.setPadArray(this.padArray);
return paramSource;
}

Expand All @@ -87,6 +89,7 @@ public BeanPropertySqlParameterSource beanParameterSource(String prefix, Object
prefix, bean, this.converter, this.fallbackParameterSource);
paramSource.setPaddingIterableParam(this.paddingIterableParams);
paramSource.setPaddingIterableBoundaries(this.paddingIterableBoundaries);
paramSource.setPadArray(this.padArray);
return paramSource;
}

Expand All @@ -101,6 +104,7 @@ public MapSqlParameterSource mapParameterSource(Map<String, ?> map) {
new ConvertibleMapSqlParameterSource(map, this.converter, this.fallbackParameterSource);
paramSource.setPaddingIterableParam(this.paddingIterableParams);
paramSource.setPaddingIterableBoundaries(this.paddingIterableBoundaries);
paramSource.setPadArray(this.padArray);
return paramSource;
}

Expand Down Expand Up @@ -140,6 +144,15 @@ public boolean isPaddingIterableParams() {
return this.paddingIterableParams;
}

/**
* Sets pad array.
*
* @param padArray the pad array y/n
*/
public void setPadArray(boolean padArray) {
this.padArray = padArray;
}

/**
* Gets converter.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* The type Iterable expand padding.
*
* @author Myeonghyeon Lee
* @author IAM20
*/
public class IterableExpandPadding {
private static final int[] REGULAR_SIZES = {0, 1, 2, 3, 4, 8, 16, 32, 50, 100, 200, 300, 500, 1000, 1500, 2000};
Expand All @@ -39,9 +40,22 @@ public class IterableExpandPadding {
*
* @param source the source
* @return the object
* @deprecated use {@link #expandIfIterable(Object, boolean)} instead
*/
@Deprecated(forRemoval = true)
public static Object expandIfIterable(Object source) {
return expandIfIterable(source, REGULAR_SIZES);
return expandIfIterable(source, true, REGULAR_SIZES);
}

/**
* Expand if iterable object.
*
* @param source the source
* @param padArray the pad array y/n
* @return the object
*/
public static Object expandIfIterable(Object source, boolean padArray) {
return expandIfIterable(source, padArray, REGULAR_SIZES);
}

/**
Expand All @@ -50,8 +64,22 @@ public static Object expandIfIterable(Object source) {
* @param source the source
* @param paddingBoundaries the padding boundaries
* @return the object
* @deprecated Use {@link #expandIfIterable(Object, boolean, int[])} instead
*/
@Deprecated(forRemoval = true)
public static Object expandIfIterable(Object source, @Nullable int[] paddingBoundaries) {
return expandIfIterable(source, true, paddingBoundaries);
}

/**
* Expand if iterable object.
*
* @param source the source
* @param padArray the pad array y/n
* @param paddingBoundaries the padding boundaries
* @return the object
*/
public static Object expandIfIterable(Object source, boolean padArray, @Nullable int[] paddingBoundaries) {
if (source == null) {
return null;
}
Expand All @@ -62,7 +90,7 @@ public static Object expandIfIterable(Object source, @Nullable int[] paddingBoun

if (source instanceof Collection) {
return CollectionExpandPadding.INSTANCE.expand((Collection<?>)source, paddingBoundaries);
} else if (source.getClass().isArray()) {
} else if (source.getClass().isArray() && padArray) {
return ArrayExpandPadding.INSTANCE.expand((Object[])source, paddingBoundaries);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.javaunit.autoparams.AutoSource;
Expand Down Expand Up @@ -227,6 +226,7 @@ void getValueIterablePadding(String name) {
ConvertibleBeanPropertySqlParameterSource sut =
new ConvertibleBeanPropertySqlParameterSource(criteria, this.converter);
sut.setPaddingIterableParam(true);
sut.setPadArray(true);

// when
Object actual = sut.getValue("list");
Expand Down Expand Up @@ -254,6 +254,7 @@ void getValueIterablePaddingBoundariesButFalse(String name) {
new ConvertibleBeanPropertySqlParameterSource(criteria, this.converter);
sut.setPaddingIterableBoundaries(new int[] {1, 10});
sut.setPaddingIterableParam(false);
sut.setPadArray(true);

// when
Object actual = sut.getValue("list");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -144,6 +143,7 @@ void getValueIterablePadding(String paramName) {
Map<String, Object> map = Collections.singletonMap(paramName, value);
ConvertibleMapSqlParameterSource sut = new ConvertibleMapSqlParameterSource(map, this.converter);
sut.setPaddingIterableParam(true);
sut.setPadArray(true);

// when
Object actual = sut.getValue(paramName);
Expand All @@ -160,6 +160,33 @@ void getValueIterablePadding(String paramName) {
InstantToTimestampConverter.INSTANCE.convert(value.get(4)));
}

@DisplayName("Array 는 expand padding flag 가 false 면 padding 을 수행하지 않는다.")
@ParameterizedTest
@AutoSource
@SuppressWarnings("unchecked")
void getValueIterablePaddingForArray(String paramName) {
// given
Instant now = Instant.now();
Instant[] value = {now.minusSeconds(300), now.minusSeconds(240),
now.minusSeconds(180), now.minusSeconds(120), now.minusSeconds(60)};
Map<String, Object> map = Collections.singletonMap(paramName, value);
ConvertibleMapSqlParameterSource sut = new ConvertibleMapSqlParameterSource(map, this.converter);
sut.setPaddingIterableParam(true);
sut.setPadArray(false);

// when
Object actual = sut.getValue(paramName);

// then
assertThat(actual).isInstanceOf(Object[].class);
Object[] array = (Object[])actual;
assertThat(array).hasSize(5);
assertThat(array[0]).isEqualTo(
InstantToTimestampConverter.INSTANCE.convert(value[0]));
assertThat(array[4]).isEqualTo(
InstantToTimestampConverter.INSTANCE.convert(value[4]));
}

@DisplayName("iterablePaddingBoundaries 를 주입하더라도 paddingIterableParam 이 false 면 padding 하지 않는다.")
@ParameterizedTest
@AutoSource
Expand All @@ -173,6 +200,7 @@ void getValueIterablePaddingBoundariesButFalse(String paramName) {
ConvertibleMapSqlParameterSource sut = new ConvertibleMapSqlParameterSource(map, this.converter);
sut.setPaddingIterableBoundaries(new int[] {1, 10});
sut.setPaddingIterableParam(false);
sut.setPadArray(true);

// when
Object actual = sut.getValue(paramName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public Object unwrap(JsonNode value) {
if (value instanceof JsonArray) {
return new JsonArray("conditional unwrapped " + value.value);
} else if (value instanceof JsonValue) {
return new JsonValue("conditional unwrapped " + value.value);
return new JsonValue("conditional unwrapped " + value.value);
} else {
return new JsonNode("conditional unwrapped " + value.value);
}
Expand Down
Loading

0 comments on commit c814ff6

Please sign in to comment.