|
22 | 22 | import org.apache.commons.lang3.math.NumberUtils; |
23 | 23 | import org.apache.commons.text.CaseUtils; |
24 | 24 | import org.apache.plc4x.plugins.codegenerator.language.mspec.model.definitions.DefaultArgument; |
25 | | -import org.apache.plc4x.plugins.codegenerator.language.mspec.model.references.DefaultBooleanTypeReference; |
26 | | -import org.apache.plc4x.plugins.codegenerator.language.mspec.model.references.DefaultFloatTypeReference; |
27 | | -import org.apache.plc4x.plugins.codegenerator.language.mspec.model.references.DefaultIntegerTypeReference; |
28 | | -import org.apache.plc4x.plugins.codegenerator.language.mspec.model.references.DefaultVstringTypeReference; |
| 25 | +import org.apache.plc4x.plugins.codegenerator.language.mspec.model.references.*; |
29 | 26 | import org.apache.plc4x.plugins.codegenerator.language.mspec.model.terms.DefaultStringLiteral; |
30 | 27 | import org.apache.plc4x.plugins.codegenerator.protocol.freemarker.BaseFreemarkerLanguageTemplateHelper; |
31 | 28 | import org.apache.plc4x.plugins.codegenerator.protocol.freemarker.FreemarkerException; |
@@ -123,6 +120,9 @@ public String getLanguageTypeNameForTypeReference(TypeReference typeReference, S |
123 | 120 | if (typeReference.isNonSimpleTypeReference()) { |
124 | 121 | return typeReference.asNonSimpleTypeReference().orElseThrow().getName(); |
125 | 122 | } |
| 123 | + if (typeReference instanceof ByteOrderTypeReference) { |
| 124 | + return "binary.byteOrder"; |
| 125 | + } |
126 | 126 | SimpleTypeReference simpleTypeReference = typeReference.asSimpleTypeReference().orElseThrow(); |
127 | 127 | switch (simpleTypeReference.getBaseType()) { |
128 | 128 | case BIT: |
@@ -583,6 +583,8 @@ String getCastExpressionForTypeReference(TypeReference typeReference) { |
583 | 583 | Tracer tracer = Tracer.start("castExpression"); |
584 | 584 | if (typeReference instanceof SimpleTypeReference) { |
585 | 585 | return tracer.dive("simpleTypeRef") + getLanguageTypeNameForTypeReference(typeReference); |
| 586 | + } else if (typeReference instanceof ByteOrderTypeReference) { |
| 587 | + return tracer.dive( "byteOrderTypeRef") + "binary.ByteOrder"; |
586 | 588 | } else if (typeReference != null) { |
587 | 589 | return tracer.dive("anyTypeRef") + "Cast" + getLanguageTypeNameForTypeReference(typeReference); |
588 | 590 | } else { |
@@ -618,10 +620,16 @@ private String toTernaryTermExpression(Field field, TypeReference fieldType, Ter |
618 | 620 | String inlineIf = "utils.InlineIf(" + toExpression(field, new DefaultBooleanTypeReference(), a, parserArguments, serializerArguments, serialize, false) + ", " + |
619 | 621 | "func() interface{} {return " + castExpressionForTypeReference + "(" + toExpression(field, fieldType, b, parserArguments, serializerArguments, serialize, false) + ")}, " + |
620 | 622 | "func() interface{} {return " + castExpressionForTypeReference + "(" + toExpression(field, fieldType, c, parserArguments, serializerArguments, serialize, false) + ")})"; |
621 | | - if (fieldType.isNonSimpleTypeReference()) { |
622 | | - return tracer.dive("nonsimpletypereference") + castExpressionForTypeReference + "(" + inlineIf + ")"; |
| 623 | + if (fieldType != null) { |
| 624 | + if (fieldType instanceof ByteOrderTypeReference) { |
| 625 | + return tracer.dive("byteordertypereference") + "(" + inlineIf + ").(binary.ByteOrder)"; |
| 626 | + } |
| 627 | + if (fieldType.isNonSimpleTypeReference()) { |
| 628 | + return tracer.dive("nonsimpletypereference") + castExpressionForTypeReference + "(" + inlineIf + ")"; |
| 629 | + } |
| 630 | + return tracer + inlineIf + ".(" + castExpressionForTypeReference + ")"; |
623 | 631 | } |
624 | | - return tracer + inlineIf + ".(" + castExpressionForTypeReference + ")"; |
| 632 | + return tracer + inlineIf; |
625 | 633 | } else { |
626 | 634 | throw new RuntimeException("Unsupported ternary operation type " + ternaryTerm.getOperation()); |
627 | 635 | } |
@@ -725,6 +733,10 @@ private String toLiteralTermExpression(Field field, TypeReference fieldType, Ter |
725 | 733 | VariableLiteral variableLiteral = (VariableLiteral) term; |
726 | 734 | if ("curPos".equals(((VariableLiteral) term).getName())) { |
727 | 735 | return "(positionAware.GetPos() - startPos)"; |
| 736 | + } else if ("BIG_ENDIAN".equals(((VariableLiteral) term).getName()) && (fieldType instanceof ByteOrderTypeReference)) { |
| 737 | + return "binary.BigEndian"; |
| 738 | + } else if ("LITTLE_ENDIAN".equals(((VariableLiteral) term).getName()) && (fieldType instanceof ByteOrderTypeReference)) { |
| 739 | + return "binary.LittleEndian"; |
728 | 740 | } |
729 | 741 | return tracer + toVariableExpression(field, fieldType, (VariableLiteral) term, parserArguments, serializerArguments, serialize, suppressPointerAccess); |
730 | 742 | } else { |
@@ -891,15 +903,15 @@ else if ((variableLiteral.getChild().isPresent()) && ((ComplexTypeDefinition) th |
891 | 903 | variableLiteral.getChild() |
892 | 904 | .map(child -> "." + capitalize(toVariableExpression(field, typeReference, child, parserArguments, serializerArguments, false, suppressPointerAccess, true))) |
893 | 905 | .orElse(""); |
894 | | - }/* |
| 906 | + } |
895 | 907 | if ((parserArguments != null) && parserArguments.stream() |
896 | 908 | .anyMatch(argument -> argument.getName().equals(variableLiteralName))) { |
897 | 909 | tracer = tracer.dive("parser argument"); |
898 | | - return tracer + "m." + capitalize(variableLiteralName) + |
| 910 | + return tracer + variableLiteralName + |
899 | 911 | variableLiteral.getChild() |
900 | 912 | .map(child -> "." + capitalize(toVariableExpression(field, typeReference, child, parserArguments, serializerArguments, false, suppressPointerAccess, true))) |
901 | 913 | .orElse(""); |
902 | | - }*/ |
| 914 | + } |
903 | 915 | String indexCall = ""; |
904 | 916 | if (variableLiteral.getIndex().isPresent()) { |
905 | 917 | tracer = tracer.dive("indexCall"); |
@@ -1582,16 +1594,21 @@ public String capitalize(String str) { |
1582 | 1594 | } |
1583 | 1595 |
|
1584 | 1596 | public String getEndiannessOptions(boolean read, boolean separatorPrefix) { |
| 1597 | + return getEndiannessOptions(read, separatorPrefix, Collections.emptyList()); |
| 1598 | + } |
| 1599 | + |
| 1600 | + public String getEndiannessOptions(boolean read, boolean separatorPrefix, List<Argument> parserArguments) { |
1585 | 1601 | Optional<Term> byteOrder = thisType.getAttribute("byteOrder"); |
1586 | 1602 | if (byteOrder.isPresent()) { |
1587 | 1603 | emitRequiredImport("encoding/binary"); |
1588 | | - |
1589 | | - String functionName = read ? "WithByteOrderForReadBufferByteBased" : "WithByteOrderForByteBasedBuffer"; |
1590 | | - String byteOrderValue = ((VariableLiteral) byteOrder.get()).getName(); |
1591 | | - if("BIG_ENDIAN".equals(byteOrderValue)) { |
1592 | | - return (separatorPrefix ? ", " : "") + "utils." + functionName + "(binary.BigEndian)"; |
1593 | | - } else if ("LITTLE_ENDIAN".equals(byteOrderValue)) { |
1594 | | - return (separatorPrefix ? ", " : "") + "utils." + functionName + "(binary.LittleEndian)"; |
| 1604 | + if(read) { |
| 1605 | + return (separatorPrefix ? ", " : "") + "utils.WithByteOrderForReadBufferByteBased(" + |
| 1606 | + toParseExpression(null, new DefaultByteOrderTypeReference(), byteOrder.orElseThrow(), parserArguments) + |
| 1607 | + ")"; |
| 1608 | + } else { |
| 1609 | + return (separatorPrefix ? ", " : "") + "utils.WithByteOrderForByteBasedBuffer(" + |
| 1610 | + toSerializationExpression(null, new DefaultByteOrderTypeReference(), byteOrder.orElseThrow(), parserArguments) + |
| 1611 | + ")"; |
1595 | 1612 | } |
1596 | 1613 | } |
1597 | 1614 | return ""; |
|
0 commit comments