Skip to content

Commit

Permalink
Split method into smaller sub methods to reduce responsibility per me…
Browse files Browse the repository at this point in the history
…thod

Signed-off-by: Yannic Klem <Yannic.Klem@bosch.io>
  • Loading branch information
Yannic92 committed May 5, 2022
1 parent 9997cc9 commit 55fa754
Showing 1 changed file with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,31 +186,43 @@ private static JsonObject buildJsonRepresentation(final List<CommandResponse<?>>
final boolean aggregateResults) {

final var schemaVersion = dittoHeaders.getSchemaVersion().orElse(JsonSchemaVersion.LATEST);
final var builder = JsonObject.newBuilder();

if (!aggregateResults && commandResponses.size() == 1) {
final CommandResponse<?> commandResponse = commandResponses.get(0);
return commandResponseToJson(commandResponse, schemaVersion);
} else {
return buildAggregatedJsonRepresentation(commandResponses, schemaVersion);
}
}

private static JsonObject buildAggregatedJsonRepresentation(final List<CommandResponse<?>> commandResponses,
final JsonSchemaVersion schemaVersion) {
final var builder = JsonObject.newBuilder();
var i = 0;
for (final var cmdR : commandResponses) {
final var key = String.format("/%s/%s", calculateServiceName(cmdR), calculateInstance(cmdR, i++));
// include both regular and special fields for devops command responses
final JsonObject responseJson;
if (cmdR instanceof ExecutePiggybackCommandResponse response) {
responseJson = response.getResponse().asObject();
} else {
responseJson = cmdR.toJson(schemaVersion, FieldType.regularOrSpecial());
}
if (!aggregateResults && commandResponses.size() == 1) {
return responseJson;
}
final JsonObject responseJson = commandResponseToJson(cmdR, schemaVersion);
builder.set(key, responseJson);
}

if (builder.isEmpty()) {
return JsonFactory.nullObject();
} else {
return builder.build();
}
}

private static JsonObject commandResponseToJson(final CommandResponse<?> commandResponse,
final JsonSchemaVersion schemaVersion) {
final JsonObject responseJson;
if (commandResponse instanceof ExecutePiggybackCommandResponse response) {
responseJson = response.getResponse().asObject();
} else {
responseJson = commandResponse.toJson(schemaVersion, FieldType.regularOrSpecial());
}
return responseJson;
}

private static String calculateServiceName(final CommandResponse<?> commandResponse) {
final String result;
if (commandResponse instanceof DevOpsCommandResponse) {
Expand Down

0 comments on commit 55fa754

Please sign in to comment.