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

cdutz pushed a commit to branch feat/code-gen-1.9
in repository https://gitbox.apache.org/repos/asf/plc4x-build-tools.git


The following commit(s) were added to refs/heads/feat/code-gen-1.9 by this push:
     new 9d7f021  feat: Added an interface for constants definitions.
9d7f021 is described below

commit 9d7f02189dc3fc631672eaadd575c9ee664c9a98
Author: Christofer Dutz <[email protected]>
AuthorDate: Thu Aug 28 11:40:20 2025 +0200

    feat: Added an interface for constants definitions.
---
 .../types/definitions/ConstantsTypeDefinition.java | 97 ++++++++++++++++++++++
 1 file changed, 97 insertions(+)

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();
+    }
+
+}

Reply via email to