Skip to content

Commit

Permalink
refactor(plc4go/spi): change the API of Serialize
Browse files Browse the repository at this point in the history
+ The default serialize now uses a WriteBufferByteBased with BigEndian (TBD get it from mspec)
+ The old Serialize is available under the name SerializeWithWriteBuffer
  • Loading branch information
sruehl committed Nov 2, 2022
1 parent f2510c2 commit 40ea49b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Expand Up @@ -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
Expand Down
Expand Up @@ -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>
Expand Down
Expand Up @@ -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"
)
Expand All @@ -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(
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit 40ea49b

Please sign in to comment.