|
| 1 | +# Finish workflow validation cleanup |
| 2 | + |
| 3 | +Assume Java 17. |
| 4 | + |
| 5 | +Use `$java-optionals` to create `WorkflowConfigValidation.java` with the revised class. |
| 6 | + |
| 7 | +This is adapted from a real AI-assisted maintainability cleanup. The original task was a broad |
| 8 | +readability/refactor pass that had to preserve the same behavior. The code below works, but the |
| 9 | +validation method should be easier to maintain. |
| 10 | + |
| 11 | +Current code: |
| 12 | + |
| 13 | +```java |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Optional; |
| 17 | + |
| 18 | +final class WorkflowConfigValidation { |
| 19 | + WorkflowValidation validate(ConnectedBoard board, Map<String, Object> yaml) { |
| 20 | + Optional<String> configuredBoardId = boardId(yaml); |
| 21 | + if (configuredBoardId.isEmpty()) { |
| 22 | + return WorkflowValidation.warn("Workflow file is missing tracker.board_id for \"" |
| 23 | + + board.boardName() + "\": " + board.workflowPath()); |
| 24 | + } |
| 25 | + String boardId = configuredBoardId.orElseThrow(); |
| 26 | + if (!boardId.equals(board.boardId()) && !boardId.equals(board.boardKey())) { |
| 27 | + return WorkflowValidation.warn("Workflow tracker.board_id does not match the connected board for \"" |
| 28 | + + board.boardName() + "\": expected " + board.boardId() + " or " + board.boardKey() |
| 29 | + + " but found " + boardId); |
| 30 | + } |
| 31 | + Optional<Integer> configuredServerPort = serverPort(yaml); |
| 32 | + if (configuredServerPort.isEmpty()) { |
| 33 | + return WorkflowValidation.warn("Workflow file is missing server.port for \"" |
| 34 | + + board.boardName() + "\": " + board.workflowPath()); |
| 35 | + } |
| 36 | + if (configuredServerPort.orElseThrow() != board.serverPort()) { |
| 37 | + return WorkflowValidation.warn("Workflow server.port does not match the connected board for \"" |
| 38 | + + board.boardName() + "\": expected " + board.serverPort() + " but found " |
| 39 | + + configuredServerPort.orElseThrow()); |
| 40 | + } |
| 41 | + return WorkflowValidation.valid(); |
| 42 | + } |
| 43 | + |
| 44 | + Optional<String> boardId(Map<String, Object> yaml) { |
| 45 | + Object trackerValue = yaml.get("tracker"); |
| 46 | + if (!(trackerValue instanceof Map<?, ?> tracker)) { |
| 47 | + return Optional.empty(); |
| 48 | + } |
| 49 | + Object value = tracker.get("board_id"); |
| 50 | + String text = value == null ? null : String.valueOf(value); |
| 51 | + return text == null || text.isBlank() ? Optional.empty() : Optional.of(text); |
| 52 | + } |
| 53 | + |
| 54 | + Optional<Integer> serverPort(Map<String, Object> yaml) { |
| 55 | + Object serverValue = yaml.get("server"); |
| 56 | + if (!(serverValue instanceof Map<?, ?> server)) { |
| 57 | + return Optional.empty(); |
| 58 | + } |
| 59 | + Object value = server.get("port"); |
| 60 | + if (value instanceof Number number) { |
| 61 | + return Optional.of(number.intValue()); |
| 62 | + } |
| 63 | + if (value instanceof String text && !text.isBlank()) { |
| 64 | + try { |
| 65 | + return Optional.of(Integer.parseInt(text.trim())); |
| 66 | + } catch (NumberFormatException ignored) { |
| 67 | + return Optional.empty(); |
| 68 | + } |
| 69 | + } |
| 70 | + return Optional.empty(); |
| 71 | + } |
| 72 | + |
| 73 | + record ConnectedBoard(String boardName, String boardId, String boardKey, int serverPort, Path workflowPath) {} |
| 74 | + |
| 75 | + record WorkflowValidation(boolean ok, String message) { |
| 76 | + static WorkflowValidation valid() { return new WorkflowValidation(true, ""); } |
| 77 | + static WorkflowValidation warn(String message) { return new WorkflowValidation(false, message); } |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +Required changes: |
| 83 | + |
| 84 | +- Keep the same warning messages and successful `WorkflowValidation.valid()` result. |
| 85 | +- Keep accepting `board.boardId()` and `board.boardKey()` as matching tracker board ids. |
| 86 | +- Keep accepting numeric server ports and trimmed numeric string server ports. |
| 87 | +- Return the missing-board warning when `tracker.board_id` is absent or blank. |
| 88 | +- Return the missing-port warning when `server.port` is absent, blank, malformed, or unsupported. |
| 89 | +- Leave `boardId(...)` and `serverPort(...)` returning `Optional`. |
| 90 | +- You may extract private helpers if that makes the validation flow clearer. |
0 commit comments