Skip to content

Commit

Permalink
Use "-" to signal null fields (#258)
Browse files Browse the repository at this point in the history
To be able to see that a fields
value was `null`, "-" is used as
the placeholder substitution value
  • Loading branch information
AntonOellerer authored Sep 13, 2024
1 parent 25ecd54 commit aa68318
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '4.2.4'
version = '4.2.5'

java {
toolchain {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ private Optional<PlaceholderData> doReflectiveResolve(String placeholderName, Lo
}
var wrappedProperty = getBeanProperty(placeholderName);
if (wrappedProperty.isEmpty()) {
return Optional.of(new ScalarPlaceholderData<>(null));
return Optional.of(new ScalarPlaceholderData<>("-"));
}
return toPlaceholderData(placeholderName, locale, wrappedProperty.get());
} catch (NoSuchMethodException | IllegalArgumentException e) {
Expand Down Expand Up @@ -502,12 +502,11 @@ private Optional<PlaceholderData> getObjectTranslation(String placeholderName, L
}

private Optional<Object> getBeanProperty(String placeholderName) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
var ignoreCasePlaceholder = placeholderName.toLowerCase();
if (SELF_REFERENCE.equals(placeholderName)) {
return Optional.ofNullable(bean);
} else if (bean.getClass().isRecord()) {
var accessor = Arrays.stream(bean.getClass().getRecordComponents())
.filter(recordComponent -> recordComponent.getName().toLowerCase().equals(ignoreCasePlaceholder))
.filter(recordComponent -> recordComponent.getName().equalsIgnoreCase(placeholderName))
.map(RecordComponent::getAccessor)
.findFirst()
.orElseThrow(() -> new NoSuchMethodException("Record %s does not have field %s".formatted(bean.getClass().toString(), placeholderName)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public boolean isTruthy() {
if (value instanceof Number number) {
return number.longValue() != 0L;
} else if (value instanceof String string) {
return !string.isEmpty();
return !string.isEmpty() && !string.equals("-");
} else if (value instanceof Boolean bool) {
return bool;
} else if (value instanceof Collection<?> collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,13 @@ void shouldResolveUUIDtoString() {
}

@Test
void shouldResolveNullToEmptyString() {
void shouldResolveNullToMinus() {
Person picardPerson = SampleModelData.PICARD_NULL;
var resolver = new ReflectionResolver(picardPerson);

var name = resolver.resolve("firstName");

assertThat(name.get().toString(), equalTo(""));
assertThat(name.get().toString(), equalTo("-"));
}

@Test
Expand Down

0 comments on commit aa68318

Please sign in to comment.