Skip to content

Commit

Permalink
add keeping whitespaces option
Browse files Browse the repository at this point in the history
  • Loading branch information
g0ddest committed Oct 13, 2024
1 parent 8742a70 commit ea4887c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ plugins {
}

group 'name.velikodniy.vitaliy'
version '0.12'
version '0.13'

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private boolean acceptFieldContent(String content, FixedField fieldAnnotation) {
if (content == null) {
return false;
}
if (content.trim().isEmpty()) {
if (content.trim().isEmpty() && !fieldAnnotation.allowEmptyStrings()) {
return false;
}
if (fieldAnnotation.ignore().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,12 @@
* @return pattern matching content to ignore
*/
String ignore() default "";

/**
* Allows empty string as value, by default it is false.
* If true - values will be kept as is
* If false - value will be null
* @return allows empty string as value boolean
*/
boolean allowEmptyStrings() default false;
}
12 changes: 12 additions & 0 deletions src/test/java/name/velikodniy/vitaliy/fixedlength/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,16 @@ void testParseMixedLineTypeCustomPredicate() throws FixedLengthException {

assertEquals(2, parse.size());
}

@Test
@DisplayName("Parse object with whitespaces preventing it to cast as null")
void testParseWhitespaceNonNull() throws FixedLengthException {

List<StringHolder> holders = new FixedLength<StringHolder>()
.registerLineType(StringHolder.class)
.parse(new ByteArrayInputStream(" some text to drop".getBytes()));

assertEquals(1, holders.size());
assertEquals(" ", holders.get(0).value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package name.velikodniy.vitaliy.fixedlength;

import name.velikodniy.vitaliy.fixedlength.annotation.FixedField;

public class StringHolder {
@FixedField(offset = 1, length = 3, padding = Character.MIN_VALUE, allowEmptyStrings = true)
public String value;
}

0 comments on commit ea4887c

Please sign in to comment.