forked from finos/morphir
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98218c5
commit 1d7e75f
Showing
9 changed files
with
286 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
.vscode/ | ||
package-lock.json | ||
node_modules/ | ||
tsp-output/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
query { | ||
elements { | ||
element_type { | ||
__typename | ||
} | ||
id | ||
name | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
DataThread/src/main/java/datathread/backends/JavaEmitter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package datathread.backends; | ||
|
||
import com.squareup.javapoet.ClassName; | ||
import com.squareup.javapoet.FieldSpec; | ||
import com.squareup.javapoet.TypeSpec; | ||
import datathread.metastore.*; | ||
|
||
import javax.lang.model.element.Modifier; | ||
import java.io.File; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
public class JavaEmitter { | ||
public static interface Context { | ||
public Optional<Element> getElement(String elementId); | ||
} | ||
|
||
private final Context context; | ||
|
||
public JavaEmitter(Context context) { | ||
this.context = context; | ||
} | ||
|
||
public TypeSpec handleDataset(Dataset dataset) { | ||
String name = nameToJavaClassName(dataset.getName()); | ||
List<FieldSpec> fields = | ||
dataset.getFields().stream() | ||
.map(f -> handleField(this.context, f)) | ||
.collect(Collectors.toList()); | ||
|
||
TypeSpec javaClass = TypeSpec.classBuilder(name) | ||
.addModifiers(Modifier.PUBLIC, Modifier.FINAL) | ||
.addFields(fields) | ||
.build(); | ||
|
||
return javaClass; | ||
} | ||
|
||
public FieldSpec handleField(Context context, Field field) { | ||
String name = field.getName(); | ||
Optional<Element> element = context.getElement(field.getElement()); | ||
ClassName className = element | ||
.map( e -> handleElementType(e.getElementType())) | ||
.orElse(ClassName.get(Object.class)); | ||
|
||
return FieldSpec.builder(className, name) | ||
.addModifiers(Modifier.PUBLIC, Modifier.FINAL) | ||
.build(); | ||
} | ||
|
||
public ClassName handleElementType(ElementType elementType) { | ||
String tipe = elementType.getAdditionalProperties().keySet().toArray()[0].toString(); | ||
|
||
// TODO | ||
switch(tipe) { | ||
case "Boolean": | ||
return ClassName.get(Boolean.class); | ||
case "Date": | ||
return ClassName.get(java.time.LocalDate.class); | ||
case "DateTime": | ||
return ClassName.get(java.time.LocalDateTime.class); | ||
case "Enum": | ||
String name = ("" + elementType.getAdditionalProperties().get("name")) | ||
.replaceAll("\\{", "").replaceAll("\\}", ""); | ||
return resolveReference(name); | ||
case "Number": | ||
return ClassName.get(Double.class); | ||
case "Reference": | ||
String ref = ("" + elementType.getAdditionalProperties().get("Reference")) | ||
.replaceAll("\\{", "").replaceAll("\\}", ""); | ||
return resolveReference(ref); | ||
case "Text": | ||
return ClassName.get(String.class); | ||
default: | ||
return ClassName.get(Object.class); | ||
} | ||
|
||
// if(cls == Enum.class) | ||
// return String.class; | ||
// else if(cls == Text.class) | ||
// return String.class; | ||
// else if(cls == Number.class) | ||
// return Double.class; | ||
//// else if(cls == Record.class) | ||
//// else if(cls == Reference.class) | ||
//// return String.class; | ||
// else if(cls == Text.class) | ||
// return String.class; | ||
// else | ||
// return Object.class; | ||
} | ||
|
||
public TypeSpec handleEnum(ElementType e) { | ||
String name = e.getAdditionalProperties().getOrDefault("name", "Enum").toString(); | ||
|
||
TypeSpec java = TypeSpec.enumBuilder(name) | ||
.addModifiers(Modifier.PUBLIC, Modifier.FINAL) | ||
.build(); | ||
|
||
return java; | ||
} | ||
|
||
public TypeSpec handleElement(Element element) { | ||
TypeSpec java = TypeSpec.classBuilder(element.getName()) | ||
.addModifiers(Modifier.PUBLIC, Modifier.FINAL) | ||
.build(); | ||
|
||
return java; | ||
} | ||
|
||
//// Utils //// | ||
public static ClassName resolveReference(String id) { | ||
ClassName className = idToClassName(id); | ||
ClassName result = className; | ||
|
||
switch (className.canonicalName()) { | ||
case "core.Integer": | ||
result = ClassName.get(Integer.class); | ||
break; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public static ClassName idToClassName(String id) { | ||
String[] split = id.split(":"); | ||
|
||
String pkg = escape(split[1]); | ||
String name = nameToJavaClassName(split[2]); | ||
|
||
return ClassName.get(pkg, name); | ||
} | ||
|
||
public static String escape(String s) { | ||
return s.replaceAll("\\W", "_"); | ||
} | ||
|
||
public static String nameToJavaClassName(String name) { | ||
return Arrays.stream(name.split("\\s+")) | ||
.map(JavaEmitter::capitalize) | ||
.collect(Collectors.joining("_")); | ||
} | ||
|
||
public static String capitalize(String s) { | ||
return s.substring(0,1).toUpperCase() + s.substring(1); | ||
} | ||
|
||
public static void main(String[] args) { | ||
final File baseDir = new File("sharing/example/metastore"); | ||
final Context context = new Context() { | ||
public Optional<Element> getElement(String elementId) { | ||
Optional<Element> e = FileStore.read(baseDir, elementId, Element.class); | ||
return e; | ||
} | ||
}; | ||
|
||
JavaEmitter emitter = new JavaEmitter(context); | ||
|
||
Dataset dataset = FileStore.read(baseDir, "dataset:/person:users", Dataset.class).get(); | ||
|
||
System.out.println(emitter.handleDataset(dataset)); | ||
|
||
// FileStore.readAllDatasets(baseDir).stream() | ||
// .map(emitter::handleDataset) | ||
// .forEach(System.out::println); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
DataThread/src/main/java/datathread/metastore/FileStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package datathread.metastore; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.*; | ||
import java.util.stream.Stream; | ||
|
||
public class FileStore { | ||
File baseDir; | ||
|
||
public FileStore(File baseDir) { | ||
this.baseDir = baseDir; | ||
} | ||
|
||
public Optional<Element> readElement(String elementId) { | ||
return read(baseDir, elementId, Element.class); | ||
} | ||
|
||
public static File resolveForID(String id) { | ||
String[] parts = id.split(":"); | ||
return resolveFile(parts[0], parts[1], parts[2]); | ||
} | ||
|
||
public static File resolveFile(String schemaType, String domain, String name) { | ||
return new File(domain, name + "." + schemaType + ".json"); | ||
} | ||
|
||
public static <T> List<T> readAll(File baseDir, String metaType, Class<T> type) { | ||
List<T> items = new ArrayList<>(); | ||
String pattern = metaType + ".json"; | ||
|
||
try (Stream<Path> paths = Files.walk(Paths.get(baseDir.getPath()))) { | ||
paths | ||
.filter(Files::isRegularFile) | ||
.filter(path -> path.getFileName().toString().endsWith(pattern)) | ||
.forEach(path -> { | ||
try { | ||
String json = new String(Files.readAllBytes(path)); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
T item = mapper.readValue(json, type); | ||
items.add(item); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return items; | ||
} | ||
|
||
public static List<Element> readAllElements(File baseDir) { | ||
return readAll(baseDir, "element", Element.class); | ||
} | ||
|
||
public static List<Dataset> readAllDatasets(File baseDir) { | ||
return readAll(baseDir, "dataset", Dataset.class); | ||
} | ||
|
||
public static <T> Optional<T> read(File baseDir, String id, Class<T> cls) { | ||
File elementFile = resolveForID(id); | ||
File absPath = new File(baseDir, elementFile.getPath()); | ||
|
||
try { | ||
String json = new String(Files.readAllBytes(Paths.get(absPath.getAbsolutePath()))); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
T item = mapper.readValue(json, cls); | ||
return Optional.of(item); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
File baseDir = new File("sharing/example/metastore"); | ||
|
||
|
||
System.out.println("==== Elements ===="); | ||
List<Element> elements = readAllElements(baseDir); | ||
for (Element element : elements) { | ||
System.out.println(element.getElementType()); | ||
} | ||
|
||
System.out.println("==== Datasets ===="); | ||
List<Dataset> datasets = readAllDatasets(baseDir); | ||
for (Dataset dataset : datasets) { | ||
System.out.println(dataset.getId()); | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|