Skip to content

Commit

Permalink
issue-27 - Add support to float (#31)
Browse files Browse the repository at this point in the history
* issue-27 - Add support to float

* issue-27 - Correction PR for Codacy
  • Loading branch information
tfdsimoes authored Nov 15, 2023
1 parent c71921d commit 3fdd66f
Showing 1 changed file with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.jetbrains.annotations.NotNull;

Expand All @@ -70,33 +69,35 @@
public class PactDslProcessor extends AbstractProcessor {

static final Map<String, TypeMapping> TYPE_MAPPING = ImmutableMap.<String, TypeMapping>builder()
.put("int", new IntegerMapping())
.put("java.lang.Integer", new IntegerMapping())
.put("Integer", new IntegerMapping())
.put("short", new ShortMapping())
.put("java.lang.Short", new ShortMapping())
.put("Short", new ShortMapping())
.put("byte", new ByteMapping())
.put("long", new LongMapping())
.put("java.lang.Long", new LongMapping())
.put("Long", new LongMapping())
.put("char", new CharMapping())
.put("java.lang.String", new StringMapping())
.put("String", new StringMapping())
.put("double", new DecimalMapping())
.put("java.lang.Double", new DecimalMapping())
.put("Double", new DecimalMapping())
.put("java.math.BigDecimal", new DecimalMapping())
.put("BigDecimal", new DecimalMapping())
.put("boolean", new BooleanMapping())
.put("Boolean", new BooleanMapping())
.put("java.lang.Boolean", new BooleanMapping())
.put("date", new DateMapping())
.put("java.time.ZonedDateTime", new ZonedDateTimeMapping())
.put("ZonedDateTime", new ZonedDateTimeMapping())
.put("java.util.Date", new DateMapping())
.put("Date", new DateMapping())
.build();
.put("int", new IntegerMapping())
.put("java.lang.Integer", new IntegerMapping())
.put("Integer", new IntegerMapping())
.put("short", new ShortMapping())
.put("java.lang.Short", new ShortMapping())
.put("Short", new ShortMapping())
.put("byte", new ByteMapping())
.put("long", new LongMapping())
.put("java.lang.Long", new LongMapping())
.put("Long", new LongMapping())
.put("char", new CharMapping())
.put("java.lang.String", new StringMapping())
.put("String", new StringMapping())
.put("float", new DecimalMapping())
.put("Float", new DecimalMapping())
.put("double", new DecimalMapping())
.put("java.lang.Double", new DecimalMapping())
.put("Double", new DecimalMapping())
.put("java.math.BigDecimal", new DecimalMapping())
.put("BigDecimal", new DecimalMapping())
.put("boolean", new BooleanMapping())
.put("Boolean", new BooleanMapping())
.put("java.lang.Boolean", new BooleanMapping())
.put("date", new DateMapping())
.put("java.time.ZonedDateTime", new ZonedDateTimeMapping())
.put("ZonedDateTime", new ZonedDateTimeMapping())
.put("java.util.Date", new DateMapping())
.put("Date", new DateMapping())
.build();

private static final String CUSTOM_MODIFIERS = "customModifiers";

Expand Down Expand Up @@ -314,15 +315,15 @@ private String getNameOrNull(final Name simpleName) {
private static Object getDefaultValue(final Element fieldElement, final String type) {
final Object realValue;
final String value = fieldElement.getAnnotation(Example.class).value();
if (StringUtils.isNumeric(value)) {
if (NumberUtils.isCreatable(value)) {
realValue = switch (type.toLowerCase()) {
case "integer", "int" -> NumberUtils.toInt(value);
case "long" -> NumberUtils.toLong(value);
case "short" -> NumberUtils.toShort(value);
case "byte" -> NumberUtils.toByte(value);
case "float" -> NumberUtils.toFloat(value);
case "double" -> NumberUtils.toDouble(value);
case "bigdecimal" -> NumberUtils.createNumber(value);
case "bigdecimal", "java.math.bigdecimal" -> NumberUtils.createNumber(value);
default -> throw new IllegalStateException("Unexpected value: " + type);
};
} else {
Expand Down

0 comments on commit 3fdd66f

Please sign in to comment.