Skip to content

Commit 4964bec

Browse files
authored
Merge pull request #1512 from ie3-institute/ms/#1511-fix-error-handling-in-method-enrichWithDefault
Fixed handling of erroneous field values in `EntitySource.enrichWithDefault()`
2 parents 2ff3dd6 + 8b1fd5c commit 4964bec

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
- Fixed `EnergyPriceValue.equals()` [#1479](https://github.com/ie3-institute/PowerSystemDataModel/issues/1479)
2424
- Fixed tests of `TimeBasedValue` [#1469](https://github.com/ie3-institute/PowerSystemDataModel/issues/1469)
2525
- Requiring `thermalBus` field in house and storage input [#1509](https://github.com/ie3-institute/PowerSystemDataModel/issues/1509)
26+
- Fixed handling of erroneous field values in `EntitySource.enrichWithDefault()` [#1511](https://github.com/ie3-institute/PowerSystemDataModel/issues/1511)
2627

2728
### Changed
2829
- Updated CI-Pipeline to run task `Deploy` and `Staging` only for `Main` [#1403](https://github.com/ie3-institute/PowerSystemDataModel/issues/1403)

src/main/java/edu/ie3/datamodel/io/factory/FactoryData.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ public boolean containsKey(String key) {
4141
return fieldsToAttributes.containsKey(key);
4242
}
4343

44+
/**
45+
* Checks if the field is empty.
46+
*
47+
* @param field to check
48+
* @return {@code true} if either the key is not present or the field is empty
49+
*/
50+
public boolean isFieldEmpty(String field) {
51+
String value = fieldsToAttributes.getOrDefault(field, null);
52+
return value == null || value.isEmpty();
53+
}
54+
4455
/**
4556
* Returns field value for given field name. Throws {@link FactoryException} if field does not
4657
* exist.

src/main/java/edu/ie3/datamodel/io/source/EntitySource.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,7 @@ WrappedFunction<E, R> enrichWithDefault(
252252
BiFunction<E, T, R> buildingFcn) {
253253
return entityData ->
254254
entityData
255-
.zip(
256-
extractFunction(entityData, fieldName, entities)
257-
.orElse(() -> Try.Success.of(defaultEntity)))
255+
.zip(extractWithDefaultFunction(entityData, fieldName, entities, defaultEntity))
258256
.map(enrichFunction(List.of(fieldName), buildingFcn));
259257
}
260258

@@ -383,6 +381,30 @@ protected static <E extends EntityData, R> Try<R, SourceException> extractFuncti
383381
+ exception.getMessage())));
384382
}
385383

384+
/**
385+
* Method to extract an entity with default.
386+
*
387+
* @param entityData data containing complex entities
388+
* @param fieldName name of the field
389+
* @param entities map: uuid to {@link Entity}
390+
* @param defaultEntity that is used if the field is empty
391+
* @return an enrichment
392+
* @param <E> type of entity data
393+
* @param <R> type of entity
394+
*/
395+
protected static <E extends EntityData, R> Try<R, SourceException> extractWithDefaultFunction(
396+
Try<E, SourceException> entityData,
397+
String fieldName,
398+
Map<UUID, R> entities,
399+
R defaultEntity) {
400+
if (entityData.convert(data -> data.isFieldEmpty(fieldName), f -> false)) {
401+
// return the default entity, if the field is empty
402+
return new Try.Success<>(defaultEntity);
403+
} else {
404+
return extractFunction(entityData, fieldName, entities);
405+
}
406+
}
407+
386408
/**
387409
* Method to extract an {@link Entity} from a given map.
388410
*

0 commit comments

Comments
 (0)