Skip to content

Commit

Permalink
Feat: Extend DurationDecoder to parse Duration so that it is able to …
Browse files Browse the repository at this point in the history
…decode Durations written in ISO 8601 standardized string format like "PT42S"? Address issue #137
  • Loading branch information
credmond-git committed Feb 16, 2024
1 parent 84dfdb4 commit a48be2f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ protected GResultOf<Duration> leafDecode(String path, ConfigNode node) {
results = GResultOf.errors(new ValidationError.ErrorDecodingException(path, node, name()));
}
} else {
results = GResultOf.errors(new ValidationError.DecodingNumberParsing(path, node, name()));
try {
results = GResultOf.result(Duration.parse(value));
} catch (Exception e) {
results = GResultOf.errors(new ValidationError.ErrorDecodingException(path, node, name()));
}
}
return results;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ void decode() {
Assertions.assertEquals(0, result.getErrors().size());
}

@Test
void decodeISO8601() {
DurationDecoder decoder = new DurationDecoder();

GResultOf<Duration> result = decoder.decode("db.port", Tags.of(), new LeafNode("PT20S"),
TypeCapture.of(Long.class), new DecoderContext(decoderService, null));
Assertions.assertTrue(result.hasResults());
Assertions.assertFalse(result.hasErrors());
Assertions.assertEquals(Duration.ofSeconds(20), result.results());
Assertions.assertEquals(0, result.getErrors().size());
}

@Test
void decodeInvalidNode() {
DurationDecoder decoder = new DurationDecoder();
Expand All @@ -83,8 +95,7 @@ void decodeInvalidNode() {
Assertions.assertNull(result.results());
Assertions.assertNotNull(result.getErrors());
Assertions.assertEquals(ValidationLevel.ERROR, result.getErrors().get(0).level());
Assertions.assertEquals("Unable to parse a number on Path: db.port, from node: LeafNode{value='12s4'} " +
"attempting to decode Duration",
Assertions.assertEquals("Unable to decode a Duration on path: db.port, from node: LeafNode{value='12s4'}",
result.getErrors().get(0).description());
}

Expand Down

0 comments on commit a48be2f

Please sign in to comment.