Skip to content

Commit

Permalink
Merge pull request #204 from croz-ltd/feature_addJavascriptArrayTypeT…
Browse files Browse the repository at this point in the history
…oFormConfigurationResponse

Return array as Javascript type for collections and arrays since yup …
  • Loading branch information
jzrilic authored Apr 3, 2024
2 parents 59c1eba + 6614afd commit 1e11b09
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
*/
public enum JavascriptType {

STRING, BOOLEAN, NUMBER, DATE, OBJECT
STRING, BOOLEAN, NUMBER, DATE, ARRAY, OBJECT

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.time.temporal.Temporal;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
Expand All @@ -42,6 +43,7 @@ public class DefaultJavaToJavascriptTypeConverter implements JavaToJavascriptTyp
CLASS_JAVASCRIPT_TYPE_MAP.put(Temporal.class, JavascriptType.DATE);
CLASS_JAVASCRIPT_TYPE_MAP.put(Number.class, JavascriptType.NUMBER);
CLASS_JAVASCRIPT_TYPE_MAP.put(Enum.class, JavascriptType.STRING);
CLASS_JAVASCRIPT_TYPE_MAP.put(Collection.class, JavascriptType.ARRAY);
}

@Override
Expand All @@ -51,11 +53,18 @@ public boolean supports(Class<?> type) {

@Override
public String convert(Class<?> type) {
return CLASS_JAVASCRIPT_TYPE_MAP.entrySet().stream()
.filter(entry -> entry.getKey().isAssignableFrom(type))
.findFirst()
.map(Map.Entry::getValue)
.orElse(JavascriptType.OBJECT)
.name().toLowerCase(Locale.ROOT);
JavascriptType javascriptType;
if (type.isArray()) {
javascriptType = JavascriptType.ARRAY;
}
else {
javascriptType = CLASS_JAVASCRIPT_TYPE_MAP.entrySet().stream()
.filter(entry -> entry.getKey().isAssignableFrom(type))
.findFirst()
.map(Map.Entry::getValue)
.orElse(JavascriptType.OBJECT);
}

return javascriptType.name().toLowerCase(Locale.ROOT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -71,7 +76,13 @@ private static Stream<Arguments> shouldConvertJavaToJavascriptTypeMethodSource()
arguments(java.sql.Date.class, JavascriptType.DATE),
arguments(Boolean.class, JavascriptType.BOOLEAN),
arguments(Object.class, JavascriptType.OBJECT),
arguments(TestEnum.class, JavascriptType.STRING)
arguments(TestEnum.class, JavascriptType.STRING),
arguments(Collection.class, JavascriptType.ARRAY),
arguments(List.class, JavascriptType.ARRAY),
arguments(Set.class, JavascriptType.ARRAY),
arguments(int[].class, JavascriptType.ARRAY),
arguments(Object[].class, JavascriptType.ARRAY),
arguments(Map.class, JavascriptType.OBJECT)
);
}
}

0 comments on commit 1e11b09

Please sign in to comment.