You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2026/08/18 22:36:15 UTC

(plc4x) 01/02: fix(codegen/go): default string fields to UTF8 encoding so generated serializers stop zero-filling

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

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit d8fce4f2d3c671bd4a185c9ee459248f1a989218
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Wed Aug 19 00:21:11 2026 +0200

    fix(codegen/go): default string fields to UTF8 encoding so generated serializers stop zero-filling
    
    GoLanguageTemplateHelper.getFieldOptions() only appended codegen.WithEncoding(...)
    when a field carried an explicit stringEncoding attribute or encoding term. A plain
    vstring/string mspec field with no explicit encoding got no option at all, so the
    generated WriteString(...) call ran with an empty encoding, and
    WriteBufferByteBased.WriteString silently zero-fills the allocated space on an
    unmatched encoding. Java is unaffected because its writer defaults to UTF-8.
    
    Default such fields (STRING/VSTRING base type, including array elements) to
    codegen.WithEncoding("UTF8") when no explicit encoding is configured. Since
    getFieldOptions() is shared between the read and write factory call sites, this
    also adds the option to the read path; ReadBufferByteBased.ReadString ignores its
    reader-args entirely, so read behavior is unaffected.
    
    This closes the root cause behind two previously-sanctioned hand-edits in
    generated files: plc4go/protocols/eip/readwrite/model/AnsiExtendedSymbolSegment.go
    (symbol) and CipIdentity.go (productName), both of which only patched the write
    side by hand. Regenerating plc4go/protocols/eip/readwrite/model with the fixed
    generator confirms both files now get codegen.WithEncoding("UTF8") on read AND
    write for those fields.
    
    A full repo-wide `mvn -P update-generated-code,with-go generate-sources` run was
    attempted to refresh all plc4go generated models, but was NOT committed: the
    locally-built code-generation/language/go module (1.0.0-SNAPSHOT) has drifted
    from the generator version actually used to produce the currently-committed
    generated code (plc4x-maven-plugin 1.9.0, from the separate plc4x-build-tools
    release used by the with-go profile). Regenerating with the local module produced
    sweeping formatting/import/gofmt-style rewrites across ~3169 files, unrelated to
    the encoding fix, and even broke compilation of hand-written call sites (e.g.
    plc4go/internal/modbus/RegisterCodec.go) whose signatures no longer matched.
    That repo-wide regeneration is a separate maintainer decision. Until it happens,
    the two hand-edits in plc4go/protocols/eip stay in place, and TestEIPDriver
    continues to guard them.
---
 .../language/go/GoLanguageTemplateHelper.java      | 27 +++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/code-generation/language/go/src/main/java/org/apache/plc4x/language/go/GoLanguageTemplateHelper.java b/code-generation/language/go/src/main/java/org/apache/plc4x/language/go/GoLanguageTemplateHelper.java
index cb4d23be2c..b4e8ad2075 100644
--- a/code-generation/language/go/src/main/java/org/apache/plc4x/language/go/GoLanguageTemplateHelper.java
+++ b/code-generation/language/go/src/main/java/org/apache/plc4x/language/go/GoLanguageTemplateHelper.java
@@ -355,6 +355,23 @@ public class GoLanguageTemplateHelper extends BaseFreemarkerLanguageTemplateHelp
         return isAnTypeOfOptional && isNonComplexOrArrayElementNonComplex(field.getType());
     }
 
+    public boolean isStringTypeField(TypeReference typeReference) {
+        if (typeReference.isArrayTypeReference()) {
+            return isStringTypeField(typeReference.asArrayTypeReference().orElseThrow().getElementTypeReference());
+        }
+        if (!typeReference.isSimpleTypeReference()) {
+            return false;
+        }
+        SimpleTypeReference simpleTypeReference = typeReference.asSimpleTypeReference().orElseThrow();
+        switch (simpleTypeReference.getBaseType()) {
+            case STRING:
+            case VSTRING:
+                return true;
+            default:
+                return false;
+        }
+    }
+
     public boolean isNonComplexOrArrayElementNonComplex(TypeReference typeReference) {
         boolean isNotAnComplexTypeReference = !typeReference.isComplexTypeReference();
         boolean arrayTypeIsNotAnComplexTypeReference = !(
@@ -1889,10 +1906,18 @@ public class GoLanguageTemplateHelper extends BaseFreemarkerLanguageTemplateHelp
             final String encoding = toParseExpression(field, field.getType(), term, parserArguments);
             sb.append(", codegen.WithEncoding(").append(encoding).append(")");
         }, () -> {
-            field.getEncoding().ifPresent(term -> {
+            field.getEncoding().ifPresentOrElse(term -> {
                 emitCodegenRequiredImports();
                 final String encoding = toParseExpression(field, field.getType(), term, parserArguments);
                 sb.append(", codegen.WithEncoding(").append(encoding).append(")");
+            }, () -> {
+                // No explicit encoding was defined on the field. If it's a string field, default
+                // to UTF8 so the generated (de-)serializers don't fall back to zero-filling the
+                // allocated space (see WriteBufferByteBased.WriteString for the unmatched-encoding case).
+                if (isStringTypeField(field.getType())) {
+                    emitCodegenRequiredImports();
+                    sb.append(", codegen.WithEncoding(\"UTF8\")");
+                }
             });
         });