Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public String getReadBufferReadMethodCall(SimpleTypeReference simpleTypeReferenc
}
throw new FreemarkerException("Unsupported float type with " + floatTypeReference.getSizeInBits() + " bits");
case STRING: {
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF8"));
if (!(encodingTerm instanceof StringLiteral)) {
throw new FreemarkerException("Encoding must be a quoted string value");
}
Expand All @@ -505,7 +505,7 @@ public String getReadBufferReadMethodCall(SimpleTypeReference simpleTypeReferenc
encoding + "\"" + ", (char**) " + valueString + ")";
}
case VSTRING: {
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF8"));
if (!(encodingTerm instanceof StringLiteral)) {
throw new FreemarkerException("Encoding must be a quoted string value");
}
Expand Down Expand Up @@ -566,16 +566,18 @@ public String getWriteBufferWriteMethodCall(SimpleTypeReference simpleTypeRefere
}
throw new FreemarkerException("Unsupported float type with " + floatTypeReference.getSizeInBits() + " bits");
case STRING: {
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF8"));
if (!(encodingTerm instanceof StringLiteral)) {
throw new FreemarkerException("Encoding must be a quoted string value");
}
String encoding = ((StringLiteral) encodingTerm).getValue();
String length = Integer.toString(simpleTypeReference.getSizeInBits());
int numChars;
if("UTF-8".equalsIgnoreCase(encoding)) {
if ("UTF8".equalsIgnoreCase(encoding)) {
numChars = simpleTypeReference.getSizeInBits() / 8;
} else if("UTF-16".equalsIgnoreCase(encoding)) {
} else if ("UTF16".equalsIgnoreCase(encoding)) {
numChars = simpleTypeReference.getSizeInBits() / 16;
} else if ("UTF16BE".equalsIgnoreCase(encoding)) {
numChars = simpleTypeReference.getSizeInBits() / 16;
} else {
throw new FreemarkerException("Unsupported encoding " + encoding);
Expand All @@ -584,7 +586,7 @@ public String getWriteBufferWriteMethodCall(SimpleTypeReference simpleTypeRefere
encoding + "\", (const uint8_t*) " + (numChars == 1 ? "&" : "") + fieldName + ")";
}
case VSTRING: {
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF8"));
if (!(encodingTerm instanceof StringLiteral)) {
throw new FreemarkerException("Encoding must be a quoted string value");
}
Expand Down Expand Up @@ -907,7 +909,7 @@ private String toTypeVariableParseExpression(Field field, VariableLiteral variab
tracer = tracer.dive("type");
if (variableLiteral.getChild().isPresent() && "encoding".equals(variableLiteral.getChild().get().getName()) && (field instanceof TypedField) && ((((TypedField) field).getType() instanceof StringTypeReference) || (((TypedField) field).getType() instanceof VstringTypeReference))) {
// TODO: replace with map join
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF8"));
if (!(encodingTerm instanceof StringLiteral)) {
throw new FreemarkerException("Encoding must be a quoted string value");
}
Expand Down Expand Up @@ -1096,7 +1098,7 @@ private String toVariableSerializationExpression(TypeDefinition baseType, Field
if (!(typedField.getType() instanceof StringTypeReference)) {
throw new FreemarkerException("Can only access 'encoding' for string types.");
}
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF8"));
if (!(encodingTerm instanceof StringLiteral)) {
throw new FreemarkerException("Encoding must be a quoted string value");
}
Expand Down Expand Up @@ -1188,7 +1190,7 @@ private String toUppercaseSerializationExpression(TypeDefinition baseType, Field
if (!(typedField.getType() instanceof StringTypeReference)) {
throw new FreemarkerException("Can only access 'encoding' for string types.");
}
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF8"));
if (!(encodingTerm instanceof StringLiteral)) {
throw new FreemarkerException("Encoding must be a quoted string value");
}
Expand Down Expand Up @@ -1265,7 +1267,7 @@ private String toStaticCallSerializationExpression(TypeDefinition baseType, Fiel
if (!(typedField.getType() instanceof StringTypeReference)) {
throw new FreemarkerException("Can only access 'encoding' for string types.");
}
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF8"));
if (!(encodingTerm instanceof StringLiteral)) {
throw new FreemarkerException("Encoding must be a quoted string value");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1269,4 +1269,16 @@ public String getFieldOptions(TypedField field, List<Argument> parserArguments)
return sb.toString();
}

public boolean isRawByteArray(DiscriminatedComplexTypeDefinition currentCase) {
Optional<Field> valueFieldOptional = currentCase.getFields().stream().filter(field -> field.isNamedField() && field.asNamedField().orElseThrow().getName().equals("value")).findFirst();
if (valueFieldOptional.isPresent()) {
Field valueField = valueFieldOptional.get();
if (valueField.isTypedField()) {
TypedField typedField = valueField.asTypedField().orElseThrow();
return typedField.getType().isArrayTypeReference() && typedField.getType().asArrayTypeReference().orElseThrow().getElementTypeReference().isByteBased();
}
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,11 @@ namespace org.apache.plc4net.drivers.${protocolName?replace("-", "")}.${outputFl
return new PlcStruct(_map);
<#break>
<#case "List">
<#if helper.isRawByteArray(case)>
return new PlcRawByteArray(value);
<#else>
return new PlcList(value);
</#if>
<#break>
<#default>
return new Plc${case.name}(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public String getReadBufferReadMethodCall(String logicalName, SimpleTypeReferenc
}
return "/*TODO: migrate me*/" + "readBuffer.ReadBigFloat(\"" + logicalName + "\", " + floatTypeReference.getSizeInBits() + ")";
case STRING: {
String encoding = "UTF-8";
String encoding = "UTF8";
if (field != null) {
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral(encoding));
encoding = encodingTerm.asLiteral()
Expand All @@ -439,7 +439,7 @@ public String getReadBufferReadMethodCall(String logicalName, SimpleTypeReferenc
return "/*TODO: migrate me*/" + "readBuffer.ReadString(\"" + logicalName + "\", uint32(" + length + "), utils.WithEncoding(\"" + encoding + "\"))";
}
case VSTRING: {
String encoding = "UTF-8";
String encoding = "UTF8";
VstringTypeReference vstringTypeReference = (VstringTypeReference) simpleTypeReference;
if (field != null) {
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral(encoding));
Expand Down Expand Up @@ -711,9 +711,9 @@ public String getWriteBufferWriteMethodCall(String logicalName, SimpleTypeRefere
return "/*TODO: migrate me*/" + "writeBuffer.WriteBigFloat(\"" + logicalName + "\", " + floatTypeReference.getSizeInBits() + ", " + fieldName + writerArgsString + ")";
case STRING: {
StringTypeReference stringTypeReference = (StringTypeReference) simpleTypeReference;
String encoding = "UTF-8";
String encoding = "UTF8";
if (field != null) {
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral(encoding));
encoding = encodingTerm.asLiteral()
.orElseThrow(() -> new FreemarkerException("Encoding must be a literal"))
.asStringLiteral()
Expand All @@ -724,9 +724,9 @@ public String getWriteBufferWriteMethodCall(String logicalName, SimpleTypeRefere
}
case VSTRING: {
VstringTypeReference vstringTypeReference = (VstringTypeReference) simpleTypeReference;
String encoding = "UTF-8";
String encoding = "UTF8";
if (field != null) {
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral(encoding));
encoding = encodingTerm.asLiteral()
.orElseThrow(() -> new FreemarkerException("Encoding must be a literal"))
.asStringLiteral()
Expand Down Expand Up @@ -1323,7 +1323,7 @@ else if (isTypeArg) {
sb.append("\"").append(((SimpleTypeReference) typeReference).getSizeInBits()).append("\"");
break;
case "encoding":
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF-8"));
final Term encodingTerm = field.getEncoding().orElse(new DefaultStringLiteral("UTF8"));
if (!(encodingTerm instanceof StringLiteral)) {
throw new FreemarkerException("Encoding must be a quoted string value");
}
Expand Down Expand Up @@ -1862,13 +1862,20 @@ public String getEndiannessOptions(boolean read, boolean separatorPrefix, List<A
Optional<Term> byteOrder = thisType.getAttribute("byteOrder");
if (byteOrder.isPresent()) {
emitRequiredImport("encoding/binary");
emitDataIoRequiredImport("encoding/binary");
String byteOrderTranslated = switch (byteOrder.orElseThrow().stringRepresentation()) {
case "\"BIG_ENDIAN\"" -> "binary.BigEndian";
case "\"LITTLE_ENDIAN\"" -> "binary.LittleEndian";
default ->
throw new RuntimeException("unmapped bytes order " + byteOrder.orElseThrow().stringRepresentation());
};
if (read) {
return (separatorPrefix ? ", " : "") + "utils.WithByteOrderForReadBufferByteBased(" +
toParseExpression(null, new DefaultByteOrderTypeReference(), byteOrder.orElseThrow(), parserArguments) +
byteOrderTranslated +
")";
} else {
return (separatorPrefix ? ", " : "") + "utils.WithByteOrderForByteBasedBuffer(" +
toSerializationExpression(null, new DefaultByteOrderTypeReference(), byteOrder.orElseThrow(), parserArguments) +
byteOrderTranslated +
")";
}
}
Expand All @@ -1877,26 +1884,26 @@ public String getEndiannessOptions(boolean read, boolean separatorPrefix, List<A

public String getFieldOptions(TypedField field, List<Argument> parserArguments) {
StringBuilder sb = new StringBuilder();
field.getEncoding().ifPresent(term -> {
field.getAttribute("stringEncoding").ifPresentOrElse(term -> {
emitCodegenRequiredImports();
final String encoding = toParseExpression(field, field.getType(), term, parserArguments);
sb.append(", codegen.WithEncoding(").append(encoding).append(")");
}, () -> {
field.getEncoding().ifPresent(term -> {
emitCodegenRequiredImports();
final String encoding = toParseExpression(field, field.getType(), term, parserArguments);
sb.append(", codegen.WithEncoding(").append(encoding).append(")");
});
});

field.getByteOrder().ifPresent(term -> {
emitCodegenRequiredImports();
emitRequiredImport("encoding/binary");
String byteOrder = "binary.BigEndian";
switch (term.stringRepresentation()) {
case "BIG_ENDIAN":
byteOrder = "binary.BigEndian";
break;
case "LITTLE_ENDIAN":
byteOrder = "binary.LittleEndian";
break;
default:
throw new RuntimeException("unmapped bytes order " + term.stringRepresentation());
}
String byteOrder = switch (term.stringRepresentation()) {
case "\"BIG_ENDIAN\"" -> "binary.BigEndian";
case "\"LITTLE_ENDIAN\"" -> "binary.LittleEndian";
default -> throw new RuntimeException("unmapped bytes order " + term.stringRepresentation());
};
sb.append(", codegen.WithByteOrder(").append(byteOrder).append(")");
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ func Cast${type.name}(structType any) ${type.name} {
return nil
}

func (m *_${type.name}) GetTypeName() string {
func (m *_${type.name}) GetPlx4xTypeName() string {
return "${type.name}"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ func ${type.name}ParseWithBuffer(ctx context.Context, readBuffer utils.ReadBuffe
<#if !field.isCountArrayField() && !field.isLengthArrayField()>
return nil, errors.Wrap(_${arrayField.name}Err, "Array fields of type byte only support 'count' and 'length' loop-types.")<@emitImport import="github.com/apache/plc4x/plc4go/spi/errors" />
</#if>
${arrayField.name}, _${arrayField.name}Err := readBuffer.ReadByteArray("${arrayField.name}", int(${helper.toParseExpression(null, null, arrayField.loopExpression, parserArguments)}))<#if arrayField.loopExpression.contains("CEIL")><@emitImport import="math" /></#if>
${arrayField.name}Raw, _${arrayField.name}Err := readBuffer.ReadByteArray("${arrayField.name}", int(${helper.toParseExpression(null, null, arrayField.loopExpression, parserArguments)}))<#if arrayField.loopExpression.contains("CEIL")><@emitImport import="math" /></#if>
if _${arrayField.name}Err != nil {
return nil, errors.Wrap(_${arrayField.name}Err, "Error parsing '${arrayField.name}' field")<@emitImport import="github.com/apache/plc4x/plc4go/spi/errors" />
}
${arrayField.name} := []api.PlcValue{values.NewPlcRawByteArray(${arrayField.name}Raw)}
<#else>
var ${arrayField.name} []api.PlcValue
for i := 0; i < int(${helper.toParseExpression(null, null, arrayField.loopExpression, parserArguments)}); i++ {
Expand Down Expand Up @@ -181,7 +182,11 @@ func ${type.name}ParseWithBuffer(ctx context.Context, readBuffer utils.ReadBuffe
|| ((case.name == "LDATE") && (simpleField.name == "nanosecondsSinceEpoch"))
|| ((case.name == "TIME_OF_DAY") && (simpleField.name == "millisecondsSinceMidnight"))
|| ((case.name == "LTIME_OF_DAY") && (simpleField.name == "nanosecondsSinceMidnight"))
|| ((case.name == "DATE_AND_TIME") && (simpleField.name == "secondsSinceEpoch"))
|| ((case.name == "DATE_AND_TIME") && (
(simpleField.name == "secondsSinceEpoch")
|| (simpleField.name == "nanosecondsOfSecond")
)
)
|| ((case.name == "DATE_AND_LTIME") && (
(simpleField.name == "year")
|| (simpleField.name == "month")
Expand All @@ -190,7 +195,7 @@ func ${type.name}ParseWithBuffer(ctx context.Context, readBuffer utils.ReadBuffe
|| (simpleField.name == "minutes")
|| (simpleField.name == "seconds")
|| (simpleField.name == "nanoseconds")
|| (simpleField.name == "nannosecondsOfSecond")
|| (simpleField.name == "nanosecondsOfSecond")
|| (simpleField.name == "nanosecondsSinceEpoch")
|| (simpleField.name == "dayOfWeek")
)
Expand Down
Loading
Loading