Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested objects in auto-sort #115

Merged
merged 4 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.askimed.nf.test.lang.channels;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;

import com.askimed.nf.test.util.AnsiColors;

public class ChannelItemComparator implements Comparator<Object> {

@Override
@SuppressWarnings("unchecked")
public int compare(Object o1, Object o2) {

if (o1 instanceof ArrayList) {
ArrayList<Object> tuple1 = (ArrayList<Object>) o1;
ArrayList<Object> tuple2 = (ArrayList<Object>) o2;
for (int i = 0; i < tuple1.size(); i++) {
Object a = tuple1.get(i);
Object b = tuple2.get(i);

int result = compareObjects(a, b);

if (result != 0) {
return result;
}

}
} else {
return compareObjects(o1, o2);
}

return 0;
}

@SuppressWarnings("rawtypes")
public int compareObjects(Object a, Object b) {

if (a.getClass() != b.getClass()) {
System.err.println(AnsiColors
.yellow("\nWarning: Cannot sort channel, order not deterministic. Objects are different types: "
+ a.getClass() + " vs. " + b.getClass()));
return 1;
}

if (a instanceof String) {
if (isPath(a)) {
return comparePaths(a.toString(), b.toString());
} else {
return compareStrings(a.toString(), b.toString());
}
} else if (isNumber(a)) {
return compareNumbers((Comparable) a, (Comparable) b);
} else if (a instanceof Map) {
return compareMaps((Map) a, (Map) b);
} else {
System.err.println(AnsiColors
.yellow("\nWarning: Cannot sort channel, order not deterministic. Unsupported objects types: "
+ a.getClass() + " vs. " + b.getClass()));
return 1;

}

}

private boolean isNumber(Object a) {
return a instanceof Integer || a instanceof Double || a instanceof Float;
}

private boolean isPath(Object a) {
return a.toString().startsWith("/");
}

@SuppressWarnings({ "rawtypes" })
private int compareMaps(Map a, Map b) {
// since we converted all nested maps to treemaps, toString returns keys sorted in deterministic order.
return compareStrings(a.toString(), b.toString());
}

public int comparePaths(String a, String b) {
String name1 = new File(a).getName();
String name2 = new File(b).getName();
return name1.compareTo(name2);
}

public int compareStrings(String a, String b) {
return a.compareTo(b);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public int compareNumbers(Comparable a, Comparable b) {
return a.compareTo(b);
}

}
56 changes: 56 additions & 0 deletions src/main/java/com/askimed/nf/test/lang/channels/Channels.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.askimed.nf.test.lang.channels;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.askimed.nf.test.util.AnsiText;
import com.askimed.nf.test.util.MapUtil;

import groovy.json.JsonOutput;
import groovy.json.JsonSlurper;

public class Channels extends TreeMap<Object, Object> {

private static final String OUTPUT_CHANNEL_PREFIX = "output_";

private static final long serialVersionUID = 1L;

public void loadFromFolder(File folder, boolean autoSort) {
for (File file : folder.listFiles()) {
if (file.getName().startsWith(OUTPUT_CHANNEL_PREFIX)) {
loadFromFile(file, autoSort);
}
}
}

@SuppressWarnings("unchecked")
public void loadFromFile(File file, boolean autoSort) {
JsonSlurper jsonSlurper = new JsonSlurper();
Map<String, Object> map = (Map<String, Object>) jsonSlurper.parse(file);
//convert map to treemap to sort keys of nested objects
map = MapUtil.convertToTreeMap(map);
if (autoSort) {
for (Object key : map.keySet()) {
Object value = map.get(key);
List<Object> channel = (List<Object>) value;
sortChannel(channel);
}
}
putAll(map);
}

public void view() {
System.out.println(AnsiText.padding(JsonOutput.prettyPrint(JsonOutput.toJson(this)), 4));
}

public void sortChannel(List<Object> channel) {
channel.sort(new ChannelItemComparator());
}

@Override
public Object get(Object key) {
return super.get(key.toString());
}
}
127 changes: 0 additions & 127 deletions src/main/java/com/askimed/nf/test/lang/process/ChannelsOutput.java

This file was deleted.

5 changes: 3 additions & 2 deletions src/main/java/com/askimed/nf/test/lang/process/Process.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.askimed.nf.test.lang.process;

import com.askimed.nf.test.lang.WorkflowMeta;
import com.askimed.nf.test.lang.channels.Channels;

public class Process extends WorkflowMeta{

private ChannelsOutput out = new ChannelsOutput();
private Channels out = new Channels();

private String mapping = "";

Expand All @@ -22,7 +23,7 @@ public String getMapping() {
return mapping;
}

public ChannelsOutput getOut() {
public Channels getOut() {
return out;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/askimed/nf/test/lang/workflow/Workflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import java.beans.Transient;

import com.askimed.nf.test.lang.WorkflowMeta;
import com.askimed.nf.test.lang.process.ChannelsOutput;
import com.askimed.nf.test.lang.channels.Channels;

public class Workflow extends WorkflowMeta {

private String mapping = "";

private String name = "workflow";

private ChannelsOutput out = new ChannelsOutput();
private Channels out = new Channels();

public void setName(String name) {
this.name = name;
Expand All @@ -26,7 +26,7 @@ public String getMapping() {
return mapping;
}

public ChannelsOutput getOut() {
public Channels getOut() {
return out;
}

Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/askimed/nf/test/util/MapUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.askimed.nf.test.util;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;

public class MapUtil {

public static TreeMap<String, Object> convertToTreeMap(Map<String, Object> hashMap) {
TreeMap<String, Object> treeMap = new TreeMap<>();
convertToTreeMap(hashMap, treeMap);
return treeMap;
}

@SuppressWarnings("unchecked")
private static void convertToTreeMap(Map<String, Object> sourceMap, TreeMap<String, Object> targetMap) {
for (Map.Entry<String, Object> entry : sourceMap.entrySet()) {
String currentKey = entry.getKey();

if (entry.getValue() instanceof Map) {
// nested map
TreeMap<String, Object> nestedTreeMap = new TreeMap<>();
targetMap.put(currentKey, nestedTreeMap);
convertToTreeMap((Map<String, Object>) entry.getValue(), nestedTreeMap);
} else if (entry.getValue() instanceof List) {
// and array
List<Object> targetList = new Vector<Object>();
for (Object item : (List<Object>) entry.getValue()) {
// nested map in array
if (item instanceof Map) {
TreeMap<String, Object> treeMap = new TreeMap<>();
convertToTreeMap((Map<String, Object>) item, treeMap);
targetList.add(treeMap);
} else {
targetList.add(item);
}
}
targetMap.put(currentKey, targetList);
} else {
targetMap.put(currentKey, entry.getValue());
}
}
}

}
Loading