Commit d71d6c4a authored by Daniel Bluhm's avatar Daniel Bluhm

Make DataElementMetadata JSON (de)serializable

Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent 1b0f1382
......@@ -11,7 +11,13 @@
package org.eclipse.ice.dev.annotations.processors;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
/**
......@@ -20,8 +26,16 @@ import lombok.Getter;
*
* @author Daniel Bluhm
*/
@Getter
@Builder
@Getter
@EqualsAndHashCode
@JsonDeserialize(builder = DataElementMetadata.DataElementMetadataBuilder.class)
@JsonAutoDetect(
fieldVisibility = Visibility.ANY,
getterVisibility = Visibility.NONE,
isGetterVisibility = Visibility.NONE,
setterVisibility = Visibility.NONE
)
public class DataElementMetadata {
/**
......@@ -32,16 +46,19 @@ public class DataElementMetadata {
/**
* Base name of classes to be generated.
*/
@JsonProperty
private String name;
/**
* Package of classes to be generated.
*/
@JsonProperty("package")
private String packageName;
/**
* Collected fields of the DataElement.
*/
@JsonProperty
private Fields fields;
/**
......
......@@ -17,20 +17,27 @@ import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.EqualsAndHashCode;
/**
* A collection of Field objects to be used especially in template rendering.
*
* @author Daniel Bluhm
*/
@EqualsAndHashCode
public class Fields implements Iterable<Field> {
/**
* The collection of fields on which views will be retrieved.
*/
private List<Field> fields;
@JsonValue
private List<Field> list;
public Fields() {
this.fields = new ArrayList<>();
this.list = new ArrayList<>();
}
/**
......@@ -38,9 +45,10 @@ public class Fields implements Iterable<Field> {
*
* @param fields initial fields
*/
@JsonCreator
public Fields(Collection<Field> fields) {
this.fields = new ArrayList<>();
this.fields.addAll(fields);
this.list = new ArrayList<>();
this.list.addAll(fields);
}
/**
......@@ -49,7 +57,7 @@ public class Fields implements Iterable<Field> {
* @param fields to add
*/
public void collect(Collection<Field> fields) {
this.fields.addAll(fields);
this.list.addAll(fields);
}
/**
......@@ -59,7 +67,7 @@ public class Fields implements Iterable<Field> {
* @see Field#isConstant()
*/
public Iterator<Field> getConstants() {
return fields.stream()
return list.stream()
.filter(Field::isConstant)
.iterator();
}
......@@ -71,7 +79,7 @@ public class Fields implements Iterable<Field> {
* @see Field#isConstant()
*/
public Iterator<Field> getMutable() {
return fields.stream()
return list.stream()
.filter(field -> !field.isConstant())
.iterator();
}
......@@ -83,7 +91,7 @@ public class Fields implements Iterable<Field> {
* @see org.eclipse.ice.data.IDataElement#matches(Object)
*/
public Iterator<Field> getMatch() {
return fields.stream()
return list.stream()
.filter(Field::isMatch)
.iterator();
}
......@@ -96,7 +104,7 @@ public class Fields implements Iterable<Field> {
* @see Field#isVarDifferent()
*/
public Iterator<Field> getVarNamesDiffer() {
return fields.stream()
return list.stream()
.filter(Field::isVarNameDifferent)
.iterator();
}
......@@ -106,7 +114,7 @@ public class Fields implements Iterable<Field> {
* @return Iterable of fields needed for interface.
*/
public Iterable<Field> getInterfaceFields() {
return fields.stream()
return list.stream()
.filter(field -> !field.isDefaultField())
.collect(Collectors.toList());
}
......@@ -117,7 +125,7 @@ public class Fields implements Iterable<Field> {
*/
public Fields getNonDefaultFields() {
return new Fields(
fields.stream()
list.stream()
.filter(field -> !field.isDefaultField())
.collect(Collectors.toList())
);
......
/*******************************************************************************
* 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.annotations.processors;
import org.eclipse.ice.dev.annotations.processors.DataElementMetadata;
import org.eclipse.ice.dev.annotations.processors.Field;
import org.eclipse.ice.dev.annotations.processors.Fields;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import org.junit.jupiter.api.Test;
class DataElementMetadataTest {
@Test
void testSerialization() throws JsonProcessingException {
DataElementMetadata pojo = DataElementMetadata.builder()
.packageName("testpackage")
.name("TestElement")
.fields(new Fields(List.of(
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);
DataElementMetadata rehydratedPojo = mapper.readValue(pojoJson, DataElementMetadata.class);
assertEquals(pojo, rehydratedPojo);
}
@Test
void testImplementation() {
DataElementMetadata pojo = DataElementMetadata.builder()
.packageName("testpackage")
.name("TestElement")
.fields(new Fields(List.of(
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.getImplementationName());
}
}
\ No newline at end of file
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