This commit is contained in:
Andrea Cavalli 2023-01-21 23:58:56 +01:00
parent bdf037da06
commit f18ad32b87
4 changed files with 26 additions and 10 deletions

View File

@ -1,7 +1,11 @@
package it.cavallium.data.generator.plugin;
import com.squareup.javapoet.ClassName;
public sealed interface ComputedTypeArray extends ComputedType permits ComputedTypeArrayFixed, ComputedTypeArrayNative,
ComputedTypeArrayVersioned {
ComputedType getBase();
ClassName getJSerializerName(String basePackageName);
}

View File

@ -1,7 +1,12 @@
package it.cavallium.data.generator.plugin;
import com.squareup.javapoet.ClassName;
public sealed interface ComputedTypeNullable extends ComputedType permits ComputedTypeNullableFixed,
ComputedTypeNullableNative, ComputedTypeNullableVersioned {
ComputedType getBase();
@Override
ClassName getJSerializerName(String basePackageName);
}

View File

@ -10,6 +10,8 @@ import com.squareup.javapoet.TypeSpec.Builder;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.data.generator.nativedata.ImmutableWrappedArrayList;
import it.cavallium.data.generator.plugin.ClassGenerator;
import it.cavallium.data.generator.plugin.ComputedTypeArray;
import it.cavallium.data.generator.plugin.ComputedTypeArrayFixed;
import it.cavallium.data.generator.plugin.ComputedTypeArrayVersioned;
import it.cavallium.data.generator.plugin.ComputedVersion;
import java.io.DataInput;
@ -35,11 +37,12 @@ public class GenSerializerArrayX extends ClassGenerator {
private Stream<GeneratedClass> generateVersionClasses(ComputedVersion version) {
return dataModel
.getArrayTypesComputed(version)
.filter(type -> type instanceof ComputedTypeArrayVersioned versioned && versioned.getVersion().equals(version))
.map(type -> generateTypeVersioned(version, (ComputedTypeArrayVersioned) type));
.filter(type -> (type instanceof ComputedTypeArrayVersioned versioned
&& versioned.getVersion().equals(version)) || type instanceof ComputedTypeArrayFixed)
.map(type -> generateTypeVersioned(version, type));
}
private GeneratedClass generateTypeVersioned(ComputedVersion version, ComputedTypeArrayVersioned typeArray) {
private GeneratedClass generateTypeVersioned(ComputedVersion version, ComputedTypeArray typeArray) {
ClassName serializerClassName = typeArray.getJSerializerName(basePackageName);
var typeArrayClassName = typeArray.getJTypeName(basePackageName);
@ -56,7 +59,7 @@ public class GenSerializerArrayX extends ClassGenerator {
return new GeneratedClass(serializerClassName.packageName(), classBuilder);
}
private void generateSerialize(ComputedVersion version, ComputedTypeArrayVersioned typeArray, Builder classBuilder) {
private void generateSerialize(ComputedVersion version, ComputedTypeArray typeArray, Builder classBuilder) {
var method = MethodSpec.methodBuilder("serialize");
method.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
@ -89,7 +92,7 @@ public class GenSerializerArrayX extends ClassGenerator {
classBuilder.addMethod(method.build());
}
private void generateDeserialize(ComputedVersion version, ComputedTypeArrayVersioned typeArray, Builder classBuilder) {
private void generateDeserialize(ComputedVersion version, ComputedTypeArray typeArray, Builder classBuilder) {
var method = MethodSpec.methodBuilder("deserialize");
method.addModifiers(Modifier.PUBLIC, Modifier.FINAL);

View File

@ -8,6 +8,8 @@ import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeSpec.Builder;
import it.cavallium.data.generator.DataSerializer;
import it.cavallium.data.generator.plugin.ClassGenerator;
import it.cavallium.data.generator.plugin.ComputedTypeNullable;
import it.cavallium.data.generator.plugin.ComputedTypeNullableFixed;
import it.cavallium.data.generator.plugin.ComputedTypeNullableVersioned;
import it.cavallium.data.generator.plugin.ComputedVersion;
import java.io.DataInput;
@ -33,11 +35,13 @@ public class GenSerializerNullableX extends ClassGenerator {
private Stream<GeneratedClass> generateVersionClasses(ComputedVersion version) {
return dataModel
.getNullableTypesComputed(version)
.filter(type -> type instanceof ComputedTypeNullableVersioned versioned && versioned.getVersion().equals(version))
.map(type -> generateTypeVersioned(version, (ComputedTypeNullableVersioned) type));
.filter(type -> (
(type instanceof ComputedTypeNullableVersioned versioned && versioned.getVersion().equals(version))
|| type instanceof ComputedTypeNullableFixed))
.map(type -> generateTypeVersioned(version, type));
}
private GeneratedClass generateTypeVersioned(ComputedVersion version, ComputedTypeNullableVersioned typeNullable) {
private GeneratedClass generateTypeVersioned(ComputedVersion version, ComputedTypeNullable typeNullable) {
ClassName serializerClassName = typeNullable.getJSerializerName(basePackageName);
var typeNullableClassName = typeNullable.getJTypeName(basePackageName);
@ -54,7 +58,7 @@ public class GenSerializerNullableX extends ClassGenerator {
return new GeneratedClass(serializerClassName.packageName(), classBuilder);
}
private void generateSerialize(ComputedVersion version, ComputedTypeNullableVersioned typeNullable, Builder classBuilder) {
private void generateSerialize(ComputedVersion version, ComputedTypeNullable typeNullable, Builder classBuilder) {
var method = MethodSpec.methodBuilder("serialize");
var base = typeNullable.getBase();
@ -89,7 +93,7 @@ public class GenSerializerNullableX extends ClassGenerator {
classBuilder.addMethod(method.build());
}
private void generateDeserialize(ComputedVersion version, ComputedTypeNullableVersioned typeNullable, Builder classBuilder) {
private void generateDeserialize(ComputedVersion version, ComputedTypeNullable typeNullable, Builder classBuilder) {
var method = MethodSpec.methodBuilder("deserialize");
var base = typeNullable.getBase();