Commit 1b0f1382 authored by Daniel Bluhm's avatar Daniel Bluhm

Simplify writers by taking metadata directly

Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent 4ddedec2
......@@ -23,18 +23,54 @@ import lombok.Getter;
@Getter
@Builder
public class DataElementMetadata {
/**
* Suffix for Implementation name.
*/
private static final String IMPL_SUFFIX = "Implementation";
/**
* Base name of classes to be generated.
*/
protected String name;
private String name;
/**
* Package of classes to be generated.
*/
protected String packageName;
private String packageName;
/**
* Collected fields of the DataElement.
*/
protected Fields fields;
private Fields fields;
/**
* Fully qualified name of this DataElement.
* @return fully qualified name.
*/
public String getFullyQualifiedName() {
String value = null;
if (packageName != null) {
value = String.format("%s.%s", packageName, name);
} else {
value = name;
}
return value;
}
/**
* Name of implementation for this DataElement.
* @return Implementation name.
*/
public String getImplementationName() {
return name + IMPL_SUFFIX;
}
/**
* Fully qualified name of this DataElement's implementation.
* @return fully qualified implementation name.
*/
public String getFullyQualifiedImplName() {
return getFullyQualifiedName() + IMPL_SUFFIX;
}
}
\ No newline at end of file
......@@ -42,26 +42,10 @@ public class DataElementWriterGenerator implements WriterGenerator {
@Override
public List<GeneratedFileWriter> generate() {
List<GeneratedFileWriter> writers = new ArrayList<>();
Fields nonDefaults = data.getFields().getNonDefaultFields();
writers.add(InterfaceWriter.builder()
.packageName(data.getPackageName())
.interfaceName(data.getName())
.fields(nonDefaults)
.types(nonDefaults.getTypes())
.build());
writers.add(ImplementationWriter.builder()
.packageName(data.getPackageName())
.interfaceName(data.getName())
.className(data.getName() + "Implementation")
.fields(data.getFields())
.types(data.getFields().getTypes())
.build());
writers.add(new InterfaceWriter(data));
writers.add(new ImplementationWriter(data));
try {
writers.add(TypeScriptWriter.builder()
.name(data.getName())
.fields(nonDefaults)
.types(nonDefaults.getTypes())
.build());
writers.add(new TypeScriptWriter(data));
} catch (UnsupportedOperationException e) {
logger.info("Failed to create typescript writer for element:", e);
}
......
......@@ -12,9 +12,6 @@
package org.eclipse.ice.dev.annotations.processors;
import lombok.Builder;
import lombok.NonNull;
import java.io.IOException;
import java.io.Writer;
......@@ -74,22 +71,14 @@ public class ImplementationWriter
* @param fields
* @param generatedFile
*/
@Builder
public ImplementationWriter(
String packageName, String interfaceName, String className,
@NonNull Fields fields, @NonNull Types types
) {
public ImplementationWriter(DataElementMetadata data) {
super(IMPL_TEMPLATE);
if (packageName != null) {
this.fqn = String.format("%s.%s", packageName, className);
} else {
this.fqn = className;
}
this.context.put(PACKAGE, packageName);
this.context.put(INTERFACE, interfaceName);
this.context.put(CLASS, className);
this.context.put(FIELDS, fields);
this.context.put(TYPES, types);
this.fqn = data.getFullyQualifiedImplName();
this.context.put(PACKAGE, data.getPackageName());
this.context.put(INTERFACE, data.getName());
this.context.put(CLASS, data.getImplementationName());
this.context.put(FIELDS, data.getFields());
this.context.put(TYPES, data.getFields().getTypes());
}
@Override
......
......@@ -17,9 +17,6 @@ import java.io.Writer;
import javax.annotation.processing.Filer;
import lombok.Builder;
import lombok.NonNull;
/**
* Writer for DataElement Interfaces.
*
......@@ -72,21 +69,16 @@ public class InterfaceWriter
* @param fields
* @param generatedFile
*/
@Builder
public InterfaceWriter(
String packageName, String interfaceName, @NonNull Fields fields,
@NonNull Types types
DataElementMetadata data
) {
super(TEMPLATE);
if (packageName != null) {
this.fqn = String.format("%s.%s", packageName, interfaceName);
} else {
this.fqn = interfaceName;
}
context.put(PACKAGE, packageName);
context.put(INTERFACE, interfaceName);
this.fqn = data.getFullyQualifiedName();
Fields fields = data.getFields().getNonDefaultFields();
context.put(PACKAGE, data.getPackageName());
context.put(INTERFACE, data.getName());
context.put(FIELDS, fields);
context.put(TYPES, types);
context.put(TYPES, fields.getTypes());
}
@Override
......
......@@ -17,9 +17,6 @@ import java.io.Writer;
import javax.annotation.processing.Filer;
import lombok.Builder;
import lombok.NonNull;
/**
* Writer for DataElement Persistence classes.
*
......@@ -45,11 +42,6 @@ public class PersistenceHandlerWriter
*/
private static final String CLASS = "class";
/**
* Context key for interface of PersistenceHandlers
*/
private static final String INTERFACE = "interface";
/**
* Context key for collection.
*/
......@@ -79,38 +71,27 @@ public class PersistenceHandlerWriter
* Fully qualified name of the class for file output.
*/
private String fqn;
/**
* Constructor
*
* @param packageName
* @param elementInterface
* @param className
* @param interfaceName
* @param implementation
* @param collection
* @param fields
* @param generatedFile
* Create instance of persistence handler writer from metadata.
* @param dataElement DataElementMetadata
* @param persistence PersistenceMetadata
*/
@Builder
public PersistenceHandlerWriter(
String packageName, String elementInterface, String className, String
interfaceName, String implementation, String collection, @NonNull Fields
fields, @NonNull Types types
DataElementMetadata dataElement,
PersistenceMetadata persistence
) {
super(TEMPLATE);
if (packageName != null) {
this.fqn = String.format("%s.%s", packageName, className);
} else {
this.fqn = className;
}
this.context.put(PACKAGE, packageName);
this.context.put(ELEMENT_INTERFACE, elementInterface);
this.context.put(CLASS, className);
this.context.put(INTERFACE, interfaceName);
this.context.put(COLLECTION, collection);
this.context.put(IMPLEMENTATION, implementation);
this.context.put(FIELDS, fields);
this.context.put(TYPES, types);
this.fqn = persistence.getHandlerName(
dataElement.getFullyQualifiedName()
);
this.context.put(PACKAGE, dataElement.getPackageName());
this.context.put(ELEMENT_INTERFACE, dataElement.getName());
this.context.put(CLASS, persistence.getHandlerName(dataElement.getName()));
this.context.put(COLLECTION, persistence.getCollection());
this.context.put(IMPLEMENTATION, dataElement.getImplementationName());
this.context.put(FIELDS, dataElement.getFields());
this.context.put(TYPES, dataElement.getFields().getTypes());
}
@Override
......
......@@ -21,8 +21,22 @@ import lombok.Getter;
@Getter
@Builder
public class PersistenceMetadata {
/**
* Suffix to PersistenceHandler class names.
*/
private static final String SUFFIX = "PersistenceHandler";
/**
* Collection name for persistence.
*/
private String collection;
/**
* Get Persistence Handler class name using data element metadata.
* @param dataElementData DataElementMetadata
* @return persistence handler class name.
*/
public String getHandlerName(String dataElementName) {
return dataElementName + SUFFIX;
}
}
......@@ -35,17 +35,7 @@ public class PersistenceWriterGenerator implements WriterGenerator {
@Override
public List<GeneratedFileWriter> generate() {
List<GeneratedFileWriter> writers = List.of(
PersistenceHandlerWriter.builder()
.packageName(dataElement.getPackageName())
.elementInterface(dataElement.getName())
.className(dataElement.getName() + "PersistenceHandler")
// TODO Just move interface name into template (it's static)
.interfaceName("IPersistenceHandler")
.implementation(dataElement.getName()+ "Implementation")
.collection(persistence.getCollection())
.fields(dataElement.getFields())
.types(dataElement.getFields().getTypes())
.build()
new PersistenceHandlerWriter(dataElement, persistence)
);
return writers;
}
......
......@@ -18,9 +18,6 @@ import java.util.Map;
import javax.annotation.processing.Filer;
import javax.tools.StandardLocation;
import lombok.Builder;
import lombok.NonNull;
/**
* Writer for TypeScript representation of DataElement.
* @author Daniel Bluhm
......@@ -80,11 +77,11 @@ public class TypeScriptWriter
* @param types of fields.
* @throws UnsupportedOperationException When any field is not supported.
*/
@Builder
public TypeScriptWriter(
String name, @NonNull Fields fields, @NonNull Types types
DataElementMetadata data
) {
super(TYPESCRIPT_TEMPLATE);
Fields fields = data.getFields().getNonDefaultFields();
for (Field field : fields) {
if (!primitiveMap.containsKey(field.getType())) {
throw new UnsupportedOperationException(String.format(
......@@ -93,10 +90,10 @@ public class TypeScriptWriter
));
}
}
this.filename = name;
this.context.put(NAME, name);
this.filename = data.getName();
this.context.put(NAME, data.getName());
this.context.put(FIELDS, fields);
this.context.put(TYPES, types);
this.context.put(TYPES, fields.getTypes());
this.context.put(PRIMITIVE_MAP, primitiveMap);
}
......
......@@ -66,7 +66,7 @@ public abstract class VelocitySourceWriter implements FileWriter {
/**
* Template for writing. Should be filled by concrete classes.
*/
protected String template;
private String template;
/**
* Context for template. Should be filled by concrete classes.
......
......@@ -13,11 +13,11 @@ import com.mongodb.client.MongoDatabase;
#imports
/**
* This is an implementation of $interface<$elementInterface>
* This is an implementation of IPersistenceHandler<$elementInterface>
* that satisfies the dependencies of the {@code @Persisted} Annotation and was
* auto-generated by the ICE Framework.
*/
public class $class implements $interface<$elementInterface> {
public class $class implements IPersistenceHandler<$elementInterface> {
/**
* The name of the collection backing this PersistenceHandler.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment