diff --git a/README.md b/README.md index 008284d..fbb6bc5 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Fixed Width Parser is a small library that purpose is: - If you are using Gradle just add the following dependency to your `build.gradle`. ```groovy -compile "com.github.joutvhu:fixed-width-parser:1.1.0" +compile "com.github.joutvhu:fixed-width-parser:1.1.2" ``` - Or add the following dependency to your `pom.xml` if you are using Maven. @@ -20,7 +20,7 @@ compile "com.github.joutvhu:fixed-width-parser:1.1.0" com.github.joutvhu fixed-width-parser - 1.1.0 + 1.1.2 ``` diff --git a/build.gradle b/build.gradle index b479378..6f8ee43 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ plugins { } group = 'com.github.joutvhu' -version '1.1.0' +version '1.1.2' sourceCompatibility = 1.8 targetCompatibility = 1.8 @@ -24,26 +24,26 @@ repositories { dependencies { annotationProcessor 'org.projectlombok:lombok:1.18.12' - compile 'org.projectlombok:lombok:1.18.12' + compileOnly 'org.projectlombok:lombok:1.18.12' - compile 'org.apache.commons:commons-lang3:3.11' - compile 'com.google.code.findbugs:jsr305:3.0.2' + implementation 'org.apache.commons:commons-lang3:3.11' + implementation 'com.google.code.findbugs:jsr305:3.0.2' - compile 'org.apache.logging.log4j:log4j-api:2.13.3' - compile 'org.apache.logging.log4j:log4j-core:2.13.3' + implementation 'org.apache.logging.log4j:log4j-api:2.13.3' + implementation 'org.apache.logging.log4j:log4j-core:2.13.3' - compile "com.google.re2j:re2j:1.4" + implementation "com.google.re2j:re2j:1.4" - compile "org.reflections:reflections:0.9.12" + implementation "org.reflections:reflections:0.9.12" - compile "com.fasterxml.jackson.core:jackson-core:2.11.2" - compile "com.fasterxml.jackson.core:jackson-annotations:2.11.2" - compile "com.fasterxml.jackson.core:jackson-databind:2.11.2" - compile "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.2" - compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.2" + implementation "com.fasterxml.jackson.core:jackson-core:2.11.2" + implementation "com.fasterxml.jackson.core:jackson-annotations:2.11.2" + implementation "com.fasterxml.jackson.core:jackson-databind:2.11.2" + implementation "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.2" + implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.2" - testCompile 'junit:junit:4.13' - testCompile 'org.junit.jupiter:junit-jupiter-api:5.6.2' + testImplementation 'junit:junit:4.13' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2' testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.6.2' @@ -134,7 +134,7 @@ publishing { withXml { def dependenciesNode = asNode().appendNode('dependencies') - configurations.compile.allDependencies.each { + configurations.implementation.allDependencies.each { def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', it.group) dependencyNode.appendNode('artifactId', it.name) diff --git a/src/main/java/com/joutvhu/fixedwidth/parser/annotation/FixedField.java b/src/main/java/com/joutvhu/fixedwidth/parser/annotation/FixedField.java index 0900712..6016fce 100644 --- a/src/main/java/com/joutvhu/fixedwidth/parser/annotation/FixedField.java +++ b/src/main/java/com/joutvhu/fixedwidth/parser/annotation/FixedField.java @@ -21,7 +21,7 @@ * * @return label name of the field */ - String label(); + String label() default ""; /** * Setups the start position of the field diff --git a/src/main/java/com/joutvhu/fixedwidth/parser/convert/validator/NumberValidator.java b/src/main/java/com/joutvhu/fixedwidth/parser/convert/validator/NumberValidator.java index 89de8c6..38abc1d 100644 --- a/src/main/java/com/joutvhu/fixedwidth/parser/convert/validator/NumberValidator.java +++ b/src/main/java/com/joutvhu/fixedwidth/parser/convert/validator/NumberValidator.java @@ -45,8 +45,8 @@ public void validate(String value, ValidationType type) { } else { String regex = isDecimal ? "^[0-9]+(\\.[0-9]+)?$" : "^[0-9]+$"; if (!Pattern.matches(regex, value)) { - throw new InvalidException(info.formatMessage( - "{title} with value \"{value}\" is not a {number_type}.", + String message = "{title} with value \"{value}\" is not " + (isDecimal ? "a" : "an") + " {number_type}."; + throw new InvalidException(info.formatMessage(message, CommonUtil.putToMap(super.getArguments(value), "{number_type}", () -> isDecimal ? "number" : "integer"))); } diff --git a/src/test/java/com/joutvhu/fixedwidth/parser/DemoTests.java b/src/test/java/com/joutvhu/fixedwidth/parser/DemoTests.java new file mode 100644 index 0000000..fda0a16 --- /dev/null +++ b/src/test/java/com/joutvhu/fixedwidth/parser/DemoTests.java @@ -0,0 +1,66 @@ +package com.joutvhu.fixedwidth.parser; + +import com.joutvhu.fixedwidth.parser.annotation.FixedField; +import com.joutvhu.fixedwidth.parser.annotation.FixedObject; +import com.joutvhu.fixedwidth.parser.annotation.FixedParam; +import com.joutvhu.fixedwidth.parser.constraint.FixedFormat; +import com.joutvhu.fixedwidth.parser.constraint.FixedOption; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +import java.time.LocalDate; +import java.util.List; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class DemoTests { + @Test + public void demoTest() { + Food food = (Food) FixedParser + .parser() + .parse("00001Dragon Fruit 09/30/2020fruit ", Product.class); + + Medicine medicine = (Medicine) FixedParser + .parser() + .parse("60002Golden Star Balm YCamphor Peppermint oil Menthol Tea Tree Oil", Product.class); + + String value = FixedParser + .parser() + .export(food); + + Assertions.assertNotNull(medicine); + Assertions.assertNotNull(value); + } + + @FixedObject(subTypes = { + @FixedObject.Type(value = Food.class, prop = "id", matchWith = "^[0-5].+$") + }, defaultSubType = Medicine.class) + public static class Product { + @FixedField(label = "Product Id", start = 0, length = 5) + private Long id; + + @FixedField(label = "Product Name", start = 5, length = 20) + private String name; + } + + @FixedObject + public static class Food extends Product { + @FixedFormat(format = "MM/dd/yyyy") + @FixedField(label = "Expiry Date", start = 25, length = 10) + private LocalDate expiryDate; + + @FixedOption(options = {"rice ", "breads", "fruit "}) + @FixedField(label = "Type", start = 35, length = 6) + private String type; + } + + @FixedObject + public static class Medicine extends Product { + @FixedFormat(format = "Y|N") + @FixedField(label = "Topical", start = 25, length = 1) + private Boolean topical; + + @FixedField(label = "Ingredients", start = 26, length = 60) + private List<@FixedParam(length = 15) String> ingredients; + } +} diff --git a/src/test/java/com/joutvhu/fixedwidth/parser/ModelG.java b/src/test/java/com/joutvhu/fixedwidth/parser/ModelG.java index 1e9b7ef..92a0eda 100644 --- a/src/test/java/com/joutvhu/fixedwidth/parser/ModelG.java +++ b/src/test/java/com/joutvhu/fixedwidth/parser/ModelG.java @@ -15,7 +15,7 @@ @FixedObject public class ModelG extends ModelD { @FixedRegex(regex = "[a-zA-Z]") - @FixedField(label = "FIELD-D", start = 3, length = 1) + @FixedField(start = 3, length = 1) private Character fieldD; public ModelG(Long fieldA, @FixedRegex(regex = "[a-zA-Z]") Character fieldD) { diff --git a/src/test/java/com/joutvhu/fixedwidth/parser/ModelGTests.java b/src/test/java/com/joutvhu/fixedwidth/parser/ModelGTests.java index 763b6bf..7074051 100644 --- a/src/test/java/com/joutvhu/fixedwidth/parser/ModelGTests.java +++ b/src/test/java/com/joutvhu/fixedwidth/parser/ModelGTests.java @@ -22,6 +22,7 @@ public void read1Test() { Assertions.fail(); } catch (Exception e) { Assertions.assertTrue(e instanceof InvalidException); + Assertions.assertEquals("fieldD field at position 4 and length 1 does not match the /[a-zA-Z]/ regex.", e.getMessage()); } }