Skip to content

Commit

Permalink
issue - 41 - Correction related to BigInteger (#45)
Browse files Browse the repository at this point in the history
* issue - 41 - Correction related to BigInteger

* Minor version upgrade

---------

Co-authored-by: Tiago Simoes <[email protected]>
Co-authored-by: Alejandro Pena Lorenzo <[email protected]>
Co-authored-by: Alejandro Pena Lorenzo <[email protected]>
  • Loading branch information
4 people authored Nov 28, 2023
1 parent 3d3c8c6 commit a931125
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
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.5</version>
<version>1.1.6</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,6 +37,7 @@
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.BigIntegerMapping;
import com.sngular.annotation.processor.mapping.BooleanMapping;
import com.sngular.annotation.processor.mapping.ByteMapping;
import com.sngular.annotation.processor.mapping.CharMapping;
Expand Down Expand Up @@ -73,6 +74,9 @@ public class PactDslProcessor extends AbstractProcessor {
.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())
Expand Down Expand Up @@ -328,6 +332,7 @@ private static Object getDefaultValue(final Element fieldElement, final String t
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);
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 BigIntegerMapping implements TypeMapping<Integer> {

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

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

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

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

@Override
public final Integer getRandomDefaultValue(final FieldValidations fieldValidations) {
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);

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

return randomDefaultValue;
}
}
5 changes: 5 additions & 0 deletions src/main/resources/templates/templateDslBuilder.ftlh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<#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}");
<#else>
${field.fieldType} ${field.name} = ${field.defaultValue?c}<#if field.suffixValue?has_content && field.suffixValue == "L">L</#if>;
</#if>
Expand Down Expand Up @@ -99,6 +101,8 @@
<#else>
pactDslJsonBody.${field.functionByType}("${field.name}", "${field.formatValue?no_esc}", ${field.name}.toInstant());
</#if>
<#elseif field.fieldType == "BigInteger">
pactDslJsonBody.${field.functionByType}("${field.name}", ${field.name}.intValue());
<#else>
pactDslJsonBody.${field.functionByType}("${field.name}", ${field.name});
</#if>
Expand Down Expand Up @@ -240,6 +244,7 @@ import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.util.Date;
import java.util.List;
Expand Down

0 comments on commit a931125

Please sign in to comment.