Skip to content

Commit

Permalink
feat: Use POJO instead of generic JsonElement
Browse files Browse the repository at this point in the history
  • Loading branch information
crowleySynopsys committed Jun 15, 2020
1 parent 3d6f9c7 commit 28736e7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 10 deletions.
Expand Up @@ -29,15 +29,17 @@
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.synopsys.integration.blackduck.service.BlackDuckServicesFactory;
import com.synopsys.integration.detectable.Extraction;
import com.synopsys.integration.detectable.detectable.codelocation.CodeLocation;
import com.synopsys.integration.detectable.detectable.exception.DetectableException;
import com.synopsys.integration.detectable.detectable.executable.ExecutableOutput;
import com.synopsys.integration.detectable.detectable.executable.ExecutableRunner;
import com.synopsys.integration.detectable.detectable.executable.ExecutableRunnerException;
import com.synopsys.integration.detectable.detectables.go.gomod.model.GoListUJsonData;
import com.synopsys.integration.detectable.detectables.go.gomod.model.ReplaceData;

public class GoModCliExtractor {
private final ExecutableRunner executableRunner;
Expand Down Expand Up @@ -76,15 +78,15 @@ private List<String> execute(final File directory, final File goExe, final Strin
private List<String> modGraphOutputWithReplacements(File directory, File goExe, List<String> listUJsonOutput) throws ExecutableRunnerException, DetectableException {
final List<String> modGraphOutput = execute(directory, goExe, "Querying for the go mod graph failed:", "mod", "graph");
String jsonString = convertOutputToJsonString(listUJsonOutput);
JsonObject json = gson.fromJson(jsonString, JsonObject.class);
JsonArray json = gson.fromJson(jsonString, JsonArray.class);

for (final JsonElement jsonElement : json.getAsJsonArray(PATHS)) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
JsonObject replace = jsonObject.getAsJsonObject("Replace");
for (final JsonElement jsonElement : json) {
GoListUJsonData data = gson.fromJson(jsonElement, GoListUJsonData.class);
ReplaceData replace = data.getReplace();
if (replace != null) {
String path = jsonObject.get("Path").getAsString();
String originalVersion = jsonObject.get("Version").getAsString();
String replaceVersion = replace.get("Version").getAsString();
String path = data.getPath();
String originalVersion = data.getVersion();
String replaceVersion = replace.getVersion();
replacementData.put(String.format("%s@%s", path, originalVersion), String.format("%s@%s", path, replaceVersion));
}
}
Expand All @@ -105,11 +107,11 @@ private String convertOutputToJsonString(List<String> listUJsonOutput) {
String goModGraphAsString = String.join(System.lineSeparator(), listUJsonOutput);
int lastCloseBrace = goModGraphAsString.lastIndexOf("},");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(String.format("{%n %s: [%n", PATHS));
stringBuilder.append("[" + System.lineSeparator());
stringBuilder.append(goModGraphAsString.substring(0, lastCloseBrace));
stringBuilder.append("}");
stringBuilder.append(goModGraphAsString.substring(lastCloseBrace + 2));
stringBuilder.append("\n] \n}");
stringBuilder.append(System.lineSeparator() + "]");

return stringBuilder.toString();
}
Expand Down
@@ -0,0 +1,39 @@
package com.synopsys.integration.detectable.detectables.go.gomod.model;

import com.google.gson.annotations.SerializedName;

public class GoListUJsonData {

@SerializedName("Path")
private String path;

@SerializedName("Version")
private String version;

@SerializedName("Replace")
private ReplaceData replace;

public String getPath() {
return path;
}

public void setPath(final String path) {
this.path = path;
}

public String getVersion() {
return version;
}

public void setVersion(final String version) {
this.version = version;
}

public ReplaceData getReplace() {
return replace;
}

public void setReplace(final ReplaceData replace) {
this.replace = replace;
}
}
@@ -0,0 +1,28 @@
package com.synopsys.integration.detectable.detectables.go.gomod.model;

import com.google.gson.annotations.SerializedName;

public class ReplaceData {

@SerializedName("Path")
private String path;

@SerializedName("Version")
private String version;

public String getPath() {
return path;
}

public void setPath(final String path) {
this.path = path;
}

public String getVersion() {
return version;
}

public void setVersion(final String version) {
this.version = version;
}
}

0 comments on commit 28736e7

Please sign in to comment.