Commit fa187fbd authored by Daniel Bluhm's avatar Daniel Bluhm

Simplify PojoFromJson, use DataElementMetadata directly

Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent d71d6c4a
......@@ -20,11 +20,11 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.ice.dev.annotations.processors.DataElementMetadata;
import org.eclipse.ice.dev.annotations.processors.DefaultFields;
import org.eclipse.ice.dev.annotations.processors.Fields;
import org.eclipse.ice.dev.annotations.processors.ImplementationWriter;
import org.eclipse.ice.dev.annotations.processors.InterfaceWriter;
import org.eclipse.ice.dev.annotations.processors.Types;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
......@@ -73,40 +73,26 @@ public class PojoFromJson {
*/
public static void handleInputJson(
InputStream is, Path destination
) throws JsonParseException, JsonMappingException, IOException {
) throws IOException {
// Parse outline from input stream
PojoOutline pojo = mapper.readValue(is, PojoOutline.class);
DataElementMetadata data = mapper.readValue(is, DataElementMetadata.class);
// Collect fields
Fields fields = new Fields();
Fields fields = data.getFields();
fields.collect(DefaultFields.get());
fields.collect(pojo.getFields());
// Write Interface
try (Writer elementInterface = Files.newBufferedWriter(
destination.resolve(pojo.getElement() + ".java")
destination.resolve(data.getName() + ".java")
)) {
InterfaceWriter.builder()
.packageName(pojo.getPackageName())
.interfaceName(pojo.getElement())
.fields(fields)
.types(new Types(fields.getInterfaceFields()))
.build()
.write(elementInterface);
new InterfaceWriter(data).write(elementInterface);
}
// Write implementation
try (Writer elementImpl = Files.newBufferedWriter(
destination.resolve(pojo.getImplementation() + ".java")
destination.resolve(data.getImplementationName() + ".java")
)) {
ImplementationWriter.builder()
.packageName(pojo.getPackageName())
.interfaceName(pojo.getElement())
.className(pojo.getImplementation())
.fields(fields)
.types(fields.getTypes())
.build()
.write(elementImpl);
new ImplementationWriter(data).write(elementImpl);
}
}
......
/*******************************************************************************
* Copyright (c) 2020- UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Daniel Bluhm - Initial implementation
*******************************************************************************/
package org.eclipse.ice.dev.pojofromjson;
import java.util.Collection;
import java.util.List;
import org.eclipse.ice.dev.annotations.processors.Field;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import lombok.Builder;
import lombok.Data;
import lombok.NonNull;
import lombok.Singular;
/**
* Representation of information read in via JSON for POJO generation.
* @author Daniel Bluhm
*/
@Data
@Builder
@JsonDeserialize(builder = PojoOutline.PojoOutlineBuilder.class)
public class PojoOutline {
/**
* Default suffix for implementation names.
*/
private static final String IMPL_SUFFIX = "Implementation";
/**
* Package of the generated classes.
*/
@JsonProperty("package")
@NonNull private String packageName;
/**
* Name of the Element (interface) to be generated.
*/
@NonNull private String element;
/**
* Name of the implementation to be generated. If null, element + IMPL_SUFFIX is
* used.
*/
private String implementation;
/**
* List of fields to generate on element.
*/
@Singular("field") private List<Field> fields;
/**
* Get name of the implementation to generate.
* @return implementation name.
*/
public String getImplementation() {
if (this.implementation == null) {
return this.element + IMPL_SUFFIX;
}
return this.implementation;
}
/**
* JSON Serialization info for Builder.
*/
private interface PojoOutlineBuilderMeta {
@JsonDeserialize(contentAs = Field.class)
public PojoOutlineBuilder fields(Collection<? extends Field> fields);
@JsonProperty("package")
public PojoOutlineBuilder packageName(@NonNull String package_);
}
/**
* JSON Serialization info for Builder.
*/
@JsonPOJOBuilder(withPrefix = "")
public static class PojoOutlineBuilder implements PojoOutlineBuilderMeta {}
}
/*******************************************************************************
* Copyright (c) 2020- UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Daniel Bluhm - Initial implementation
*******************************************************************************/
package org.eclipse.ice.tests.dev.pojofromjson;
import org.eclipse.ice.dev.annotations.processors.Field;
import org.eclipse.ice.dev.pojofromjson.PojoOutline;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class PojoOutlineTest {
@Test
public void testSerialization() throws JsonProcessingException {
PojoOutline pojo = PojoOutline.builder()
.packageName("testpackage")
.element("TestElement")
.field(
Field.builder()
.name("test")
.type(String.class)
.docString("Test docs.")
.match(false)
.primitive(true)
.nullable(true)
.build()
).build();
ObjectMapper mapper = new ObjectMapper();
String pojoJson = mapper.writeValueAsString(pojo);
System.out.println(pojoJson);
PojoOutline rehydratedPojo = mapper.readValue(pojoJson, PojoOutline.class);
assertEquals(pojo, rehydratedPojo);
}
@Test
public void testImplementation() {
PojoOutline pojo = PojoOutline.builder()
.packageName("testpackage")
.element("TestElement")
.field(
Field.builder()
.name("test")
.type(String.class)
.docString("Test docs.")
.match(false)
.primitive(true)
.nullable(true)
.getter(false)
.setter(false)
.build()
).build();
assertEquals("TestElementImplementation", pojo.getImplementation());
pojo = PojoOutline.builder()
.packageName("testpackage")
.element("TestElement")
.implementation("TestElementImpl")
.field(
Field.builder()
.name("test")
.type(String.class)
.docString("Test docs.")
.match(false)
.primitive(true)
.nullable(true)
.getter(false)
.setter(false)
.build()
).build();
assertEquals("TestElementImpl", pojo.getImplementation());
}
}
\ No newline at end of file
{
"package": "testpackage",
"element": "TestElement",
"name": "TestElement",
"fields": [{
"name": "test",
"type": "String"
......
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