This commit is contained in:
Andrea Cavalli 2023-01-21 22:48:32 +01:00
parent 7b2fbe04d8
commit 539a9d7fd5
5 changed files with 261 additions and 7 deletions

View File

@ -1,6 +1,6 @@
package it.cavallium.data.generator.plugin;
public sealed interface ComputedTypeArray extends ComputedType permits ComputedTypeArrayNative,
public sealed interface ComputedTypeArray extends ComputedType permits ComputedTypeArrayFixed, ComputedTypeArrayNative,
ComputedTypeArrayVersioned {
ComputedType getBase();

View File

@ -0,0 +1,120 @@
package it.cavallium.data.generator.plugin;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import it.cavallium.data.generator.nativedata.UpgradeUtil;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
public final class ComputedTypeArrayFixed implements ComputedTypeArray {
private final String baseType;
private final ComputedVersion currentVersion;
private ComputedType computedChild;
private final ComputedTypeSupplier computedTypeSupplier;
public ComputedTypeArrayFixed(String baseType, ComputedVersion currentVersion, ComputedTypeSupplier computedTypeSupplier) {
this.baseType = baseType;
this.currentVersion = currentVersion;
this.computedTypeSupplier = computedTypeSupplier;
}
public ComputedType getBase() {
return child();
}
public ComputedType child() {
synchronized (this) {
if (computedChild == null) {
computedChild = computedTypeSupplier.get(baseType);
if (computedChild instanceof ComputedTypeNullableVersioned) {
throw new IllegalStateException();
} else if (computedChild instanceof ComputedTypeArrayFixed) {
throw new IllegalStateException();
}
}
}
return computedChild;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ComputedTypeArrayFixed that = (ComputedTypeArrayFixed) o;
return Objects.equals(baseType, that.baseType);
}
@Override
public int hashCode() {
return baseType != null ? baseType.hashCode() : 0;
}
@Override
public String getName() {
return "§" + baseType;
}
@Override
public TypeName getJTypeName(String basePackageName) {
return ParameterizedTypeName.get(ClassName.get(List.class),
computedTypeSupplier.get(baseType).getJTypeName(basePackageName));
}
@Override
public ClassName getJSerializerName(String basePackageName) {
return ClassName.get(currentVersion.getSerializersPackage(basePackageName), "Array" + baseType + "Serializer");
}
@Override
public FieldLocation getJSerializerInstance(String basePackageName) {
var className = ClassName.get(currentVersion.getPackage(basePackageName), "Version");
var serializerFieldName = "Array" + baseType + "SerializerInstance";
return new FieldLocation(className, serializerFieldName);
}
@Override
public TypeName getJUpgraderName(String basePackageName) {
throw new UnsupportedOperationException("Not upgradable");
}
@Override
public FieldLocation getJUpgraderInstance(String basePackageName) {
throw new UnsupportedOperationException("Not upgradable");
}
@Override
public CodeBlock wrapWithUpgrade(String basePackageName, CodeBlock content, ComputedType next) {
var builder = CodeBlock.builder();
builder.add("$T.upgradeArray(", UpgradeUtil.class);
builder.add(content);
var upgraderInstance = getBase().getJUpgraderInstance(basePackageName);
builder.add(", $T.$N)", upgraderInstance.className(), upgraderInstance.fieldName());
return builder.build();
}
@Override
public Stream<ComputedType> getDependencies() {
return Stream.of(child());
}
@Override
public Stream<ComputedType> getDependents() {
return computedTypeSupplier.getDependents(getName());
}
@Override
public String toString() {
return baseType + "[] (custom)";
}
}

View File

@ -1,7 +1,7 @@
package it.cavallium.data.generator.plugin;
public sealed interface ComputedTypeNullable extends ComputedType permits ComputedTypeNullableNative,
ComputedTypeNullableVersioned {
public sealed interface ComputedTypeNullable extends ComputedType permits ComputedTypeNullableFixed,
ComputedTypeNullableNative, ComputedTypeNullableVersioned {
ComputedType getBase();
}

View File

@ -0,0 +1,124 @@
package it.cavallium.data.generator.plugin;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.TypeName;
import it.cavallium.data.generator.nativedata.UpgradeUtil;
import java.util.Objects;
import java.util.stream.Stream;
public final class ComputedTypeNullableFixed implements ComputedTypeNullable {
private final String baseType;
private final ComputedVersion currentVersion;
private ComputedType computedChild;
private final ComputedTypeSupplier computedTypeSupplier;
public ComputedTypeNullableFixed(String baseType, ComputedVersion currentVersion, ComputedTypeSupplier computedTypeSupplier) {
this.baseType = baseType;
this.currentVersion = currentVersion;
this.computedTypeSupplier = computedTypeSupplier;
}
public VersionedComputedType getBase() {
return (VersionedComputedType) computedTypeSupplier.get(baseType);
}
public ComputedType child() {
synchronized (this) {
if (computedChild == null) {
computedChild = computedTypeSupplier.get(baseType);
}
}
if (computedChild instanceof ComputedTypeNullableFixed) {
throw new IllegalStateException();
} else if (computedChild instanceof ComputedTypeArrayVersioned) {
throw new IllegalStateException();
}
return computedChild;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ComputedTypeNullableFixed that = (ComputedTypeNullableFixed) o;
return Objects.equals(baseType, that.baseType);
}
@Override
public int hashCode() {
return baseType != null ? baseType.hashCode() : 0;
}
@Override
public String getName() {
return "-" + baseType;
}
@Override
public TypeName getJTypeName(String basePackageName) {
return getJTypeNameOfVersion(currentVersion, basePackageName);
}
private TypeName getJTypeNameOfVersion(ComputedVersion version, String basePackageName) {
return ClassName.get(version.getDataNullablesPackage(basePackageName),
"Nullable" + baseType);
}
@Override
public ClassName getJSerializerName(String basePackageName) {
return ClassName.get(currentVersion.getSerializersPackage(basePackageName),
"Nullable" + baseType + "Serializer");
}
@Override
public FieldLocation getJSerializerInstance(String basePackageName) {
var className = ClassName.get(currentVersion.getPackage(basePackageName), "Version");
var serializerFieldName = "Nullable" + baseType + "SerializerInstance";
return new FieldLocation(className, serializerFieldName);
}
@Override
public TypeName getJUpgraderName(String basePackageName) {
throw new UnsupportedOperationException("Not upgradable");
}
@Override
public FieldLocation getJUpgraderInstance(String basePackageName) {
throw new UnsupportedOperationException("Not upgradable");
}
@Override
public CodeBlock wrapWithUpgrade(String basePackageName, CodeBlock content, ComputedType next) {
var builder = CodeBlock.builder();
var upgraderInstance = getBase().getJUpgraderInstance(basePackageName);
builder.add("new $T($T.upgradeNullable(", next.getJTypeName(basePackageName), UpgradeUtil.class);
builder.add(content);
builder.add(".value(), $T.$N)", upgraderInstance.className(), upgraderInstance.fieldName());
builder.add(")");
return builder.build();
}
@Override
public Stream<ComputedType> getDependencies() {
return Stream.of(child());
}
@Override
public Stream<ComputedType> getDependents() {
return computedTypeSupplier.getDependents(getName());
}
@Override
public String toString() {
return "-" + baseType + " (custom)";
}
}

View File

@ -338,12 +338,17 @@ public class DataModel {
.filter(x -> x.startsWith("-"))
.map(nullableName -> nullableName.substring(1))
.toList();
// Compute nullable base types
// Compute nullable versioned types
nullableRawTypes.stream()
.filter(nullableName -> !NATIVE_TYPES.contains(nullableName))
.filter(key -> superTypesData.containsKey(key) || baseTypesData.containsKey(key))
.map(nullableName -> new VersionedType(nullableName, version))
.map(baseType -> new ComputedTypeNullableVersioned(baseType, computedTypeSupplier))
.forEach(versionBaseTypes::add);
// Compute nullable other types
nullableRawTypes.stream()
.filter(customTypesData::containsKey)
.map(baseType -> new ComputedTypeNullableFixed(baseType, computedVersions.get(latestVersion), computedTypeSupplier))
.forEach(versionBaseTypes::add);
// Compute nullable native types
nullableRawTypes.stream()
.filter(NATIVE_TYPES::contains)
@ -359,12 +364,17 @@ public class DataModel {
.filter(x -> x.startsWith("§"))
.map(nullableName -> nullableName.substring(1))
.toList();
// Compute array base types
// Compute array versioned types
arrayRawTypes.stream()
.filter(nullableName -> !NATIVE_TYPES.contains(nullableName))
.filter(key -> superTypesData.containsKey(key) || baseTypesData.containsKey(key))
.map(nullableName -> new VersionedType(nullableName, version))
.map(baseType -> new ComputedTypeArrayVersioned(baseType, computedTypeSupplier))
.forEach(versionBaseTypes::add);
// Compute array other types
arrayRawTypes.stream()
.filter(customTypesData::containsKey)
.map(type -> new ComputedTypeArrayFixed(type, computedVersions.get(latestVersion), computedTypeSupplier))
.forEach(versionBaseTypes::add);
// Compute array native types
arrayRawTypes.stream()
.filter(NATIVE_TYPES::contains)