Commit 78bb20dd authored by Johannes Mey's avatar Johannes Mey
Browse files

fi token and enum serialisation and add tests

parent 07fff375
Pipeline #3043 passed with stage
in 46 seconds
......@@ -84,6 +84,8 @@ aspect BackendAspect {
public void Program.generateAspect(StringBuilder sb) {
sb.append("import java.util.ArrayList;\n");
sb.append("import java.util.Collections;\n");
sb.append("import java.time.Instant;\n");
sb.append("import java.time.Period;\n");
sb.append("aspect RelAstAPI {\n");
for (TypeDecl td: getTypeDecls()) {
......@@ -1034,15 +1036,15 @@ aspect Serializer {
case "String":
sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "());\n");
break;
case "java.time.Instant":
sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "()).toString());\n");
case "Instant":
sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().toString());\n");
break;
case "java.time.Period":
sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "()).toString());\n");
case "Period":
sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().toString());\n");
break;
default:
// assume that the type is an enum. there is no way of checking this here
sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "()).name());\n");
sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().name());\n");
// sb.append("throw new DeserializationException(\"Unable to deserialize child node of type \"" + getTypeUse() + "\"\")")
}
}
......@@ -1192,7 +1194,7 @@ aspect Serializer {
case "Character":
sb.append(ind(indent) + "String chars = children.get(\"" + getID() + "\").asText();\n");
sb.append(ind(indent) + "if (chars.length() == 1) {\n");
sb.append(ind(indent + 2) + "element.set" + getID() + "(chars.charAt(0))\n");
sb.append(ind(indent + 2) + "element.set" + getID() + "(chars.charAt(0));\n");
sb.append(ind(indent) + "} else {\n");
sb.append(ind(indent + 2) + "throw new DeserializationException(\"unable to deserialize char '\" + chars + \"'\");\n");
sb.append(ind(indent) + "}\n");
......@@ -1200,15 +1202,15 @@ aspect Serializer {
case "String":
sb.append(ind(indent) + "element.set" + getID() + "(children.get(\"" + getID() + "\").asText());\n");
break;
case "java.time.Instant":
sb.append(ind(indent) + "element.set" + getID() + "(java.time.Instant.parse(children.get(\"" + getID() + "\").asText()));\n");
case "Instant":
sb.append(ind(indent) + "element.set" + getID() + "(Instant.parse(children.get(\"" + getID() + "\").asText()));\n");
break;
case "java.time.Period":
sb.append(ind(indent) + "element.set" + getID() + "(java.time.Period.parse(children.get(\"" + getID() + "\").asText()));\n");
case "Period":
sb.append(ind(indent) + "element.set" + getID() + "(Period.parse(children.get(\"" + getID() + "\").asText()));\n");
break;
default:
// assume that the type is an enum. there is no way of checking this here
sb.append(ind(indent) + "element.set" + getID() + "Enum.valueOf(" + type + ".getClass(), children.get(\"" + getID() + "\".asText())));\n");
sb.append(ind(indent) + "element.set" + getID() + "(Enum.valueOf(" + type + ".class, children.get(\"" + getID() + "\").asText()));\n");
// sb.append("throw new DeserializationException(\"Unable to deserialize child node of type \"" + getTypeUse() + "\"\")")
}
......
aspect Utils {
public String A.toString() {
return getName();
}
public String A.toString() {
return getName();
}
public String B.toString() {
return getName();
}
public String B.toString() {
return getName();
}
public enum Weekday {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
}
\ No newline at end of file
......@@ -2,7 +2,20 @@
Root ::= A* B* C;
A:NamedElement;
B:NamedElement;
C:NamedElement ::= D1:D [D2:D] D3:D* <F1:float> <F2:Float>;
C:NamedElement ::= D1:D [D2:D] D3:D*
<F1:float> <F2:Float>
<DD1:double> <DD2:Double>
<I1:int> <I2:Integer>
<S1:short> <S2:Short>
<L1:long> <L2:Long>
<B1:byte> <B2:Byte>
<O1:boolean> <O2:Boolean>
<C1:char> <C2:Character>
<T1:String>
<T2>
<N:Instant>
<P:Period>
<Day:Weekday>;
D:NamedElement;
abstract NamedElement ::= <Name>;
......
......@@ -5,6 +5,8 @@ import serializer.ast.*;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.time.Period;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -37,6 +39,34 @@ class Serializer {
c.setF1(42f);
c.setF2(42.13f);
c.setDD1(42.d);
c.setDD2(42.13d);
c.setI1(23);
c.setI2(42);
c.setS1((short)66);
c.setS2((short)77);
c.setL1(342l);
c.setL2(5453L);
c.setB1((byte)5);
c.setB2((byte)'c');
c.setO1(true);
c.setO2(false);
c.setC1('c');
c.setC2((char)5);
c.setT1("23");
c.setT2("dfjsv");
c.setN(Instant.now());
c.setP(Period.ofDays(1));
c.setDay(Weekday.FRIDAY);
r.setC(c);
// non-containment relations
......
Supports Markdown
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