Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovsenka committed Sep 28, 2024
1 parent 7cb81ea commit 3cecd0f
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 172 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ jobs:
- run: make test
working-directory: ./app
- name: Publish code coverage
uses: paambaati/codeclimate-action@v9.0.0
uses: paambaati/codeclimate-action@v3.1.1
env:
CC_TEST_REPORTER_ID: ${{secrets.CC_TEST_REPORTER_ID}}
JACOCO_SOURCE_PATH: app/src/main/java ./app/cc-test-reporter format-coverage ./app/build/reports/jacoco/test/jacocoTestReport.xml --input-type jacoco
JACOCO_SOURCE_PATH: ${{github.workspace}}app/src/main/java
with:
coverageCommand: make -C app report
coverageLocations: ${{github.workspace}}app/build/reports/jacoco/test/jacocoTestReport.xml:jacoco
10 changes: 9 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ repositories {

dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.2"))
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
testImplementation("org.junit.jupiter:junit-jupiter:5.10.3")
implementation("info.picocli:picocli:4.7.6")
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
implementation("com.fasterxml.jackson.core:jackson-databind:2.17.1")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.18.0-rc1")
}



tasks.withType<Test> {
useJUnitPlatform()
}
Expand All @@ -30,6 +32,12 @@ tasks.test {
useJUnitPlatform()
}

tasks.jacocoTestReport {
reports {
xml.required = true
}
}

application {
mainClass = "hexlet.code.App"
}
Expand Down
15 changes: 3 additions & 12 deletions app/src/main/java/hexlet/code/App.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package hexlet.code;

import hexlet.code.formatters.FormatterFactory;
import hexlet.code.formatters.IFormatter;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.nio.file.Paths;
import java.util.Map;
import java.util.concurrent.Callable;


@Command(name = "gendiff", mixinStandardHelpOptions = true,
description = "Compares two configuration files and shows a difference.",
version = "gendiff 0.2"
version = "gendiff 1.0"
)
public class App implements Callable<Integer> {

@Option(names = { "-f", "--format" },
defaultValue = "stylish",
paramLabel = "format",
description = "output format [default: stylish]")
private String format;
String format;

@Parameters(paramLabel = "filepath1", description = "path to first file")
String filepath;
Expand All @@ -32,12 +28,7 @@ public class App implements Callable<Integer> {

@Override
public Integer call() throws Exception {
Map<String, Object[]> diffResult = Differ.generate(
Paths.get(filepath).toAbsolutePath().normalize(),
Paths.get(filepath2).toAbsolutePath().normalize()
);
IFormatter formatter = FormatterFactory.getFormatter(format);
System.out.println(formatter.format(diffResult));
System.out.print(Differ.generate(filepath, filepath2, format));
return 0;
}
public static void main(String[] args) {
Expand Down
43 changes: 43 additions & 0 deletions app/src/main/java/hexlet/code/DiffBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package hexlet.code;

import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.LinkedHashMap;
import java.util.ArrayList;

public class DiffBuilder {
public static Map<String, Map<Object, Object>> build(Map<String, Object> mapFile, Map<String, Object> mapFile2) {
SortedSet<String> keys = new TreeSet<>(mapFile.keySet());
keys.addAll(mapFile2.keySet());
Map<String, Map<Object, Object>> diffResult = new LinkedHashMap<>();
LinkedHashMap<Object, Object> deleteMap = new LinkedHashMap<>();
LinkedHashMap<Object, Object> notChangedMap = new LinkedHashMap<>();
LinkedHashMap<Object, Object> changedMap = new LinkedHashMap<>();
LinkedHashMap<Object, Object> addedMap = new LinkedHashMap<>();
for (String key : keys) {
Object value = mapFile.get(key) == null ? "null" : mapFile.get(key);
if (mapFile2.containsKey(key)) {
Object differFileKeyValue = mapFile2.get(key) == null ? "null" : mapFile2.get(key);
if (differFileKeyValue.equals(value)) {
notChangedMap.put(key, value);
} else if (!mapFile.containsKey(key)) {
addedMap.put(key, differFileKeyValue);
} else {
ArrayList<Object> values = new ArrayList<>();
values.add(value);
values.add(differFileKeyValue);
changedMap.put(key, values);
}
mapFile2.remove(key);
} else {
deleteMap.put(key, value);
}
}
diffResult.put("notchanged", notChangedMap);
diffResult.put("add", addedMap);
diffResult.put("changed", changedMap);
diffResult.put("delete", deleteMap);
return diffResult;
}
}
44 changes: 10 additions & 34 deletions app/src/main/java/hexlet/code/Differ.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,21 @@
package hexlet.code;

import hexlet.code.formatters.IFormatter;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.LinkedHashMap;


public class Differ {
public static Map<String, Object[]> generate(Path filepath, Path filepath2) {
public static String generate(String pathToFile, String pathToFile2, String format) throws Exception {
Path filepath = Paths.get(pathToFile).toAbsolutePath().normalize();
Path filepath2 = Paths.get(pathToFile2).toAbsolutePath().normalize();

Map<String, Object> mapFile = Parser.parse(filepath);
Map<String, Object> mapFile2 = Parser.parse(filepath2);
return compareFiles(mapFile, mapFile2);
}

private static Map<String, Object[]> compareFiles(Map<String, Object> mapFile, Map<String, Object> mapFile2) {
SortedSet<String> keys = new TreeSet<>(mapFile.keySet());
keys.addAll(mapFile2.keySet());
Map<String, Object[]> diffResult = new LinkedHashMap<>();
for (String key : keys) {
Object value = mapFile.get(key) == null ? "null" : mapFile.get(key);
if (mapFile2.containsKey(key)) {
Object differFileKeyValue = mapFile2.get(key) == null ? "null" : mapFile2.get(key);
if (differFileKeyValue.equals(value)) {
diffResult.put(key, new Object[] {"none", value});
} else if (!mapFile.containsKey(key)) {
diffResult.put(key, new Object[] {"+", differFileKeyValue });
} else {
diffResult.put(key, new Object[] {"-+", value, differFileKeyValue });
}
mapFile2.remove(key);
} else {
diffResult.put(key, new Object[] {"-", value});
}
}
mapFile2.forEach((k, v) -> {
if (!mapFile.containsKey(k)) {
diffResult.put(k, new Object[] {"+", v});
}
});
return diffResult;
IFormatter formatter = null;
formatter = FormatterFactory.getFormatter(format);
return formatter.format(DiffBuilder.build(mapFile, mapFile2));
}

}
21 changes: 21 additions & 0 deletions app/src/main/java/hexlet/code/FormatterFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hexlet.code;

import hexlet.code.formatters.IFormatter;
import hexlet.code.formatters.JsonFormatter;
import hexlet.code.formatters.PlainFormatter;
import hexlet.code.formatters.StylishFormatter;

public class FormatterFactory {

public static IFormatter getFormatter(String format) throws Exception {
return switch (format) {
case "stylish" -> new StylishFormatter();
case "plain" -> new PlainFormatter();
case "json" -> new JsonFormatter();
default -> throw new Exception("Unexpected format '%s'. Possible formats: [stylish, plain, json]"
.formatted(format));
};
}


}
19 changes: 0 additions & 19 deletions app/src/main/java/hexlet/code/ObjectMapperFactory.java

This file was deleted.

10 changes: 8 additions & 2 deletions app/src/main/java/hexlet/code/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;

import java.io.File;
import java.io.IOException;
Expand All @@ -11,8 +13,12 @@
public class Parser {
public static Map<String, Object> parse(Path file) {
Map<String, Object> mapFile = null;
ObjectMapperFactory factory = new ObjectMapperFactory();
ObjectMapper mapper = factory.getObjectMapper(file);
ObjectMapper mapper = null;
if (file.toString().endsWith(".yml")) {
mapper = new YAMLMapper();
} else if (file.toString().endsWith(".json")) {
mapper = new JsonMapper();
}
try {
mapFile = mapper.readValue(new File(file.toString()),
new TypeReference<>() { });
Expand Down
14 changes: 0 additions & 14 deletions app/src/main/java/hexlet/code/formatters/FormatterFactory.java

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/java/hexlet/code/formatters/IFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import java.util.Map;

public interface IFormatter {
String format(Map<String, Object[]> differMap);
String format(Map<String, Map<Object, Object>> differMap);
}
25 changes: 8 additions & 17 deletions app/src/main/java/hexlet/code/formatters/JsonFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,19 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import com.fasterxml.jackson.databind.json.JsonMapper;

import java.util.Map;
import java.util.TreeMap;

public class JsonFormatter implements IFormatter {
@Override
public String format(Map<String, Object[]> differMap) {
ObjectMapper mapper = new ObjectMapper();
HashMap<String, Object> map = new HashMap<>();
differMap.forEach((k, v) -> {
switch (v[0].toString()) {
case "+", "-":
map.put(k, v[1]);
break;
case "-+":
map.put(k, v[2]);
break;
default:
break;
}
});
public String format(Map<String, Map<Object, Object>> differMap) {
TreeMap<Object, Object> properties = new TreeMap<>();
differMap.forEach((k, v) -> properties.putAll(v));
ObjectMapper mapper = new JsonMapper();
try {
return mapper.writeValueAsString(map);
return mapper.writeValueAsString(properties);
} catch (JsonProcessingException e) {
System.out.println(e.getMessage());
}
Expand Down
29 changes: 18 additions & 11 deletions app/src/main/java/hexlet/code/formatters/PlainFormatter.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
package hexlet.code.formatters;

import java.util.ArrayList;
import java.util.Map;

public class PlainFormatter implements IFormatter {
@Override
public String format(Map<String, Object[]> differMap) {
public String format(Map<String, Map<Object, Object>> differMap) {
StringBuilder sb = new StringBuilder();
differMap.forEach((k, v) -> {
Object keyvalue = v[1].getClass().isArray() ? "[complex value]" : v[1];
Object keyvalue2;
switch (v[0].toString()) {
case "+":
sb.append("\nProperty '%s' was added with value: %s".formatted(k, keyvalue));
switch (k) {
case "add":
v.forEach((key, value) ->
sb.append("\nProperty '%s' was added with value: %s"
.formatted(key, value)));
break;
case "-":
sb.append("\nProperty '%s' was removed".formatted(k));
case "delete":
v.forEach((key, value) ->
sb.append("\nProperty '%s' was removed"
.formatted(key)));
break;
case "-+":
keyvalue2 = v[2].getClass().isArray() ? "[complex value]" : v[2];
sb.append("\nProperty '%s' was updated. From %s to %s".formatted(k, keyvalue, keyvalue2));
case "changed":
v.forEach((key, value) -> {
Object resultValue = ((ArrayList<?>) value).get(0);
Object resultValue2 = ((ArrayList<?>) value).get(1);
sb.append("\nProperty '%s' was updated. From %s to %s"
.formatted(key, resultValue, resultValue2));
});
break;
default:
break;
Expand Down
27 changes: 16 additions & 11 deletions app/src/main/java/hexlet/code/formatters/StylishFormatter.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
package hexlet.code.formatters;

import java.util.ArrayList;
import java.util.Map;

public class StylishFormatter implements IFormatter {
@Override
public String format(Map<String, Object[]> differMap) {
public String format(Map<String, Map<Object, Object>> differMap) {
StringBuilder result = new StringBuilder("{");
differMap.forEach((k, v) -> {
switch (v[0].toString()) {
case "none":
result.append("\n\t").append(k).append(": ").append(v[1]);
switch (k) {
case "notchanged":
v.forEach((key, value) -> result.append("\n ").append(key).append(": ").append(value));
break;
case "+":
result.append("\n\t+ ").append(k).append(": ").append(v[1]);
case "add":
v.forEach((key, value) -> result.append("\n + ").append(key).append(": ").append(v.get(key)));
break;
case "-":
result.append("\n\t- ").append(k).append(": ").append(v[1]);
case "delete":
v.forEach((key, value) -> result.append("\n - ").append(key).append(": ").append(v.get(key)));
break;
case "-+":
result.append("\n\t- ").append(k).append(": ").append(v[1]);
result.append("\n\t+ ").append(k).append(": ").append(v[2]);
case "changed":
v.forEach((key, value) -> {
Object resultValue = ((ArrayList<?>) value).get(0);
Object resultValue2 = ((ArrayList<?>) value).get(1);
result.append("\n - ").append(key).append(": ").append(resultValue);
result.append("\n + ").append(key).append(": ").append(resultValue2);
});
break;
default:
break;
Expand Down
Loading

0 comments on commit 3cecd0f

Please sign in to comment.