You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2020/10/12 21:27:54 UTC

[plc4x] branch feature/plc4go updated: - Finshed the last little quirks in the code-generation

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch feature/plc4go
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/feature/plc4go by this push:
     new bb6852d  - Finshed the last little quirks in the code-generation
bb6852d is described below

commit bb6852df5ec86337f502df0d5079ce84fdd44c63
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Mon Oct 12 23:27:47 2020 +0200

    - Finshed the last little quirks in the code-generation
---
 .../language/go/GoLanguageTemplateHelper.java      |  19 +++-
 .../go/hack/DefaultFloatTypeReference.java         |  22 +++--
 .../apache/plc4x/language/go/utils/FieldUtils.java | 103 +++++++++++++++++++++
 .../main/resources/templates/go/enum-template.ftlh |   2 +-
 .../modbus/readwrite/model/ModbusDataType.go       |   2 +-
 .../plc4go/s7/readwrite/model/COTPTpduSize.go      |   2 +-
 .../plc4go/s7/readwrite/model/DataTransportSize.go |   2 +-
 .../plc4go/s7/readwrite/model/MemoryArea.go        |   2 +-
 .../s7/readwrite/model/S7VarPayloadDataItem.go     |   6 +-
 .../plc4go/s7/readwrite/model/TransportSize.go     |  20 ++--
 10 files changed, 150 insertions(+), 30 deletions(-)

diff --git a/build-utils/language-go/src/main/java/org/apache/plc4x/language/go/GoLanguageTemplateHelper.java b/build-utils/language-go/src/main/java/org/apache/plc4x/language/go/GoLanguageTemplateHelper.java
index 258f8df..6bd1ef1 100644
--- a/build-utils/language-go/src/main/java/org/apache/plc4x/language/go/GoLanguageTemplateHelper.java
+++ b/build-utils/language-go/src/main/java/org/apache/plc4x/language/go/GoLanguageTemplateHelper.java
@@ -24,6 +24,7 @@ import org.apache.commons.lang3.math.NumberUtils;
 import org.apache.plc4x.language.go.hack.DefaultBooleanTypeReference;
 import org.apache.plc4x.language.go.hack.DefaultFloatTypeReference;
 import org.apache.plc4x.language.go.hack.DefaultIntegerTypeReference;
+import org.apache.plc4x.language.go.utils.FieldUtils;
 import org.apache.plc4x.plugins.codegenerator.protocol.freemarker.BaseFreemarkerLanguageTemplateHelper;
 import org.apache.plc4x.plugins.codegenerator.types.definitions.*;
 import org.apache.plc4x.plugins.codegenerator.types.enums.EnumValue;
@@ -432,6 +433,12 @@ public class GoLanguageTemplateHelper extends BaseFreemarkerLanguageTemplateHelp
                 ((vl.getChild().getChild() != null) ?
                     "." + toVariableExpression(typeReference, vl.getChild().getChild(), parserArguments, serializerArguments, false, suppressPointerAccess) : "");
         }
+        // If we are accessing enum constants, these also need to be output differently.
+        else if ((getFieldForNameFromCurrent(vl.getName()) instanceof EnumField) && (vl.getChild() != null)) {
+            return vl.getName() + "." + StringUtils.capitalize(vl.getChild().getName()) + "()" +
+                ((vl.getChild().getChild() != null) ?
+                    "." + toVariableExpression(typeReference, vl.getChild().getChild(), parserArguments, serializerArguments, false, suppressPointerAccess) : "");
+        }
         // CAST expressions are special as we need to add a ".class" to the second parameter in Java.
         else if ("CAST".equals(vl.getName())) {
             if ((vl.getArgs() == null) || (vl.getArgs().size() != 2)) {
@@ -530,10 +537,10 @@ public class GoLanguageTemplateHelper extends BaseFreemarkerLanguageTemplateHelp
             return getCastExpressionForTypeReference(typeReference) + "(" + ((VariableLiteral) vl.getArgs().get(0)).getName() + "ArraySizeInBytes(" + sb.toString() + "))";
         }
         else if("CEIL".equals(vl.getName())) {
-            VariableLiteral va = (VariableLiteral) vl.getArgs().get(0);
+            Term va = vl.getArgs().get(0);
             // The Ceil function expects 64 bit floating point values.
-            TypeReference tr = new DefaultFloatTypeReference(64);
-            return "math.Ceil(" + toVariableExpression(tr, va, parserArguments, serializerArguments, true, suppressPointerAccess);
+            TypeReference tr = new DefaultFloatTypeReference();
+            return "math.Ceil(" + toExpression(tr, va, parserArguments, serializerArguments, serialize, suppressPointerAccess) + ")";
         }
         // All uppercase names are not fields, but utility methods.
         else if (vl.getName().equals(vl.getName().toUpperCase())) {
@@ -702,6 +709,12 @@ public class GoLanguageTemplateHelper extends BaseFreemarkerLanguageTemplateHelp
             imports.add("\"strconv\"");
         }
 
+        // For CEIL functions: "math"
+        if(((ComplexTypeDefinition) getThisTypeDefinition()).getFields().stream().anyMatch(field ->
+            FieldUtils.contains(field, "CEIL"))) {
+            imports.add("\"math\"");
+        }
+
         return imports;
     }
 
diff --git a/build-utils/language-go/src/main/java/org/apache/plc4x/language/go/hack/DefaultFloatTypeReference.java b/build-utils/language-go/src/main/java/org/apache/plc4x/language/go/hack/DefaultFloatTypeReference.java
index 1367abf..239f99c 100644
--- a/build-utils/language-go/src/main/java/org/apache/plc4x/language/go/hack/DefaultFloatTypeReference.java
+++ b/build-utils/language-go/src/main/java/org/apache/plc4x/language/go/hack/DefaultFloatTypeReference.java
@@ -18,15 +18,9 @@ under the License.
 */
 package org.apache.plc4x.language.go.hack;
 
-import org.apache.plc4x.plugins.codegenerator.types.references.IntegerTypeReference;
+import org.apache.plc4x.plugins.codegenerator.types.references.FloatTypeReference;
 
-public class DefaultFloatTypeReference implements IntegerTypeReference {
-
-    private final int sizeInBits;
-
-    public DefaultFloatTypeReference(int sizeInBits) {
-        this.sizeInBits = sizeInBits;
-    }
+public class DefaultFloatTypeReference implements FloatTypeReference {
 
     @Override
     public SimpleBaseType getBaseType() {
@@ -35,7 +29,17 @@ public class DefaultFloatTypeReference implements IntegerTypeReference {
 
     @Override
     public int getSizeInBits() {
-        return sizeInBits;
+        return 64;
+    }
+
+    @Override
+    public int getExponent() {
+        return 11;
+    }
+
+    @Override
+    public int getMantissa() {
+        return 52;
     }
 
 }
diff --git a/build-utils/language-go/src/main/java/org/apache/plc4x/language/go/utils/FieldUtils.java b/build-utils/language-go/src/main/java/org/apache/plc4x/language/go/utils/FieldUtils.java
new file mode 100644
index 0000000..4ff81bd
--- /dev/null
+++ b/build-utils/language-go/src/main/java/org/apache/plc4x/language/go/utils/FieldUtils.java
@@ -0,0 +1,103 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+package org.apache.plc4x.language.go.utils;
+
+import org.apache.plc4x.plugins.codegenerator.types.fields.*;
+import org.apache.plc4x.plugins.codegenerator.types.terms.Term;
+
+public class FieldUtils {
+
+    public static boolean contains(Field field, String label) {
+        switch(field.getTypeName()) {
+            case "array": {
+                ArrayField arrayField = (ArrayField) field;
+                return arrayField.getLoopExpression().contains(label);
+            }
+            case "checksum": {
+                ChecksumField checksumField = (ChecksumField) field;
+                return checksumField.getChecksumExpression().contains(label);
+            }
+            case "const": {
+                ConstField constField = (ConstField) field;
+                return false;
+            }
+            case "discriminator": {
+                DiscriminatorField discriminatorField = (DiscriminatorField) field;
+                return false;
+            }
+            case "enum": {
+                EnumField enumField = (EnumField) field;
+                return false;
+            }
+            case "implicit": {
+                ImplicitField implicitField = (ImplicitField) field;
+                return implicitField.getSerializeExpression().contains(label);
+            }
+            case "manualArray": {
+                ManualArrayField manualArrayField = (ManualArrayField) field;
+                return
+                    manualArrayField.getSerializeExpression().contains(label) ||
+                        manualArrayField.getParseExpression().contains(label) ||
+                        manualArrayField.getLoopExpression().contains(label) ||
+                        manualArrayField.getLengthExpression().contains(label);
+            }
+            case "manual": {
+                ManualField manualField = (ManualField) field;
+                return
+                    manualField.getSerializeExpression().contains(label) ||
+                        manualField.getParseExpression().contains(label) ||
+                        manualField.getLengthExpression().contains(label);
+            }
+            case "optional": {
+                OptionalField optionalField = (OptionalField) field;
+                return optionalField.getConditionExpression().contains(label);
+            }
+            case "padding": {
+                PaddingField paddingField = (PaddingField) field;
+                return
+                    paddingField.getPaddingCondition().contains(label) ||
+                        paddingField.getPaddingValue().contains(label);
+            }
+            case "reserved": {
+                ReservedField reservedField = (ReservedField) field;
+                return false;
+            }
+            case "simple": {
+                SimpleField simpleField = (SimpleField) field;
+                return false;
+            }
+            case "switch": {
+                SwitchField switchField = (SwitchField) field;
+                for (Term discriminatorExpression : switchField.getDiscriminatorExpressions()) {
+                    if(discriminatorExpression.contains(label)) {
+                        return true;
+                    }
+                }
+                return false;
+            }
+            case "virtual": {
+                VirtualField virtualField = (VirtualField) field;
+                return virtualField.getValueExpression().contains(label);
+            }
+        }
+        return false;
+    }
+
+
+}
diff --git a/build-utils/language-go/src/main/resources/templates/go/enum-template.ftlh b/build-utils/language-go/src/main/resources/templates/go/enum-template.ftlh
index 536e5c6..29430be 100644
--- a/build-utils/language-go/src/main/resources/templates/go/enum-template.ftlh
+++ b/build-utils/language-go/src/main/resources/templates/go/enum-template.ftlh
@@ -64,7 +64,7 @@ returning the constant for a given enum value.
 <#if type.constantNames?has_content>
     <#list type.constantNames as constantName>
 
-func (e ${type.name}) Get${constantName?cap_first}() ${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName))} {
+func (e ${type.name}) ${constantName?cap_first}() ${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName))} {
     switch e  {
         <#list helper.getUniqueEnumValues(type.enumValues) as enumValue>
         case ${helper.escapeValue(type.type, enumValue.value)}: { /* '${enumValue.value}' */
diff --git a/sandbox/plc4go/internal/plc4go/modbus/readwrite/model/ModbusDataType.go b/sandbox/plc4go/internal/plc4go/modbus/readwrite/model/ModbusDataType.go
index 56879d6..5c272e3 100644
--- a/sandbox/plc4go/internal/plc4go/modbus/readwrite/model/ModbusDataType.go
+++ b/sandbox/plc4go/internal/plc4go/modbus/readwrite/model/ModbusDataType.go
@@ -53,7 +53,7 @@ const (
 	ModbusDataType_WSTRING        ModbusDataType = 83
 )
 
-func (e ModbusDataType) GetDataTypeSize() uint8 {
+func (e ModbusDataType) DataTypeSize() uint8 {
 	switch e {
 	case 00:
 		{ /* '00' */
diff --git a/sandbox/plc4go/internal/plc4go/s7/readwrite/model/COTPTpduSize.go b/sandbox/plc4go/internal/plc4go/s7/readwrite/model/COTPTpduSize.go
index 4f8e907..34f3a7d 100644
--- a/sandbox/plc4go/internal/plc4go/s7/readwrite/model/COTPTpduSize.go
+++ b/sandbox/plc4go/internal/plc4go/s7/readwrite/model/COTPTpduSize.go
@@ -32,7 +32,7 @@ const (
 	COTPTpduSize_SIZE_8192 COTPTpduSize = 0x0d
 )
 
-func (e COTPTpduSize) GetSizeInBytes() uint16 {
+func (e COTPTpduSize) SizeInBytes() uint16 {
 	switch e {
 	case 0x07:
 		{ /* '0x07' */
diff --git a/sandbox/plc4go/internal/plc4go/s7/readwrite/model/DataTransportSize.go b/sandbox/plc4go/internal/plc4go/s7/readwrite/model/DataTransportSize.go
index e5d88a7..36400cf 100644
--- a/sandbox/plc4go/internal/plc4go/s7/readwrite/model/DataTransportSize.go
+++ b/sandbox/plc4go/internal/plc4go/s7/readwrite/model/DataTransportSize.go
@@ -32,7 +32,7 @@ const (
 	DataTransportSize_OCTET_STRING    DataTransportSize = 0x09
 )
 
-func (e DataTransportSize) GetSizeInBits() bool {
+func (e DataTransportSize) SizeInBits() bool {
 	switch e {
 	case 0x00:
 		{ /* '0x00' */
diff --git a/sandbox/plc4go/internal/plc4go/s7/readwrite/model/MemoryArea.go b/sandbox/plc4go/internal/plc4go/s7/readwrite/model/MemoryArea.go
index 10ff2be..f7c0593 100644
--- a/sandbox/plc4go/internal/plc4go/s7/readwrite/model/MemoryArea.go
+++ b/sandbox/plc4go/internal/plc4go/s7/readwrite/model/MemoryArea.go
@@ -34,7 +34,7 @@ const (
 	MemoryArea_LOCAL_DATA               MemoryArea = 0x86
 )
 
-func (e MemoryArea) GetShortName() string {
+func (e MemoryArea) ShortName() string {
 	switch e {
 	case 0x1C:
 		{ /* '0x1C' */
diff --git a/sandbox/plc4go/internal/plc4go/s7/readwrite/model/S7VarPayloadDataItem.go b/sandbox/plc4go/internal/plc4go/s7/readwrite/model/S7VarPayloadDataItem.go
index f99c7cf..80aff88 100644
--- a/sandbox/plc4go/internal/plc4go/s7/readwrite/model/S7VarPayloadDataItem.go
+++ b/sandbox/plc4go/internal/plc4go/s7/readwrite/model/S7VarPayloadDataItem.go
@@ -112,8 +112,8 @@ func S7VarPayloadDataItemParse(io spi.ReadBuffer, lastItem bool) (spi.Message, e
 	var data []int8
 	// Count array
 	{
-		data := make([]int8, spi.InlineIf(transportSize.GetSizeInBits(), uint16(math.Ceil(float64(dataLength)/float64(uint16(8.0)))), uint16(dataLength)))
-		for curItem := uint16(0); curItem < uint16(spi.InlineIf(transportSize.GetSizeInBits(), uint16(math.Ceil(float64(dataLength)/float64(uint16(8.0)))), uint16(dataLength))); curItem++ {
+		data := make([]int8, spi.InlineIf(transportSize.SizeInBits(), uint16(math.Ceil(float64(dataLength)/float64(float64(8.0)))), uint16(dataLength)))
+		for curItem := uint16(0); curItem < uint16(spi.InlineIf(transportSize.SizeInBits(), uint16(math.Ceil(float64(dataLength)/float64(float64(8.0)))), uint16(dataLength))); curItem++ {
 
 			data = append(data, io.ReadInt8(8))
 		}
@@ -143,7 +143,7 @@ func (m S7VarPayloadDataItem) Serialize(io spi.WriteBuffer, lastItem bool) {
 	transportSize.Serialize(io)
 
 	// Implicit Field (dataLength) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
-	dataLength := uint16(uint16(uint16(len(m.data))) * uint16(uint16(spi.InlineIf(bool(bool((m.transportSize) == (DataTransportSize_BIT))), uint16(uint16(1)), uint16(uint16(spi.InlineIf(m.transportSize.GetSizeInBits(), uint16(uint16(8)), uint16(uint16(1)))))))))
+	dataLength := uint16(uint16(uint16(len(m.data))) * uint16(uint16(spi.InlineIf(bool(bool((m.transportSize) == (DataTransportSize_BIT))), uint16(uint16(1)), uint16(uint16(spi.InlineIf(transportSize.SizeInBits(), uint16(uint16(8)), uint16(uint16(1)))))))))
 	io.WriteUint16(16, (dataLength))
 
 	// Array Field (data)
diff --git a/sandbox/plc4go/internal/plc4go/s7/readwrite/model/TransportSize.go b/sandbox/plc4go/internal/plc4go/s7/readwrite/model/TransportSize.go
index c28e9d9..6007091 100644
--- a/sandbox/plc4go/internal/plc4go/s7/readwrite/model/TransportSize.go
+++ b/sandbox/plc4go/internal/plc4go/s7/readwrite/model/TransportSize.go
@@ -50,7 +50,7 @@ const (
 	TransportSize_DATE_AND_TIME TransportSize = 0x0F
 )
 
-func (e TransportSize) GetSupported_S7_300() bool {
+func (e TransportSize) Supported_S7_300() bool {
 	switch e {
 	case 0x00:
 		{ /* '0x00' */
@@ -123,7 +123,7 @@ func (e TransportSize) GetSupported_S7_300() bool {
 	}
 }
 
-func (e TransportSize) GetSupported_LOGO() bool {
+func (e TransportSize) Supported_LOGO() bool {
 	switch e {
 	case 0x00:
 		{ /* '0x00' */
@@ -196,7 +196,7 @@ func (e TransportSize) GetSupported_LOGO() bool {
 	}
 }
 
-func (e TransportSize) GetSizeInBytes() uint8 {
+func (e TransportSize) SizeInBytes() uint8 {
 	switch e {
 	case 0x00:
 		{ /* '0x00' */
@@ -269,7 +269,7 @@ func (e TransportSize) GetSizeInBytes() uint8 {
 	}
 }
 
-func (e TransportSize) GetSupported_S7_400() bool {
+func (e TransportSize) Supported_S7_400() bool {
 	switch e {
 	case 0x00:
 		{ /* '0x00' */
@@ -342,7 +342,7 @@ func (e TransportSize) GetSupported_S7_400() bool {
 	}
 }
 
-func (e TransportSize) GetSupported_S7_1200() bool {
+func (e TransportSize) Supported_S7_1200() bool {
 	switch e {
 	case 0x00:
 		{ /* '0x00' */
@@ -415,7 +415,7 @@ func (e TransportSize) GetSupported_S7_1200() bool {
 	}
 }
 
-func (e TransportSize) GetSizeCode() uint8 {
+func (e TransportSize) SizeCode() uint8 {
 	switch e {
 	case 0x00:
 		{ /* '0x00' */
@@ -488,7 +488,7 @@ func (e TransportSize) GetSizeCode() uint8 {
 	}
 }
 
-func (e TransportSize) GetSupported_S7_1500() bool {
+func (e TransportSize) Supported_S7_1500() bool {
 	switch e {
 	case 0x00:
 		{ /* '0x00' */
@@ -561,7 +561,7 @@ func (e TransportSize) GetSupported_S7_1500() bool {
 	}
 }
 
-func (e TransportSize) GetDataTransportSize() DataTransportSize {
+func (e TransportSize) DataTransportSize() DataTransportSize {
 	switch e {
 	case 0x00:
 		{ /* '0x00' */
@@ -634,7 +634,7 @@ func (e TransportSize) GetDataTransportSize() DataTransportSize {
 	}
 }
 
-func (e TransportSize) GetBaseType() TransportSize {
+func (e TransportSize) BaseType() TransportSize {
 	switch e {
 	case 0x00:
 		{ /* '0x00' */
@@ -707,7 +707,7 @@ func (e TransportSize) GetBaseType() TransportSize {
 	}
 }
 
-func (e TransportSize) GetDataProtocolId() uint8 {
+func (e TransportSize) DataProtocolId() uint8 {
 	switch e {
 	case 0x00:
 		{ /* '0x00' */