Skip to content

Commit

Permalink
Refactor basic types with several corrections (#48)
Browse files Browse the repository at this point in the history
* Refactor basic types with several corrections

* Corrections imports for codacy

* Corrections for codacy

* Corrections code in byte set in pactDslJsonBody

---------

Co-authored-by: Tiago Simoes <[email protected]>
  • Loading branch information
tfdsimoes and Tiago Simoes authored Dec 11, 2023
1 parent 1beb8a8 commit 310ca70
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 54 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.sngular</groupId>
<artifactId>pact-annotation-processor</artifactId>
<version>1.1.8</version>
<version>1.1.9</version>

<name>PactDslBuilder - Annotation Processor</name>
<description>Pact DSL Builder annotation processor.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
import com.sngular.annotation.pact.PactDslBodyBuilder;
import com.sngular.annotation.processor.exception.TemplateFactoryException;
import com.sngular.annotation.processor.exception.TemplateGenerationException;
import com.sngular.annotation.processor.mapping.BigDecimalMapping;
import com.sngular.annotation.processor.mapping.BigIntegerMapping;
import com.sngular.annotation.processor.mapping.BooleanMapping;
import com.sngular.annotation.processor.mapping.ByteMapping;
import com.sngular.annotation.processor.mapping.CharMapping;
import com.sngular.annotation.processor.mapping.DateMapping;
import com.sngular.annotation.processor.mapping.DecimalMapping;
import com.sngular.annotation.processor.mapping.DoubleMapping;
import com.sngular.annotation.processor.mapping.FloatMapping;
import com.sngular.annotation.processor.mapping.IntegerMapping;
import com.sngular.annotation.processor.mapping.LongMapping;
import com.sngular.annotation.processor.mapping.ShortMapping;
Expand Down Expand Up @@ -72,33 +74,24 @@ 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("java.math.BigInteger", new BigIntegerMapping())
.put("BigInteger", new BigIntegerMapping())
.put("biginteger", new BigIntegerMapping())
.put("short", new ShortMapping())
.put("java.lang.Short", new ShortMapping())
.put("Short", new ShortMapping())
.put("byte", new ByteMapping())
.put("Byte", new ByteMapping())
.put("long", new LongMapping())
.put("java.lang.Long", new LongMapping())
.put("Long", new LongMapping())
.put("char", new CharMapping())
.put("Character", new CharMapping())
.put("java.lang.Character", 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("float", new FloatMapping())
.put("Float", new FloatMapping())
.put("double", new DoubleMapping())
.put("Double", new DoubleMapping())
.put("BigDecimal", new BigDecimalMapping())
.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())
Expand Down Expand Up @@ -330,15 +323,15 @@ private static Object getDefaultValue(final Element fieldElement, final String t
final Object realValue;
final String value = fieldElement.getAnnotation(Example.class).value();
if (NumberUtils.isCreatable(value)) {
realValue = switch (type.toLowerCase()) {
case "integer", "int" -> NumberUtils.toInt(value);
case "biginteger" -> NumberUtils.createBigInteger(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", "java.math.bigdecimal" -> NumberUtils.createNumber(value);
realValue = switch (type) {
case "int", "Integer" -> NumberUtils.toInt(value);
case "BigInteger" -> NumberUtils.createBigInteger(value);
case "long", "Long" -> NumberUtils.toLong(value);
case "short", "Short" -> NumberUtils.toShort(value);
case "byte", "Byte" -> NumberUtils.toByte(value);
case "float", "Float" -> NumberUtils.toFloat(value);
case "double", "Double" -> NumberUtils.toDouble(value);
case "BigDecimal" -> NumberUtils.createBigDecimal(value);
default -> throw new IllegalStateException("Unexpected value: " + type);
};
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* * License, v. 2.0. If a copy of the MPL was not distributed with this
* * file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package com.sngular.annotation.processor.mapping;

import java.util.Objects;

import com.sngular.annotation.processor.model.FieldValidations;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;

public class BigDecimalMapping implements TypeMapping<Number> {

private final UniformRandomProvider uniformRandomProvider = RandomSource.XO_RO_SHI_RO_128_PP.create();

@Override
public final String getFieldType() {
return "BigDecimal";
}

@Override
public final String getFunctionType() {
return "decimalType";
}

@Override
public final String getFunctionOnlyValue() {
return "decimalValue";
}

@Override
public final Number getRandomDefaultValue(final FieldValidations fieldValidations) {
final Number randomDefaultValue;

if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) {
final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), 0);
final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Double.MAX_VALUE);

randomDefaultValue = uniformRandomProvider.nextDouble(minValue, maxValue);
} else {
randomDefaultValue = uniformRandomProvider.nextDouble(Double.MIN_VALUE, Double.MAX_VALUE);
}

return randomDefaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public final Integer getRandomDefaultValue(final FieldValidations fieldValidatio
final int randomDefaultValue;

if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) {
final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), (int) Byte.MIN_VALUE);
final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Byte.MAX_VALUE);
final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), Integer.MIN_VALUE);
final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), Integer.MAX_VALUE);

randomDefaultValue = uniformRandomProvider.nextInt(minValue, maxValue);
} else {
randomDefaultValue = uniformRandomProvider.nextInt(0, Integer.MAX_VALUE);
randomDefaultValue = uniformRandomProvider.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE);
}

return randomDefaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public final String getFieldType() {

@Override
public final String getFunctionType() {
return "byteType";
return "integerType";
}

@Override
public final String getFunctionOnlyValue() {
return "byteValue";
return "integerType";
}

@Override
Expand All @@ -42,7 +42,7 @@ public final Integer getRandomDefaultValue(final FieldValidations fieldValidatio

randomDefaultValue = uniformRandomProvider.nextInt(minValue, maxValue);
} else {
randomDefaultValue = uniformRandomProvider.nextInt(0, Integer.MAX_VALUE);
randomDefaultValue = uniformRandomProvider.nextInt(Byte.MIN_VALUE, Byte.MAX_VALUE);
}

return randomDefaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;

public class DecimalMapping implements TypeMapping<Number> {
public class DoubleMapping implements TypeMapping<Number> {

private final UniformRandomProvider uniformRandomProvider = RandomSource.XO_RO_SHI_RO_128_PP.create();

@Override
public final String getFieldType() {
return "java.math.BigDecimal";
return "double";
}

@Override
Expand All @@ -37,8 +37,8 @@ public final Number getRandomDefaultValue(final FieldValidations fieldValidation
final Number randomDefaultValue;

if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) {
final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), (int) Byte.MIN_VALUE);
final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Byte.MAX_VALUE);
final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), 0);
final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Double.MAX_VALUE);

randomDefaultValue = uniformRandomProvider.nextDouble(minValue, maxValue);
} else {
Expand All @@ -47,4 +47,9 @@ public final Number getRandomDefaultValue(final FieldValidations fieldValidation

return randomDefaultValue;
}

@Override
public final String getSuffixValue() {
return "D";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* * License, v. 2.0. If a copy of the MPL was not distributed with this
* * file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

package com.sngular.annotation.processor.mapping;

import java.util.Objects;

import com.sngular.annotation.processor.model.FieldValidations;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;

public class FloatMapping implements TypeMapping<Number> {

private final UniformRandomProvider uniformRandomProvider = RandomSource.XO_RO_SHI_RO_128_PP.create();

@Override
public final String getFieldType() {
return "float";
}

@Override
public final String getFunctionType() {
return "decimalType";
}

@Override
public final String getFunctionOnlyValue() {
return "decimalValue";
}

@Override
public final Number getRandomDefaultValue(final FieldValidations fieldValidations) {
final Number randomDefaultValue;

if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) {
final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), 0);
final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Float.MAX_VALUE);

randomDefaultValue = uniformRandomProvider.nextDouble(minValue, maxValue);
} else {
randomDefaultValue = uniformRandomProvider.nextDouble(0, Float.MAX_VALUE);
}

return randomDefaultValue;
}

@Override
public final String getSuffixValue() {
return "F";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public final Integer getRandomDefaultValue(final FieldValidations fieldValidatio
final int randomDefaultValue;

if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) {
final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), (int) Byte.MIN_VALUE);
final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Byte.MAX_VALUE);
final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), Integer.MIN_VALUE);
final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), Integer.MAX_VALUE);

randomDefaultValue = uniformRandomProvider.nextInt(minValue, maxValue);
} else {
randomDefaultValue = uniformRandomProvider.nextInt(0, Integer.MAX_VALUE);
randomDefaultValue = uniformRandomProvider.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE);
}

return randomDefaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final Long getRandomDefaultValue(final FieldValidations fieldValidations)

randomDefaultValue = uniformRandomProvider.nextLong(minValue, maxValue);
} else {
randomDefaultValue = uniformRandomProvider.nextLong(0, Long.MAX_VALUE);
randomDefaultValue = uniformRandomProvider.nextLong(Long.MIN_VALUE, Long.MAX_VALUE);
}

return randomDefaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,15 @@ public final String getFunctionType() {

@Override
public final String getFunctionOnlyValue() {
return "shortValue";
return "integerValue";
}

@Override
public final Integer getRandomDefaultValue(final FieldValidations fieldValidations) {
final int randomDefaultValue;
if (Objects.nonNull(fieldValidations) && ObjectUtils.anyNotNull(fieldValidations.getMin(), fieldValidations.getMax())) {
int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), (int) Short.MIN_VALUE);
minValue = minValue < Short.MIN_VALUE ? minValue : Short.MIN_VALUE;

int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Short.MAX_VALUE);
maxValue = maxValue > Short.MAX_VALUE ? maxValue : Short.MAX_VALUE;
final int minValue = ObjectUtils.defaultIfNull(fieldValidations.getMin(), (int) Short.MIN_VALUE);
final int maxValue = ObjectUtils.defaultIfNull(fieldValidations.getMax(), (int) Short.MAX_VALUE);

randomDefaultValue = uniformRandomProvider.nextInt(minValue, maxValue);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class StringMapping implements TypeMapping<String> {

@Override
public final String getFieldType() {
return "java.lang.String";
return "String";
}

@Override
Expand Down
20 changes: 13 additions & 7 deletions src/main/resources/templates/templateDslBuilder.ftlh
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@

<#macro writeAttrField field>
<#if field.functionByType??>
<#if field.fieldType == "char">
<#if field.fieldType == "boolean">
${field.fieldType} ${field.name} = ${field.defaultValue?string};
<#elseif field.fieldType == "char">
String ${field.name} = "${field.defaultValue}";
<#elseif field.defaultValue?is_number>
<#if field.functionByType == "decimalType">
${field.fieldType} ${field.name} = new BigDecimal("${field.defaultValue?c}");
<#elseif field.fieldType == "BigInteger">
${field.fieldType} ${field.name} = new BigInteger("${field.defaultValue?c}");
<#if field.fieldType == "BigInteger">
BigInteger ${field.name} = new BigInteger("${field.defaultValue?c}");
<#elseif field.fieldType == "BigDecimal">
BigDecimal ${field.name} = new BigDecimal("${field.defaultValue?c}");
<#else>
${field.fieldType} ${field.name} = ${field.defaultValue?c}<#if field.suffixValue?has_content && field.suffixValue == "L">L</#if>;
${field.fieldType} ${field.name} = ${field.defaultValue?c}<#if field.suffixValue?has_content>${field.suffixValue}</#if>;
</#if>
<#elseif field.defaultValue?is_boolean>
${field.fieldType} ${field.name} = ${field.defaultValue?then('true', 'false')};
Expand All @@ -21,7 +23,7 @@
${field.fieldType} ${field.name} = "${field.defaultValue}";
</#if>
<#else>
<@writeComplexAttrField complexField=field/>
<@writeComplexAttrField complexField=field/>
</#if>
</#macro>

Expand Down Expand Up @@ -105,6 +107,10 @@
pactDslJsonBody.${field.functionByType}("${field.name}", (int) ${field.name});
<#elseif field.fieldType == "BigInteger">
pactDslJsonBody.${field.functionByType}("${field.name}", ${field.name}.intValue());
<#elseif field.fieldType == "float">
pactDslJsonBody.${field.functionByType}("${field.name}", (double) ${field.name});
<#elseif field.fieldType == "byte">
pactDslJsonBody.${field.functionByType}("${field.name}", (int) ${field.name});
<#else>
pactDslJsonBody.${field.functionByType}("${field.name}", ${field.name});
</#if>
Expand Down

0 comments on commit 310ca70

Please sign in to comment.