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


The following commit(s) were added to refs/heads/develop by this push:
     new 40ea49b06 refactor(plc4go/spi): change the API of Serialize
40ea49b06 is described below

commit 40ea49b06d7d33b761fd43133205dba910ba3dc2
Author: Sebastian Rühl <[email protected]>
AuthorDate: Wed Nov 2 12:46:23 2022 +0100

    refactor(plc4go/spi): change the API of Serialize
    
    + The default serialize now uses a WriteBufferByteBased with BigEndian (TBD 
get it from mspec)
    + The old Serialize is available under the name SerializeWithWriteBuffer
---
 .../resources/templates/go/complex-type-template.go.ftlh   | 10 +++++++++-
 .../main/resources/templates/go/data-io-template.go.ftlh   | 10 +++++++++-
 .../src/main/resources/templates/go/enum-template.go.ftlh  | 14 ++++++++++++--
 3 files changed, 30 insertions(+), 4 deletions(-)

diff --git 
a/code-generation/language-go/src/main/resources/templates/go/complex-type-template.go.ftlh
 
b/code-generation/language-go/src/main/resources/templates/go/complex-type-template.go.ftlh
index 904ddf43a..232caf0f5 100644
--- 
a/code-generation/language-go/src/main/resources/templates/go/complex-type-template.go.ftlh
+++ 
b/code-generation/language-go/src/main/resources/templates/go/complex-type-template.go.ftlh
@@ -1565,7 +1565,15 @@ func (pm *_${type.name}) SerializeParent(writeBuffer 
utils.WriteBuffer, child ${
        m := child
        _ = m
 <#else>
-func (m *_${type.name}) Serialize(writeBuffer utils.WriteBuffer) error {
+func (m *_${type.name}) Serialize() ([]byte, error) {
+       wb := 
utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.BigEndian))<@emitImport
 import="encoding/binary" /> // TODO: get endianness from mspec
+       if err := m.SerializeWithWriteBuffer(wb); err != nil {
+               return nil, err
+       }
+       return wb.GetBytes(), nil
+}
+
+func (m *_${type.name}) SerializeWithWriteBuffer(writeBuffer 
utils.WriteBuffer) error {
 </#if>
        positionAware := writeBuffer
        _ = positionAware
diff --git 
a/code-generation/language-go/src/main/resources/templates/go/data-io-template.go.ftlh
 
b/code-generation/language-go/src/main/resources/templates/go/data-io-template.go.ftlh
index a4acf66ce..b16f41eac 100644
--- 
a/code-generation/language-go/src/main/resources/templates/go/data-io-template.go.ftlh
+++ 
b/code-generation/language-go/src/main/resources/templates/go/data-io-template.go.ftlh
@@ -204,7 +204,15 @@ func ${type.name}Parse(readBuffer utils.ReadBuffer<#if 
parserArguments?has_conte
        return nil, errors.New("unsupported type")<@emitImport 
import="github.com/pkg/errors" />
 }
 
-func ${type.name}Serialize(writeBuffer utils.WriteBuffer, value 
api.PlcValue<#if parserArguments?has_content>, <#list parserArguments as 
parserArgument>${parserArgument.name} <#if 
parserArgument.type.isNonSimpleTypeReference() && 
!parserArgument.type.isEnumTypeReference()>I</#if>${helper.getLanguageTypeNameForTypeReference(parserArgument.type)}<#sep>,
 </#sep></#list></#if>) error {
+func ${type.name}Serialize(value api.PlcValue<#if 
parserArguments?has_content>, <#list parserArguments as 
parserArgument>${parserArgument.name} <#if 
parserArgument.type.isNonSimpleTypeReference() && 
!parserArgument.type.isEnumTypeReference()>I</#if>${helper.getLanguageTypeNameForTypeReference(parserArgument.type)}<#sep>,
 </#sep></#list></#if>) ([]byte, error) {
+       wb := 
utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.BigEndian))<@emitImport
 import="encoding/binary" /> // TODO: get endianness from mspec
+       if err := ${type.name}SerializeWithWriteBuffer(wb, value<#if 
parserArguments?has_content>, <#list parserArguments as 
parserArgument>${parserArgument.name}<#sep>, </#sep></#list></#if>); err != nil 
{
+               return nil, err
+       }
+       return wb.GetBytes(), nil
+}
+
+func ${type.name}SerializeWithWriteBuffer(writeBuffer utils.WriteBuffer, value 
api.PlcValue<#if parserArguments?has_content>, <#list parserArguments as 
parserArgument>${parserArgument.name} <#if 
parserArgument.type.isNonSimpleTypeReference() && 
!parserArgument.type.isEnumTypeReference()>I</#if>${helper.getLanguageTypeNameForTypeReference(parserArgument.type)}<#sep>,
 </#sep></#list></#if>) error {
        <#if parserArguments?has_content>
        m := struct {
        <#list parserArguments as parserArgument>
diff --git 
a/code-generation/language-go/src/main/resources/templates/go/enum-template.go.ftlh
 
b/code-generation/language-go/src/main/resources/templates/go/enum-template.go.ftlh
index e46efc9c9..83daad774 100644
--- 
a/code-generation/language-go/src/main/resources/templates/go/enum-template.go.ftlh
+++ 
b/code-generation/language-go/src/main/resources/templates/go/enum-template.go.ftlh
@@ -49,6 +49,8 @@ ${helper.fileName(protocolName, languageName, 
outputFlavor)?replace(".", "/")}/m
 package model
 
 import (
+       "encoding/binary"
+
        "github.com/apache/plc4x/plc4go/spi/utils"
        "github.com/pkg/errors"
 )
@@ -60,12 +62,12 @@ import (
 type ${type.name} ${baseType}
 
 type I${type.name} interface {
+       utils.Serializable
 <#if type.constantNames?has_content>
        <#list type.constantNames as constantName>
        ${constantName?cap_first}() 
${helper.getLanguageTypeNameForTypeReference(type.getConstantType(constantName))}
        </#list>
 </#if>
-       Serialize(writeBuffer utils.WriteBuffer) error
 }
 
 const(
@@ -190,7 +192,15 @@ func ${type.name}Parse(readBuffer utils.ReadBuffer) 
(${type.name}, error) {
        }
 }
 
-func (e ${type.name}) Serialize(writeBuffer utils.WriteBuffer) error {
+func (e ${type.name}) Serialize() ([]byte, error) {
+       wb := 
utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.BigEndian))
 // TODO: get endianness from mspec
+       if err := e.SerializeWithWriteBuffer(wb); err != nil {
+               return nil, err
+       }
+       return wb.GetBytes(), nil
+}
+
+func (e ${type.name}) SerializeWithWriteBuffer(writeBuffer utils.WriteBuffer) 
error {
        return ${helper.getWriteBufferWriteMethodCall(type.name, 
type.type.orElseThrow(), 
helper.getLanguageTypeNameForTypeReference(type.type.orElseThrow()) + "(e)", 
null, "utils.WithAdditionalStringRepresentation(e.PLC4XEnumName())")}
 }
        </#if>

Reply via email to