Skip to content
Snippets Groups Projects
Commit 78bb20dd authored by Johannes Mey's avatar Johannes Mey
Browse files

fi token and enum serialisation and add tests

parent 07fff375
No related branches found
No related tags found
1 merge request!1Mquat2
Pipeline #3043 passed
...@@ -84,6 +84,8 @@ aspect BackendAspect { ...@@ -84,6 +84,8 @@ aspect BackendAspect {
public void Program.generateAspect(StringBuilder sb) { public void Program.generateAspect(StringBuilder sb) {
sb.append("import java.util.ArrayList;\n"); sb.append("import java.util.ArrayList;\n");
sb.append("import java.util.Collections;\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"); sb.append("aspect RelAstAPI {\n");
for (TypeDecl td: getTypeDecls()) { for (TypeDecl td: getTypeDecls()) {
...@@ -1034,15 +1036,15 @@ aspect Serializer { ...@@ -1034,15 +1036,15 @@ aspect Serializer {
case "String": case "String":
sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "());\n"); sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "());\n");
break; break;
case "java.time.Instant": case "Instant":
sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "()).toString());\n"); sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().toString());\n");
break; break;
case "java.time.Period": case "Period":
sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "()).toString());\n"); sb.append(ind(indent) + "g.writeStringField(\"" + getID() + "\", get" + getID() + "().toString());\n");
break; break;
default: default:
// assume that the type is an enum. there is no way of checking this here // 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() + "\"\")") // sb.append("throw new DeserializationException(\"Unable to deserialize child node of type \"" + getTypeUse() + "\"\")")
} }
} }
...@@ -1192,7 +1194,7 @@ aspect Serializer { ...@@ -1192,7 +1194,7 @@ aspect Serializer {
case "Character": case "Character":
sb.append(ind(indent) + "String chars = children.get(\"" + getID() + "\").asText();\n"); sb.append(ind(indent) + "String chars = children.get(\"" + getID() + "\").asText();\n");
sb.append(ind(indent) + "if (chars.length() == 1) {\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) + "} else {\n");
sb.append(ind(indent + 2) + "throw new DeserializationException(\"unable to deserialize char '\" + chars + \"'\");\n"); sb.append(ind(indent + 2) + "throw new DeserializationException(\"unable to deserialize char '\" + chars + \"'\");\n");
sb.append(ind(indent) + "}\n"); sb.append(ind(indent) + "}\n");
...@@ -1200,15 +1202,15 @@ aspect Serializer { ...@@ -1200,15 +1202,15 @@ aspect Serializer {
case "String": case "String":
sb.append(ind(indent) + "element.set" + getID() + "(children.get(\"" + getID() + "\").asText());\n"); sb.append(ind(indent) + "element.set" + getID() + "(children.get(\"" + getID() + "\").asText());\n");
break; break;
case "java.time.Instant": case "Instant":
sb.append(ind(indent) + "element.set" + getID() + "(java.time.Instant.parse(children.get(\"" + getID() + "\").asText()));\n"); sb.append(ind(indent) + "element.set" + getID() + "(Instant.parse(children.get(\"" + getID() + "\").asText()));\n");
break; break;
case "java.time.Period": case "Period":
sb.append(ind(indent) + "element.set" + getID() + "(java.time.Period.parse(children.get(\"" + getID() + "\").asText()));\n"); sb.append(ind(indent) + "element.set" + getID() + "(Period.parse(children.get(\"" + getID() + "\").asText()));\n");
break; break;
default: default:
// assume that the type is an enum. there is no way of checking this here // 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() + "\"\")") // sb.append("throw new DeserializationException(\"Unable to deserialize child node of type \"" + getTypeUse() + "\"\")")
} }
......
...@@ -6,4 +6,8 @@ aspect Utils { ...@@ -6,4 +6,8 @@ aspect Utils {
public String B.toString() { public String B.toString() {
return getName(); return getName();
} }
public enum Weekday {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
} }
\ No newline at end of file
...@@ -2,7 +2,20 @@ ...@@ -2,7 +2,20 @@
Root ::= A* B* C; Root ::= A* B* C;
A:NamedElement; A:NamedElement;
B: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; D:NamedElement;
abstract NamedElement ::= <Name>; abstract NamedElement ::= <Name>;
......
...@@ -5,6 +5,8 @@ import serializer.ast.*; ...@@ -5,6 +5,8 @@ import serializer.ast.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.time.Period;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -37,6 +39,34 @@ class Serializer { ...@@ -37,6 +39,34 @@ class Serializer {
c.setF1(42f); c.setF1(42f);
c.setF2(42.13f); 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); r.setC(c);
// non-containment relations // non-containment relations
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment