Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

39 suport for array data types #52

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/com/sngular/annotation/pact/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
String value() default "";

String format() default "";

String[] array() default { };
}
142 changes: 116 additions & 26 deletions src/main/java/com/sngular/annotation/processor/PactDslProcessor.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove the license ?

* * 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;

import java.io.IOException;
Expand Down Expand Up @@ -38,20 +32,7 @@
import com.sngular.annotation.processor.exception.PactProcessorException;
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.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;
import com.sngular.annotation.processor.mapping.StringMapping;
import com.sngular.annotation.processor.mapping.TypeMapping;
import com.sngular.annotation.processor.mapping.ZonedDateTimeMapping;
import com.sngular.annotation.processor.mapping.*;
import com.sngular.annotation.processor.model.ClassBuilderTemplate;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is better to have individual imports that a "wild" ( * )
I believe you did not import the style code file

import com.sngular.annotation.processor.model.DslComplexField;
import com.sngular.annotation.processor.model.DslComplexTypeEnum;
Expand Down Expand Up @@ -101,6 +82,29 @@ public class PactDslProcessor extends AbstractProcessor {
.put("ZonedDateTime", new ZonedDateTimeMapping())
.put("java.util.Date", new DateMapping())
.put("Date", new DateMapping())
.put("String[]", new StringArrayMapping())
.put("java.lang.String[]", new StringArrayMapping())
.put("boolean[]", new BooleanArrayMapping())
.put("java.lang.Boolean[]", new BooleanArrayWrapMapping())
.put("byte[]", new ByteArrayMapping())
.put("java.lang.Byte[]", new ByteArrayWrapMapping())
.put("short[]", new ShortArrayMapping())
.put("java.lang.Short[]", new ShortArrayWrapMapping())
.put("int[]", new IntArrayMapping())
.put("java.lang.Integer[]", new IntArrayWrapMapping())
.put("long[]", new LongArrayMapping())
.put("java.lang.Long[]", new LongArrayWrapMapping())
.put("char[]", new CharArrayMapping())
.put("java.lang.Character[]", new CharArrayWrapMapping())
.put("float[]", new FloatArrayMapping())
.put("java.lang.Float[]", new FloatArrayWrapMapping())
.put("double[]", new DoubleArrayMapping())
.put("java.lang.Double[]", new DoubleArrayWrapMapping())
.put("java.math.BigInteger[]", new BigIntegerArrayMapping())
.put("java.math.BigDecimal[]", new BigDecimalArrayMapping())
.put("java.time.ZonedDateTime[]", new ZonedDateTimeArrayMapping())
.put("java.util.Date[]", new DateArrayMapping())
.put("Date[]", new DateArrayMapping())
.build();

private static final String CUSTOM_MODIFIERS = "customModifiers";
Expand Down Expand Up @@ -128,6 +132,83 @@ private static String getFormat(final Element fieldElement, final String default
return StringUtils.defaultIfEmpty(value, defaultFormat);
}

/**
* This method gets the values obtained from the array attribute of the example annotation
* ( @Example(array = {"value1","value2"} )
* @param fieldElement Element, Class containing the attributes annotated with Example
* @param defaultFormat String, Array type.
* @return The processed strings to set the parameter: parametersimpleFieldBuilder.defaultValue
*/
private static String getArrayFromExample(final Element fieldElement, final String defaultFormat) {
String[] value = fieldElement.getAnnotation(Example.class).array();
return PactDslProcessor.getStringFromArray(value,defaultFormat);
}

/**
* This method process the array of values to format them according to the type of array.
* @param arrayValues String[], Strings to be processed.
* @param arrayType String, Array type to be processed.
* @return The processed strings to set the parameter: parametersimpleFieldBuilder.defaultValue
*/
private static String getStringFromArray(String[] arrayValues, String arrayType) {

int size = arrayValues.length-1;
String arrayToString = "";
String element = "";

for (int i = 0; i < arrayValues.length; i++) {

element = arrayValues[i];
switch (TypeArray.get(arrayType)) {
case STRING_ARRAY:
if (i == size) {
arrayToString += "\"" + element + "\"";
} else {
arrayToString += "\"" + element + "\",";
}
break;
case LONG_ARRAY:
case FLOAT_ARRAY:
case DOUBLE_ARRAY:
case BIG_INTEGER_ARRAY:
case BIG_DECIMAL_ARRAY:
case ZONED_DATE_TIME_ARRAY:
case DATE_ARRAY:
if (i == size) { //ultimo elemento
if (i != 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comments in Spanish or add in English

arrayToString += "\"" + element; //si son mas de uno
} else { arrayToString += element; } //si es unico elemento
} else {
if (i != 0) {
arrayToString += "\"" + element + "\","; //si son mas de uno y elemento intermedio
} else { arrayToString += element + "\","; } //si son mas de uno y primer elemento
}
break;
case BOOLEAN_ARRAY:
case BYTE_ARRAY:
case SHORT_ARRAY:
case INT_ARRAY:
if (i == size) {
arrayToString += element ;
} else {
arrayToString += element + ",";
}
break;

case CHAR_ARRAY:
if (i == size) {
arrayToString += "'" + element + "'";
} else {
arrayToString += "'" + element + "',";
}
break;
default:
arrayToString = "not_found_type_array";
}
}
return arrayToString;
}

@Override
public final SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
Expand Down Expand Up @@ -345,13 +426,21 @@ private DslSimpleField.DslSimpleFieldBuilder createSimpleFieldBuilder(final Elem
.empty(false);

if (Objects.nonNull(fieldElement.getAnnotation(DslExclude.class))) {
simpleFieldBuilder.empty(true);
simpleFieldBuilder.empty(true);

} else if (Objects.nonNull(fieldElement.getAnnotation(Example.class))) {
simpleFieldBuilder.defaultValue(getDefaultValue(fieldElement, mapping.getFieldType()));
simpleFieldBuilder.formatValue(getFormat(fieldElement, mapping.getFormatValue()));

if (fieldElement.getAnnotation(Example.class).array().length != 0) {
simpleFieldBuilder.defaultValue(getArrayFromExample(fieldElement, mapping.getFieldType()));
simpleFieldBuilder.formatValue(getFormat(fieldElement, mapping.getFormatValue()));

} else {
simpleFieldBuilder.defaultValue(getDefaultValue(fieldElement, mapping.getFieldType()));
simpleFieldBuilder.formatValue(getFormat(fieldElement, mapping.getFormatValue()));
}
} else {
simpleFieldBuilder.defaultValue(mapping.getRandomDefaultValue(validationBuilder.build(), randomSource));
simpleFieldBuilder.formatValue(mapping.getFormatValue());
simpleFieldBuilder.defaultValue(mapping.getRandomDefaultValue(validationBuilder.build(), randomSource));
simpleFieldBuilder.formatValue(mapping.getFormatValue());
}

return simpleFieldBuilder;
Expand Down Expand Up @@ -385,6 +474,7 @@ private static Object getDefaultValue(final Element fieldElement, final String t
private Optional<TypeMapping<?>> extractMappingByType(final Element element) {

final var type = element.asType();

return switch (type.getKind()) {
case BOOLEAN -> Optional.of(TYPE_MAPPING.get("boolean"));
case BYTE -> Optional.of(TYPE_MAPPING.get("byte"));
Expand All @@ -394,10 +484,10 @@ private Optional<TypeMapping<?>> extractMappingByType(final Element element) {
case CHAR -> Optional.of(TYPE_MAPPING.get("char"));
case FLOAT -> Optional.of(TYPE_MAPPING.get("float"));
case DOUBLE -> Optional.of(TYPE_MAPPING.get("double"));
case ARRAY -> Optional.of(TYPE_MAPPING.get(type.toString()));
case DECLARED -> Optional.ofNullable(TYPE_MAPPING.get(this.typeUtils.asElement(type).getSimpleName().toString()));
default -> Optional.empty();
};
}

}

108 changes: 108 additions & 0 deletions src/main/java/com/sngular/annotation/processor/TypeArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.sngular.annotation.processor;

import java.util.Arrays;
import java.util.Optional;

/** Represents an enum of array types.
* @author Miguel Angel Escobar
* @version 1.0
*/
public enum TypeArray {

/**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to have a comment for each type :)

* An array type {@code String[]}.
*/
STRING_ARRAY("","String[]"),
/**
* An array type {@code boolean[]}.
*/
BOOLEAN_ARRAY("boolean[]","Boolean[]"),

/**
* An array type {@code byte[]}.
*/
BYTE_ARRAY("byte[]","Byte[]"),

/**
* An array type {@code short[]}.
*/
SHORT_ARRAY("short[]","Short[]"),

/**
* An array type {@code int[]}.
*/
INT_ARRAY("int[]","Integer[]"),

/**
* An array type {@code long[]}.
*/
LONG_ARRAY("long[]","Long[]"),

/**
* An array type {@code char[]}.
*/
CHAR_ARRAY("char[]","Character[]"),

/**
* An array type {@code float[]}.
*/
FLOAT_ARRAY("float[]","Float[]"),

/**
* An array type {@code double[]}.
*/
DOUBLE_ARRAY("double[]","Double[]"),

/**
* An array type {@code BigInteger[]}.
*/
BIG_INTEGER_ARRAY("","BigInteger[]"),

/**
* An array type {@code BigDecimal[]}.
*/
BIG_DECIMAL_ARRAY("","BigDecimal[]"),

/**
* An array type {@code ZonedDateTime[]}.
*/
ZONED_DATE_TIME_ARRAY("","ZonedDateTime[]"),

/**
* An array type {@code Date[]}.
*/
DATE_ARRAY("","Date[]");

private String primitiveName;
private String objectName;

TypeArray(String primitiveName, String objectName) {
this.primitiveName = primitiveName;
this.objectName = objectName;
}
/**
* {@return primitive name}
*/
public String getPrimitiveName() {
return primitiveName;
}

/**
* {@return object name}
*/
public String getobjectName() {
return objectName;
}

/**
* {@return TypeArray value that matches typeString}
*/
public static TypeArray get(String typeString) {
Optional<TypeArray> op = Arrays.stream(TypeArray.values())
.filter(typeArray -> typeArray.primitiveName.equals(typeString)
|| typeArray.objectName.equals(typeString))
.findFirst();

return op.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.sngular.annotation.processor.mapping;

import com.sngular.annotation.processor.model.FieldValidations;
import org.apache.commons.rng.UniformRandomProvider;

public class BigDecimalArrayMapping implements TypeMapping<String> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why TypeMapping should not be TypeMapping or TypeMapping?


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

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

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

@Override
public final String getRandomDefaultValue(final FieldValidations fieldValidations, final UniformRandomProvider uniformRandomProvider) {
return "12345678901234567890123456789012345678901234567890123456789012345678901234567890.901234567890123456789012345678901234567890";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generate with a random is better

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 com.sngular.annotation.processor.model.FieldValidations;
import org.apache.commons.rng.UniformRandomProvider;

public class BigIntegerArrayMapping implements TypeMapping<String> {

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

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

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

@Override
public final String getRandomDefaultValue(final FieldValidations fieldValidations, final UniformRandomProvider uniformRandomProvider) {
return "12345678901234567890123456789012345678901234567890123456789012345678901234567890";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generate with a random is better

}
}
Loading
Loading