Skip to content

Commit

Permalink
fix(codegen/plc4go): fix encoding retrieval with null fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Jul 27, 2023
1 parent e6cfa18 commit 20e5c91
Showing 1 changed file with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public String getLanguageTypeNameForField(Field field) {
TypedField typedField = field.asTypedField().orElseThrow();
String encoding = null;
Optional<Term> encodingAttribute = field.getAttribute("encoding");
if(encodingAttribute.isPresent()) {
if (encodingAttribute.isPresent()) {
encoding = encodingAttribute.get().toString();
}
return getLanguageTypeNameForTypeReference(typedField.getType(), encoding);
Expand Down Expand Up @@ -370,21 +370,27 @@ public String getReadBufferReadMethodCall(String logicalName, SimpleTypeReferenc
}
return "readBuffer.ReadBigFloat(\"" + logicalName + "\", " + floatTypeReference.getSizeInBits() + ")";
case STRING: {
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
if (!(encodingTerm instanceof StringLiteral)) {
throw new FreemarkerException("Encoding must be a quoted string value");
String encoding = "UTF-8";
if (field != null) {
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral(encoding));
encoding = encodingTerm.asLiteral()
.orElseThrow(() -> new FreemarkerException("Encoding must be a literal"))
.asStringLiteral()
.orElseThrow(() -> new FreemarkerException("Encoding must be a quoted string value")).getValue();
}
String encoding = ((StringLiteral) encodingTerm).getValue();
String length = Integer.toString(simpleTypeReference.getSizeInBits());
return "readBuffer.ReadString(\"" + logicalName + "\", uint32(" + length + "), \"" + encoding + "\")";
}
case VSTRING: {
String encoding = "UTF-8";
VstringTypeReference vstringTypeReference = (VstringTypeReference) simpleTypeReference;
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
if (!(encodingTerm instanceof StringLiteral)) {
throw new FreemarkerException("Encoding must be a quoted string value");
if (field != null) {
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral(encoding));
encoding = encodingTerm.asLiteral()
.orElseThrow(() -> new FreemarkerException("Encoding must be a literal"))
.asStringLiteral()
.orElseThrow(() -> new FreemarkerException("Encoding must be a quoted string value")).getValue();
}
String encoding = ((StringLiteral) encodingTerm).getValue();
String lengthExpression = toExpression(field, null, vstringTypeReference.getLengthExpression(), null, null, false, false);
return "readBuffer.ReadString(\"" + logicalName + "\", uint32(" + lengthExpression + "), \"" + encoding + "\")";
}
Expand Down Expand Up @@ -473,22 +479,28 @@ public String getWriteBufferWriteMethodCall(String logicalName, SimpleTypeRefere
return "writeBuffer.WriteBigFloat(\"" + logicalName + "\", " + floatTypeReference.getSizeInBits() + ", " + fieldName + writerArgsString + ")";
case STRING: {
StringTypeReference stringTypeReference = (StringTypeReference) simpleTypeReference;
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
String encoding = encodingTerm.asLiteral()
.orElseThrow(() -> new FreemarkerException("Encoding must be a literal"))
.asStringLiteral()
.orElseThrow(() -> new FreemarkerException("Encoding must be a quoted string value")).getValue();
String encoding = "UTF-8";
if (field != null) {
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
encoding = encodingTerm.asLiteral()
.orElseThrow(() -> new FreemarkerException("Encoding must be a literal"))
.asStringLiteral()
.orElseThrow(() -> new FreemarkerException("Encoding must be a quoted string value")).getValue();
}
String length = Integer.toString(simpleTypeReference.getSizeInBits());
return "writeBuffer.WriteString(\"" + logicalName + "\", uint32(" + length + "), \"" +
encoding + "\", " + fieldName + writerArgsString + ")";
}
case VSTRING: {
VstringTypeReference vstringTypeReference = (VstringTypeReference) simpleTypeReference;
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
String encoding = encodingTerm.asLiteral()
.orElseThrow(() -> new FreemarkerException("Encoding must be a literal"))
.asStringLiteral()
.orElseThrow(() -> new FreemarkerException("Encoding must be a quoted string value")).getValue();
String encoding = "UTF-8";
if (field != null) {
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
encoding = encodingTerm.asLiteral()
.orElseThrow(() -> new FreemarkerException("Encoding must be a literal"))
.asStringLiteral()
.orElseThrow(() -> new FreemarkerException("Encoding must be a quoted string value")).getValue();
}
String lengthExpression = toExpression(field, null, vstringTypeReference.getLengthExpression(), null, Collections.singletonList(new DefaultArgument("stringLength", new DefaultIntegerTypeReference(SimpleTypeReference.SimpleBaseType.INT, 32))), true, false);
String length = Integer.toString(simpleTypeReference.getSizeInBits());
return "writeBuffer.WriteString(\"" + logicalName + "\", uint32(" + lengthExpression + "), \"" +
Expand Down Expand Up @@ -584,7 +596,7 @@ String getCastExpressionForTypeReference(TypeReference typeReference) {
if (typeReference instanceof SimpleTypeReference) {
return tracer.dive("simpleTypeRef") + getLanguageTypeNameForTypeReference(typeReference);
} else if (typeReference instanceof ByteOrderTypeReference) {
return tracer.dive( "byteOrderTypeRef") + "binary.ByteOrder";
return tracer.dive("byteOrderTypeRef") + "binary.ByteOrder";
} else if (typeReference != null) {
return tracer.dive("anyTypeRef") + "Cast" + getLanguageTypeNameForTypeReference(typeReference);
} else {
Expand Down Expand Up @@ -1601,7 +1613,7 @@ public String getEndiannessOptions(boolean read, boolean separatorPrefix, List<A
Optional<Term> byteOrder = thisType.getAttribute("byteOrder");
if (byteOrder.isPresent()) {
emitRequiredImport("encoding/binary");
if(read) {
if (read) {
return (separatorPrefix ? ", " : "") + "utils.WithByteOrderForReadBufferByteBased(" +
toParseExpression(null, new DefaultByteOrderTypeReference(), byteOrder.orElseThrow(), parserArguments) +
")";
Expand Down

0 comments on commit 20e5c91

Please sign in to comment.