Skip to content

Commit

Permalink
fix(codegen): fixed issues with wrong type handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Feb 24, 2022
1 parent c216cbe commit aa61ce2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,22 +258,11 @@ public boolean isEnumField(Field field) {
if (typeReference.isSimpleTypeReference()) {
return false;
}
TypeDefinition typeDefinition = getTypeDefinitionForTypeReference(typeReference);
TypeDefinition typeDefinition = typeReference.asNonSimpleTypeReference().orElseThrow()
.getTypeDefinition();
return typeDefinition instanceof EnumTypeDefinition;
}

/* *********************************************************************************
* Methods related to type-definitions.
**********************************************************************************/

public TypeDefinition getTypeDefinitionForTypeReference(TypeReference typeReference) {
Objects.requireNonNull(typeReference);
NonSimpleTypeReference complexTypeReference = typeReference
.asNonSimpleTypeReference()
.orElseThrow(() -> new FreemarkerException("Type reference must be a non simple type reference"));
return complexTypeReference.getTypeDefinition();
}

/* *********************************************************************************
* Methods related to terms and expressions.
**********************************************************************************/
Expand Down Expand Up @@ -403,14 +392,12 @@ public Collection<EnumValue> getEnumValuesForUniqueConstantValues(List<EnumValue
return filteredEnumValues.values();
}

public SimpleTypeReference getEnumFieldSimpleTypeReference(TypeReference type, String fieldName) {
TypeDefinition typeDefinition = getTypeDefinitionForTypeReference(type);

if (typeDefinition instanceof EnumTypeDefinition
&& ((EnumTypeDefinition) typeDefinition).getConstantType(fieldName) instanceof SimpleTypeReference) {
return (SimpleTypeReference) ((EnumTypeDefinition) typeDefinition).getConstantType(fieldName);
public SimpleTypeReference getEnumFieldSimpleTypeReference(NonSimpleTypeReference type, String fieldName) {
if (!(type.getTypeDefinition() instanceof EnumTypeDefinition)
|| !(((EnumTypeDefinition) type.getTypeDefinition()).getConstantType(fieldName) instanceof SimpleTypeReference)) {
throw new IllegalArgumentException("not an enum type or enum constant is not a simple type");
}
throw new IllegalArgumentException("not an enum type or enum constant is not a simple type");
return (SimpleTypeReference) ((EnumTypeDefinition) type.getTypeDefinition()).getConstantType(fieldName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ public String getLanguageTypeNameForField(Field field) {
}
TypedField typedField = field.asTypedField().orElseThrow(IllegalStateException::new);
TypeReference typeReference = typedField.getType();
if (typeReference.isComplexTypeReference()) {
final TypeDefinition typeDefinition = getTypeDefinitionForTypeReference(typeReference);
if (typeDefinition instanceof DataIoTypeDefinition) {
if (typeReference.isNonSimpleTypeReference()) {
if (typeReference.asNonSimpleTypeReference().orElseThrow().getTypeDefinition() instanceof DataIoTypeDefinition) {
return "plc4c_data*";
}
}
Expand Down Expand Up @@ -323,8 +322,8 @@ public String escapeValue(TypeReference typeReference, String valueString) {
}
if ("null".equals(valueString)) {
// C doesn't like NULL values for enums, so we have to return something else (we'll treat -1 as NULL)
if (typeReference instanceof ComplexTypeReference) {
if (getTypeDefinitionForTypeReference(typeReference) instanceof EnumTypeDefinition) {
if (typeReference.isNonSimpleTypeReference()) {
if (typeReference.asNonSimpleTypeReference().orElseThrow().getTypeDefinition().isEnumTypeDefinition()) {
return "-1";
}
}
Expand Down Expand Up @@ -648,11 +647,10 @@ private String toVariableLiteralExpression(TypeDefinition baseType, VariableLite
}
lengthExpression = "_message";
} else {
final Optional<TypeReference> typeReferenceForProperty = ((ComplexTypeDefinition) baseType).getTypeReferenceForProperty(variableLiteral.getName());
if (typeReferenceForProperty.isEmpty()) {
throw new FreemarkerException("Unknown type for property " + variableLiteral.getName());
}
lengthType = getTypeDefinitionForTypeReference(typeReferenceForProperty.get());
final TypeReference typeReferenceForProperty = ((ComplexTypeDefinition) baseType)
.getTypeReferenceForProperty(variableLiteral.getName())
.orElseThrow(()->new FreemarkerException("Unknown type for property " + variableLiteral.getName()));
lengthType = typeReferenceForProperty.asNonSimpleTypeReference().orElseThrow().getTypeDefinition();
lengthExpression = variableExpressionGenerator.apply(variableLiteral);
}
return tracer + getCTypeName(lengthType.getName()) + "_length_in_bytes(" + lengthExpression + ")";
Expand Down Expand Up @@ -983,8 +981,7 @@ private String toVariableSerializationExpression(TypeDefinition baseType, Field
if (typeReferenceForProperty.isPresent()) {
final TypeReference typeReference = typeReferenceForProperty.get();
if (typeReference instanceof ComplexTypeReference) {
final TypeDefinition typeDefinitionForTypeReference =
getTypeDefinitionForTypeReference(typeReference);
final TypeDefinition typeDefinitionForTypeReference = typeReference.asComplexTypeReference().orElseThrow().getTypeDefinition();
if ((typeDefinitionForTypeReference instanceof EnumTypeDefinition) && (variableLiteral.getChild().isPresent())) {
tracer = tracer.dive("is enum type definition");
sb.append(camelCaseToSnakeCase(variableLiteral.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,8 @@ else if ((variableLiteral.getChild().isPresent()) && ((ComplexTypeDefinition) th
.flatMap(TypeReferenceConversions::asNonSimpleTypeReference);
if (typeReferenceForProperty.isPresent()) {
tracer = tracer.dive("complex");
final TypeReference nonSimpleTypeReference = typeReferenceForProperty.get();
TypeDefinition typeDefinition = getTypeDefinitionForTypeReference(nonSimpleTypeReference);
final NonSimpleTypeReference nonSimpleTypeReference = typeReferenceForProperty.get();
TypeDefinition typeDefinition = nonSimpleTypeReference.getTypeDefinition();
if (typeDefinition instanceof ComplexTypeDefinition) {
tracer = tracer.dive("complex");
ComplexTypeDefinition complexTypeDefinition = (ComplexTypeDefinition) typeDefinition;
Expand Down Expand Up @@ -1424,8 +1424,9 @@ public boolean needsReferenceForParserArgument(String propertyName, TypeReferenc
complexTypeDefinition -> complexTypeDefinition.getPropertyFieldByName(propertyName)
.map(TypedField.class::cast)
.map(TypedField::getType)
.filter(ComplexTypeReference.class::isInstance)
.map(this::getTypeDefinitionForTypeReference)
.filter(NonSimpleTypeReference.class::isInstance)
.map(NonSimpleTypeReference.class::cast)
.map(NonSimpleTypeReference::getTypeDefinition)
.map(typeDefinition -> !(typeDefinition instanceof EnumTypeDefinition))
.orElse(false)
)
Expand Down

0 comments on commit aa61ce2

Please sign in to comment.