Skip to content

Commit

Permalink
Rely on setters for catching out-of-range values.
Browse files Browse the repository at this point in the history
Now that setters in the Mapping class can catch out-of-range values (for
slots properly annotated with range constraints in the underlying LinkML
model), we no longer need to explicitly check values when reading a
SSSOM/TSV file: we can just call the setters with whatever value we got
and catch the IllegalArgumentException that would be thrown if the value
is out of the expected range.
  • Loading branch information
gouttegd committed Jul 19, 2024
1 parent cf76669 commit 65f9f15
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
8 changes: 6 additions & 2 deletions core/src/main/java/org/incenp/obofoundry/sssom/Slot.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,13 @@ public void setValue(T object, Object value) {
try {
Method accessor = object.getClass().getDeclaredMethod(accessorName, new Class<?>[] { field.getType() });
accessor.invoke(object, new Object[] { field.getType().cast(value) });
} catch ( NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e ) {
} catch ( NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException e ) {
// Should never happen
} catch ( InvocationTargetException e ) {
if ( e.getCause() instanceof IllegalArgumentException ) {
throw IllegalArgumentException.class.cast(e.getCause());
}
// Should never happen (IAE is the only exception thrown by setters)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -344,16 +344,11 @@ private <T> void setSlotValue(Slot<T> slot, T object, Object rawValue) throws SS
}
} else if ( type == Double.class && String.class.isInstance(rawValue) ) {
try {
Double v = Double.valueOf(String.class.cast(rawValue));
// HACK(ish): We are using the fact that all Double-ranged slots in SSSOM are
// limited to the [0.0,1.0] range. If that changes in the future, we'll need to
// use the minimal/maximal value constraints from the LinkML model.
if ( v < 0.0 || v > 1.0 ) {
throw new SSSOMFormatException(String.format("Out-of-range value for '%s'", slot.getName()));
}
slot.setValue(object, v);
slot.setValue(object, Double.valueOf(String.class.cast(rawValue)));
} catch ( NumberFormatException e ) {
onTypingError(slot.getName(), e);
} catch ( IllegalArgumentException e ) {
throw new SSSOMFormatException(String.format("Out-of-range value for '%s'", slot.getName()));
}
} else if ( type == EntityType.class && String.class.isInstance(rawValue) ) {
EntityType value = EntityType.fromString(String.class.cast(rawValue));
Expand Down

0 comments on commit 65f9f15

Please sign in to comment.