Skip to content

Commit

Permalink
fixing up the java library
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengoldbaum committed Jul 20, 2024
1 parent 0059fb7 commit 672dfcf
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 143 deletions.
1 change: 1 addition & 0 deletions DataThread/src/main/grammar/main.tsp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "./Element.tsp";
import "./ElementInfo.tsp";
import "./Dataset.tsp";

95 changes: 53 additions & 42 deletions DataThread/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,63 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ms.data.casc.manifest.model.SystemDataElement;
import com.squareup.javapoet.TypeSpec;
import datathread.metastore.Element;
import datathread.Identifier;
import datathread.backends.JavaEmitter;
import datathread.metastore.FileStore;
import datathread.metastore.Metastore;
import datathread.metastore.Router;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.File;

import javax.lang.model.element.Modifier;
import java.util.Map;
import java.util.Optional;

public class Main {
public static void main(String[] args) {
String json = null;
try {
System.out.println(new File(".").getAbsolutePath());
File baseDir = new File("sharing/example/metastore");
String id = "element:person:active";
File file = new File(baseDir, resolveForID(id).getPath());

json = new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
} catch (IOException e) {
e.printStackTrace();
}
ObjectMapper mapper = new ObjectMapper();

try {
Element element = mapper.readValue(json, Element.class);
TypeSpec java = elementToJava(element);
System.out.println(java);
} catch (IOException e) {
e.printStackTrace();
}
}

public static TypeSpec elementToJava(Element element) {
TypeSpec java = TypeSpec.classBuilder(element.getName())
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.build();

return java;
}
public static void main(String[] args) {

public static File resolveForID(String id) {
String[] parts = id.split(":");
return resolveFile(parts[0], parts[1], parts[2]);
// Create separate stores for Dataset, Element, and ElementInfo
Metastore exampleMetastore = new FileStore(Paths.get("DataThread", "example", "metastore", "automated"));
final Metastore buildMetastore = new FileStore(Paths.get("DataThread", "build", "metastore", "automated"));

// Setup the router
final Router router = new Router(Map.of(
Element.class, exampleMetastore
, ElementInfo.class, buildMetastore
, Dataset.class, exampleMetastore
));

// Setup the test domain and subject
final String[] domain = {"person"};
final Identifier id = new Identifier("element", domain, "age");

// Read Age Element from examples
Optional<Element> age = router.read(id, Element.class);
System.out.println("Element: " + age);

// Augment Age Element by saving a related ElementInfo
ElementInfo info = new ElementInfo();
info.setId(Identifier.toString(id));
info.setDescription("The age of the person in years");
info.setDisplayName("Age");
Optional<String> response = router.write(id, ElementInfo.class, info);
System.out.println("Response: " + response);

// Read it back
Optional<ElementInfo> outfo = router.read(id, ElementInfo.class);
System.out.println("OUtfo: " + outfo);

// Write to Java
JavaEmitter javaEmitter = new JavaEmitter(router);
Optional<TypeSpec> java = age.map(e -> javaEmitter.handleElement(e));
System.out.println("Element Java:");
System.out.println(java);

// Again with a Dataset
Optional<Dataset> people = router.read(new Identifier("dataset", domain, "people"), Dataset.class);
Optional<TypeSpec> javaD = people
.map(o -> javaEmitter.handleDataset(o));
System.out.println();
System.out.println("Dataset Java:");
System.out.println(javaD);
}

public static File resolveFile(String schemaType, String domain, String name) {
return new File(domain, name + "." + schemaType + ".json");
}
}
8 changes: 8 additions & 0 deletions DataThread/src/main/java/datathread/Identifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public static Optional<Identifier> from(String urn) {
return Optional.empty();
}
}

public static String toString(Identifier id) {
String scheme = id.scheme();
String domain = String.join("/", id.domain());
String name = id.name();

return scheme + ":" + domain + ":" + name;
}
}
49 changes: 12 additions & 37 deletions DataThread/src/main/java/datathread/backends/JavaEmitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,26 @@
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.TypeSpec;
import datathread.Identifier;
import datathread.metastore.*;
import datathread.metastore.Metastore;

import javax.lang.model.element.Modifier;
import java.io.File;
import java.nio.file.Path;
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;
private final Metastore context;

public JavaEmitter(Context context) {
public JavaEmitter(Metastore context) {
this.context = context;
}

public TypeSpec handleDataset(Dataset dataset) {
String name = nameToJava(dataset.getName());
List<FieldSpec> fields =
dataset.getFields().stream()
.map(f -> handleField(this.context, f))
.map(field -> handleField(field))
.collect(Collectors.toList());

TypeSpec javaClass = TypeSpec.classBuilder(name)
Expand All @@ -40,12 +34,16 @@ public TypeSpec handleDataset(Dataset dataset) {
return javaClass;
}

public FieldSpec handleField(Context context, Field field) {
public FieldSpec handleField(Field field) {
String name = field.getName();
Optional<Element> element = context.getElement(field.getElement());
Optional<Element> element =
Identifier.from(field.getElement())
.flatMap(id -> context.read(id, Element.class))
.map(Element.class::cast); // Cast the Optional<Object> to Optional<Element>

ClassName className = element
.map( e -> handleElementType(e.getElementType()))
.orElse(ClassName.get(Object.class));
.map(e -> handleElementType(e.getElementType()))
.orElse(ClassName.get(Object.class));

return FieldSpec.builder(className, name)
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
Expand Down Expand Up @@ -127,27 +125,4 @@ public static String nameToJava(String name) {
public static String capitalize(String s) {
return s.substring(0,1).toUpperCase() + s.substring(1);
}

public static void main(String[] args) {
final Path baseDir = Path.of("DataThread","example","metastore", "automated");
final FileStore fileStore = new FileStore(baseDir);

final Context context = new Context() {
public Optional<Element> getElement(String elementId) {
return Identifier.from(elementId)
.flatMap(id -> fileStore.resolveAndRead(id, Element.class));
}
};

JavaEmitter emitter = new JavaEmitter(context);
Optional<Identifier> datasetId = Identifier.from("dataset:/person:users");
Dataset dataset = datasetId
.flatMap(id -> fileStore.resolveAndRead(id, Dataset.class))
.orElse(null);
System.out.println(emitter.handleDataset(dataset));

// FileStore.readAllDatasets(baseDir).stream()
// .map(emitter::handleDataset)
// .forEach(System.out::println);
}
}
35 changes: 6 additions & 29 deletions DataThread/src/main/java/datathread/metastore/FileStore.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package datathread.metastore;

import com.fasterxml.jackson.databind.ObjectMapper;
import datathread.Identifier;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -9,23 +12,19 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.ObjectMapper;

import datathread.Identifier;

public class FileStore implements Metastore {
Path baseDir;

public FileStore(Path baseDir) {
this.baseDir = baseDir;
}

public <T> Optional<T> resolveAndRead(Identifier id, Class<T> tipe) {
public <T> Optional<T> read(Identifier id, Class<T> tipe) {
Path filePath = resolveForID(this.baseDir, id, tipe);
return FileStore.loadFromFile(filePath, tipe);
}

public <T> List<T> findAllAndRead(Class<T> metaType) {
public <T> List<T> readAll(Class<T> metaType) {
String fileSuffix = FileStore.classNameToFileStyle(metaType);
List<T> results = Collections.emptyList();

Expand Down Expand Up @@ -76,7 +75,7 @@ public static Path resolveForID(Path baseDir, Identifier id, Class tipe) {

public static Path resolveFile(Path baseDir, String schema, String[] domain, String name, String fileSuffix) {
Path path = Path.of(".", domain).resolve(name + "." + fileSuffix + ".json");
Path absPath = baseDir.resolve(schema).resolve(path);
Path absPath = baseDir.resolve(path);
return absPath;
}

Expand All @@ -93,26 +92,4 @@ public static <T> Optional<T> loadFromFile(Path path, Class<T> tipe) {
return Optional.empty();
}
}

public static void main(String[] args) {
// Create a FileStore instance with the base directory
Path baseDir = Path.of("DataThread","example","metastore","automated");
FileStore fileStore = new FileStore(baseDir);

// Test the findAllAndRead method
List<Element> results = fileStore.findAllAndRead(Element.class);
for (Element result : results) {
System.out.println(result);
}

// resolveForID
String scheme = "scheme";
String[] domain = {"domain"};
String name = "name";
Identifier id = new Identifier(scheme, domain, name);

Path actual = FileStore.resolveForID(baseDir, id, Element.class).normalize();

System.out.println(actual);
}
}
9 changes: 5 additions & 4 deletions DataThread/src/main/java/datathread/metastore/Metastore.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package datathread.metastore;

import java.util.*;

import datathread.Identifier;

import java.util.List;
import java.util.Optional;


public interface Metastore {
public <T> Optional<T> resolveAndRead(Identifier id, Class<T> tipe);
public <T> Optional<T> read(Identifier id, Class<T> tipe);

public <T> List<T> findAllAndRead(Class<T> tipe);
public <T> List<T> readAll(Class<T> tipe);

public <T> Optional<String> write(Identifier id, Class<T> tipe, T data);
}
40 changes: 9 additions & 31 deletions DataThread/src/main/java/datathread/metastore/Router.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package datathread.metastore;

import java.util.*;

import datathread.Identifier;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class Router implements Metastore {
private final Map<Class, Metastore> routes;

public Router(Map<Class, Metastore> routes) {
this.routes = routes;
}

public <T> Optional<T> resolveAndRead(Identifier id, Class<T> tipe) {
public <T> Optional<T> read(Identifier id, Class<T> tipe) {
return Optional.ofNullable(this.routes.get(tipe))
.flatMap(h -> h.resolveAndRead(id, tipe))
.flatMap(h -> h.read(id, tipe))
.filter(tipe::isInstance)
.map(tipe::cast);
}

public <T> List<T> findAllAndRead(Class<T> tipe) {
public <T> List<T> readAll(Class<T> tipe) {
return Optional.ofNullable(this.routes.get(tipe))
.map(h -> h.findAllAndRead(tipe))
.map(h -> h.readAll(tipe))
.orElse(Collections.emptyList());
}

Expand All @@ -31,29 +34,4 @@ public <T> Optional<String> write(Identifier id, Class<T> tipe, T data) {
? Optional.of("No storage handler for type " + tipe)
: handler.write(id, tipe, data);
}

public static void main(String[] args) {
final Class stringClass = String.class;

final Metastore m1 = new Metastore() {
public <T> Optional<T> resolveAndRead(Identifier id, Class<T> tipe) {
return Optional.empty();
}

public <T> List<T> findAllAndRead(Class<T> tipe) {
return Collections.emptyList();
}

public <T> Optional<String> write(Identifier id, Class<T> tipe, T data) {
return tipe == stringClass ? Optional.empty() : Optional.of("No storage handler for type " + tipe);
}
};

final Router router = new Router(Map.of(stringClass, m1));
final String[] domain = {"domain1", "domain2"};
final Identifier id = new Identifier("element", domain, "Name1");
Object o = router.write(id, stringClass, "hello");
System.out.println(o);

}
}

0 comments on commit 672dfcf

Please sign in to comment.