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

add keeping whitespaces option #46

Merged
merged 1 commit into from
Oct 13, 2024
Merged
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: 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;
}
Loading