This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x-build-tools.git
The following commit(s) were added to refs/heads/develop by this push:
new 3aab52f Improve the code generation even more ... (#132)
3aab52f is described below
commit 3aab52fd5ff3cb7c4306ff67aa87eea499db73d7
Author: Christofer Dutz <[email protected]>
AuthorDate: Fri Aug 29 10:17:49 2025 +0200
Improve the code generation even more ... (#132)
* feat: Added an interface for constants definitions.
* feat: Added support for "state" fields.
* feat: add field conversions for StateField
* chore: Removed a not needed import.
---------
Co-authored-by: Sebastian Rühl <[email protected]>
---
code-generation/RELEASE_NOTES | 2 +
.../types/definitions/ComplexTypeDefinition.java | 7 ++
.../types/definitions/ConstantsTypeDefinition.java | 97 ++++++++++++++++++++++
.../types/fields/FieldConversions.java | 16 +++-
.../codegenerator/types/fields/StateField.java | 31 +++++++
5 files changed, 152 insertions(+), 1 deletion(-)
diff --git a/code-generation/RELEASE_NOTES b/code-generation/RELEASE_NOTES
index 4d054c5..ad8a870 100644
--- a/code-generation/RELEASE_NOTES
+++ b/code-generation/RELEASE_NOTES
@@ -3,6 +3,8 @@
New Features
------------
+- Added a new type for 'constants'
+- Added a new field type for 'state'
Incompatible changes
--------------------
diff --git
a/code-generation/types-base/src/main/java/org/apache/plc4x/plugins/codegenerator/types/definitions/ComplexTypeDefinition.java
b/code-generation/types-base/src/main/java/org/apache/plc4x/plugins/codegenerator/types/definitions/ComplexTypeDefinition.java
index 96524c8..be13443 100644
---
a/code-generation/types-base/src/main/java/org/apache/plc4x/plugins/codegenerator/types/definitions/ComplexTypeDefinition.java
+++
b/code-generation/types-base/src/main/java/org/apache/plc4x/plugins/codegenerator/types/definitions/ComplexTypeDefinition.java
@@ -63,6 +63,13 @@ public interface ComplexTypeDefinition extends
TypeDefinition {
*/
List<VirtualField> getAllVirtualFields();
+ /**
+ * Get only the fields which are of type StateField.
+ *
+ * @return all state fields
+ */
+ List<StateField> getStateFields();
+
/**
* Get only the fields which are of type ConstField.
*
diff --git
a/code-generation/types-base/src/main/java/org/apache/plc4x/plugins/codegenerator/types/definitions/ConstantsTypeDefinition.java
b/code-generation/types-base/src/main/java/org/apache/plc4x/plugins/codegenerator/types/definitions/ConstantsTypeDefinition.java
new file mode 100644
index 0000000..f935086
--- /dev/null
+++
b/code-generation/types-base/src/main/java/org/apache/plc4x/plugins/codegenerator/types/definitions/ConstantsTypeDefinition.java
@@ -0,0 +1,97 @@
+/*
+ * 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
+ *
+ * https://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.plugins.codegenerator.types.definitions;
+
+import org.apache.plc4x.plugins.codegenerator.types.fields.*;
+import org.apache.plc4x.plugins.codegenerator.types.references.TypeReference;
+
+import java.util.*;
+
+public interface ConstantsTypeDefinition extends TypeDefinition {
+
+ /**
+ * Get all fields no matter the type.
+ *
+ * @return all fields
+ */
+ List<Field> getFields();
+
+ /**
+ * Get only the fields which are of type ConstField.
+ *
+ * @return all constant fields
+ */
+ List<ConstField> getConstFields();
+
+ /**
+ * Get only the fields which are of type SimpleField or OptionalField.
+ *
+ * @return all simple and optional fields
+ */
+ List<PropertyField> getPropertyFields();
+
+ /**
+ * Returns a {@link NamedField} defined by {@code fieldName}.
+ *
+ * @param fieldName the fieldName to search for
+ * @return {@link NamedField} if found.
+ */
+ default Optional<NamedField> getNamedFieldByName(String fieldName) {
+ return getFields().stream()
+ .filter(FieldConversions::isNamedField)
+ .map(field -> (NamedField) field)
+ .filter(namedField -> namedField.getName().equals(fieldName))
+ .findFirst();
+ }
+
+ /**
+ * Returns a {@link PropertyField} defined by {@code fieldName}.
+ *
+ * @param fieldName the fieldName to search for
+ * @return {@link PropertyField} if found.
+ */
+ default Optional<PropertyField> getPropertyFieldByName(String fieldName) {
+ return getPropertyFields().stream()
+ .filter(propertyField ->
propertyField.getName().equals(fieldName))
+ .findFirst();
+ }
+
+ /**
+ * Return the {@link TypeReference} of a given property.
+ *
+ * @param propertyName name of the property
+ * @return the type reference of the given property
+ */
+ default Optional<TypeReference> getTypeReferenceForProperty(String
propertyName) {
+ Objects.requireNonNull(propertyName);
+ // If this is a built-in type, use that.
+ if (BuiltIns.builtInFields.containsKey(propertyName)) {
+ return Optional.of(BuiltIns.builtInFields.get(propertyName));
+ }
+ // Check if the expression root is referencing a field
+ final Optional<PropertyField> propertyFieldOptional =
getPropertyFields().stream()
+ .filter(propertyField ->
propertyField.getName().equals(propertyName))
+ .findFirst();
+ if (propertyFieldOptional.isPresent()) {
+ return propertyFieldOptional.map(PropertyField::getType);
+ }
+ return Optional.empty();
+ }
+
+}
diff --git
a/code-generation/types-base/src/main/java/org/apache/plc4x/plugins/codegenerator/types/fields/FieldConversions.java
b/code-generation/types-base/src/main/java/org/apache/plc4x/plugins/codegenerator/types/fields/FieldConversions.java
index 13fccd1..65c6747 100644
---
a/code-generation/types-base/src/main/java/org/apache/plc4x/plugins/codegenerator/types/fields/FieldConversions.java
+++
b/code-generation/types-base/src/main/java/org/apache/plc4x/plugins/codegenerator/types/fields/FieldConversions.java
@@ -262,6 +262,20 @@ public interface FieldConversions {
return
Optional.of(this).filter(SimpleField.class::isInstance).map(SimpleField.class::cast);
}
+ /**
+ * @return true if {@code this} is instance of {@link StateField}
+ */
+ default boolean isStateField() {
+ return this instanceof StateField;
+ }
+
+ /**
+ * @return a {@link StateField} if castable.
+ */
+ default Optional<StateField> asStateField() {
+ return
Optional.of(this).filter(StateField.class::isInstance).map(StateField.class::cast);
+ }
+
/**
* @return true if {@code this} is instance of {@link SwitchField}
*/
@@ -317,7 +331,7 @@ public interface FieldConversions {
default Optional<VirtualField> asVirtualField() {
return
Optional.of(this).filter(VirtualField.class::isInstance).map(VirtualField.class::cast);
}
-
+
/**
* @return true if {@code this} is instance of {@link ValidationField}
*/
diff --git
a/code-generation/types-base/src/main/java/org/apache/plc4x/plugins/codegenerator/types/fields/StateField.java
b/code-generation/types-base/src/main/java/org/apache/plc4x/plugins/codegenerator/types/fields/StateField.java
new file mode 100644
index 0000000..370ef3c
--- /dev/null
+++
b/code-generation/types-base/src/main/java/org/apache/plc4x/plugins/codegenerator/types/fields/StateField.java
@@ -0,0 +1,31 @@
+/*
+ * 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
+ *
+ * https://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.plugins.codegenerator.types.fields;
+
+import org.apache.plc4x.plugins.codegenerator.types.terms.Term;
+
+public interface StateField extends PropertyField {
+
+ default String getTypeName() {
+ return "state";
+ }
+
+ Term getValueExpression();
+
+}