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 6891b70  plc4go: fixed json rendering of padding fields
6891b70 is described below

commit 6891b70b0bc4689aa44683fcdb74c4fedc41af2d
Author: Sebastian Rühl <[email protected]>
AuthorDate: Wed Apr 21 09:06:45 2021 +0200

    plc4go: fixed json rendering of padding fields
    
    + added options for rendering/validation of reader/writers
---
 .../resources/templates/go/model-template.go.ftlh  |  8 ++--
 go.mod                                             |  2 +-
 go.sum                                             |  2 +
 .../s7/readwrite/model/S7VarPayloadDataItem.go     |  8 ++--
 plc4go/internal/plc4go/s7/s7Io_test.go             | 10 ++---
 .../plc4go/spi/utils/ReadBufferXmlBased.go         | 44 ++++++++++++++++++++--
 .../plc4go/spi/utils/WriteBufferJsonBased.go       | 27 ++++++++++---
 .../plc4go/spi/utils/WriteBufferXmlBased.go        | 44 ++++++++++++++++++++--
 plc4go/internal/plc4go/spi/utils/bufferCommons.go  |  1 +
 9 files changed, 120 insertions(+), 26 deletions(-)

diff --git 
a/build-utils/language-go/src/main/resources/templates/go/model-template.go.ftlh
 
b/build-utils/language-go/src/main/resources/templates/go/model-template.go.ftlh
index af2abd8..ba03f8e 100644
--- 
a/build-utils/language-go/src/main/resources/templates/go/model-template.go.ftlh
+++ 
b/build-utils/language-go/src/main/resources/templates/go/model-template.go.ftlh
@@ -794,7 +794,7 @@ func ${type.name}Parse(io utils.ReadBuffer<#if 
type.parserArguments?has_content>
 
        // Padding Field (padding)
        {
-               io.PullContext("padding")
+               io.PullContext("padding", utils.WithRenderAsList(true))
                _timesPadding := (${helper.toParseExpression(paddingField, 
paddingField.paddingCondition, type.parserArguments)})
                for ;(io.HasMore(${helper.getNumBits(simpleTypeReference)})) && 
(_timesPadding > 0);_timesPadding-- {<@emitImport import="io" />
                        // Just read the padding data and ignore it
@@ -803,7 +803,7 @@ func ${type.name}Parse(io utils.ReadBuffer<#if 
type.parserArguments?has_content>
                                return nil, errors.Wrap(_err, "Error parsing 
'padding' field")<@emitImport import="github.com/pkg/errors" />
                        }
                }
-               io.CloseContext("padding")
+               io.CloseContext("padding", utils.WithRenderAsList(true))
        }
                                <#break>
                        <#case "reserved">
@@ -1082,7 +1082,7 @@ func (m *${type.name}) Serialize(io utils.WriteBuffer<#if 
helper.getSerializerAr
 
        // Padding Field (padding)
        {
-               io.PushContext("padding")
+               io.PushContext("padding", utils.WithRenderAsList(true))
                _timesPadding := 
uint8(${helper.toSerializationExpression(paddingField, 
paddingField.paddingCondition, type.parserArguments)})
                for ;_timesPadding > 0; _timesPadding-- {
                        _paddingValue := 
${helper.getLanguageTypeNameForField(field)}(${helper.toSerializationExpression(paddingField,
 paddingField.paddingValue, type.parserArguments)})
@@ -1091,7 +1091,7 @@ func (m *${type.name}) Serialize(io utils.WriteBuffer<#if 
helper.getSerializerAr
                                return errors.Wrap(_paddingErr, "Error 
serializing 'padding' field")<@emitImport import="github.com/pkg/errors" />
                        }
                }
-               io.PopContext("padding")
+               io.PopContext("padding", utils.WithRenderAsList(true))
        }
                                <#break>
                        <#case "reserved">
diff --git a/go.mod b/go.mod
index 751a141..fbb654f 100644
--- a/go.mod
+++ b/go.mod
@@ -21,6 +21,6 @@ module github.com/apache/plc4x
 go 1.15
 
 require (
-       github.com/apache/plc4x/plc4go v0.0.0-20210420175605-7ec5b56fe1cf // 
indirect
+       github.com/apache/plc4x/plc4go v0.0.0-20210420224849-3211e42cc4c9 // 
indirect
        github.com/sirupsen/logrus v1.7.0 // indirect
 )
diff --git a/go.sum b/go.sum
index c53de2f..17cc29c 100644
--- a/go.sum
+++ b/go.sum
@@ -98,6 +98,8 @@ github.com/apache/plc4x/plc4go 
v0.0.0-20210420140858-e7f2f4c6b117 h1:aJkJp07IONj
 github.com/apache/plc4x/plc4go v0.0.0-20210420140858-e7f2f4c6b117/go.mod 
h1:NqpWaMMhsu3sTm418XwsUWgmY7e2zjgpRMyddBUwmGk=
 github.com/apache/plc4x/plc4go v0.0.0-20210420175605-7ec5b56fe1cf 
h1:HYsSjhzqXX2CWIP9iF+or7q3bLJEvB3XBx146pejQ04=
 github.com/apache/plc4x/plc4go v0.0.0-20210420175605-7ec5b56fe1cf/go.mod 
h1:NqpWaMMhsu3sTm418XwsUWgmY7e2zjgpRMyddBUwmGk=
+github.com/apache/plc4x/plc4go v0.0.0-20210420224849-3211e42cc4c9 
h1:59aETf6ALxKG6/qtC3HuNrFJV1XIezBIS1AMt2lbgQI=
+github.com/apache/plc4x/plc4go v0.0.0-20210420224849-3211e42cc4c9/go.mod 
h1:NqpWaMMhsu3sTm418XwsUWgmY7e2zjgpRMyddBUwmGk=
 github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod 
h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
 github.com/davecgh/go-spew v1.1.1/go.mod 
h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/dnephin/pflag v1.0.7/go.mod 
h1:uxE91IoWURlOiTUIA8Mq5ZZkAv3dPUfZNaT80Zm7OQE=
diff --git a/plc4go/internal/plc4go/s7/readwrite/model/S7VarPayloadDataItem.go 
b/plc4go/internal/plc4go/s7/readwrite/model/S7VarPayloadDataItem.go
index 958f2d0..f23eb87 100644
--- a/plc4go/internal/plc4go/s7/readwrite/model/S7VarPayloadDataItem.go
+++ b/plc4go/internal/plc4go/s7/readwrite/model/S7VarPayloadDataItem.go
@@ -143,7 +143,7 @@ func S7VarPayloadDataItemParse(io utils.ReadBuffer, 
lastItem bool) (*S7VarPayloa
 
        // Padding Field (padding)
        {
-               io.PullContext("padding")
+               io.PullContext("padding", utils.WithRenderAsList(true))
                _timesPadding := (utils.InlineIf(lastItem, func() uint16 { 
return uint16(uint8(0)) }, func() uint16 { return 
uint16(uint8(uint8(len(data))) % uint8(uint8(2))) }))
                for ; (io.HasMore(8)) && (_timesPadding > 0); _timesPadding-- {
                        // Just read the padding data and ignore it
@@ -152,7 +152,7 @@ func S7VarPayloadDataItemParse(io utils.ReadBuffer, 
lastItem bool) (*S7VarPayloa
                                return nil, errors.Wrap(_err, "Error parsing 
'padding' field")
                        }
                }
-               io.CloseContext("padding")
+               io.CloseContext("padding", utils.WithRenderAsList(true))
        }
 
        io.CloseContext("S7VarPayloadDataItem")
@@ -205,7 +205,7 @@ func (m *S7VarPayloadDataItem) Serialize(io 
utils.WriteBuffer, lastItem bool) er
 
        // Padding Field (padding)
        {
-               io.PushContext("padding")
+               io.PushContext("padding", utils.WithRenderAsList(true))
                _timesPadding := uint8(utils.InlineIf(lastItem, func() uint16 { 
return uint16(uint8(0)) }, func() uint16 { return 
uint16(uint8(uint8(len(m.Data))) % uint8(uint8(2))) }))
                for ; _timesPadding > 0; _timesPadding-- {
                        _paddingValue := uint8(uint8(0))
@@ -214,7 +214,7 @@ func (m *S7VarPayloadDataItem) Serialize(io 
utils.WriteBuffer, lastItem bool) er
                                return errors.Wrap(_paddingErr, "Error 
serializing 'padding' field")
                        }
                }
-               io.PopContext("padding")
+               io.PopContext("padding", utils.WithRenderAsList(true))
        }
 
        io.PopContext("S7VarPayloadDataItem")
diff --git a/plc4go/internal/plc4go/s7/s7Io_test.go 
b/plc4go/internal/plc4go/s7/s7Io_test.go
index efc79dd..c3ffdef 100644
--- a/plc4go/internal/plc4go/s7/s7Io_test.go
+++ b/plc4go/internal/plc4go/s7/s7Io_test.go
@@ -169,7 +169,7 @@ func TestS7MessageBytes(t *testing.T) {
         <eot dataType="bit" bitLength="1">false</eot>
         <tpduRef dataType="uint" bitLength="7">13</tpduRef>
       </COTPPacketData>
-      <parameters>
+      <parameters isList="true">
         <COTPParameter>
           <parameterType dataType="uint" bitLength="8">192</parameterType>
           <parameterLength dataType="uint" bitLength="8">1</parameterLength>
@@ -199,7 +199,7 @@ func TestS7MessageBytes(t *testing.T) {
         </S7Parameter>
         <S7Payload>
           <S7PayloadReadVarResponse>
-            <items>
+            <items isList="true">
               <S7VarPayloadDataItem>
                 <returnCode>
                   <DataTransportErrorCode dataType="uint" bitLength="8" 
stringRepresentation="OK">255</DataTransportErrorCode>
@@ -208,10 +208,10 @@ func TestS7MessageBytes(t *testing.T) {
                   <DataTransportSize dataType="uint" bitLength="8" 
stringRepresentation="BIT">3</DataTransportSize>
                 </transportSize>
                 <dataLength dataType="uint" bitLength="16">1</dataLength>
-                <data>
+                <data isList="true">
                   <value dataType="int" bitLength="8">1</value>
                 </data>
-                <padding></padding>
+                <padding isList="true"></padding>
               </S7VarPayloadDataItem>
             </items>
           </S7PayloadReadVarResponse>
@@ -269,7 +269,7 @@ func TestS7MessageBytes(t *testing.T) {
                     "dataLength": 1,
                     "dataLength__plc4x_bitLength": 16,
                     "dataLength__plc4x_dataType": "uint",
-                    "padding": {},
+                    "padding": [],
                     "returnCode": {
                       "DataTransportErrorCode": 255,
                       "DataTransportErrorCode__plc4x_bitLength": 8,
diff --git a/plc4go/internal/plc4go/spi/utils/ReadBufferXmlBased.go 
b/plc4go/internal/plc4go/spi/utils/ReadBufferXmlBased.go
index 21e1729..1bbfda7 100644
--- a/plc4go/internal/plc4go/spi/utils/ReadBufferXmlBased.go
+++ b/plc4go/internal/plc4go/spi/utils/ReadBufferXmlBased.go
@@ -27,10 +27,23 @@ import (
        "math/big"
 )
 
+// NewXmlReadBuffer return as ReadBuffer which doesn't validate attributes and 
lists
 func NewXmlReadBuffer(reader io.Reader) ReadBuffer {
        return &xmlReadBuffer{
-               Decoder: xml.NewDecoder(reader),
-               pos:     1,
+               Decoder:        xml.NewDecoder(reader),
+               pos:            1,
+               doValidateList: false,
+               doValidateAttr: false,
+       }
+}
+
+// NewStrictXmlReadBuffer return as ReadBuffer which does validate attributes 
and lists depending on the setting
+func NewStrictXmlReadBuffer(reader io.Reader, validateAttr bool, validateList 
bool) ReadBuffer {
+       return &xmlReadBuffer{
+               Decoder:        xml.NewDecoder(reader),
+               pos:            1,
+               doValidateAttr: validateAttr,
+               doValidateList: validateList,
        }
 }
 
@@ -43,7 +56,9 @@ func NewXmlReadBuffer(reader io.Reader) ReadBuffer {
 type xmlReadBuffer struct {
        bufferCommons
        *xml.Decoder
-       pos uint
+       pos            uint
+       doValidateAttr bool
+       doValidateList bool
 }
 
 //
@@ -69,6 +84,9 @@ func (x *xmlReadBuffer) PullContext(logicalName string, 
readerArgs ...WithReader
        if startElement.Name.Local != logicalName {
                return errors.Errorf("Unexpected Start element '%s'. Expected 
'%s'", startElement.Name.Local, logicalName)
        }
+       if err := x.validateIfList(readerArgs, startElement); err != nil {
+               return err
+       }
        return nil
 }
 
@@ -291,6 +309,23 @@ func (x *xmlReadBuffer) decode(logicalName string, 
dataType string, bitLength ui
        return nil
 }
 
+func (x *xmlReadBuffer) validateIfList(readerArgs []WithReaderArgs, 
startElement xml.StartElement) error {
+       if !x.doValidateList {
+               return nil
+       }
+       if x.isToBeRenderedAsList(upcastReaderArgs(readerArgs...)...) {
+               for _, attr := range startElement.Attr {
+                       switch attr.Name.Local {
+                       case rwIsListKey:
+                               if attr.Value != "true" {
+                                       return errors.Errorf("Startelement 
should be marked as %s=true", rwIsListKey)
+                               }
+                       }
+               }
+       }
+       return nil
+}
+
 func (x *xmlReadBuffer) validateStartElement(startElement xml.StartElement, 
logicalName string, dataType string, bitLength uint8, readerArgs 
...WithReaderArgs) error {
        logicalName = x.sanitizeLogicalName(logicalName)
        if startElement.Name.Local != logicalName {
@@ -302,6 +337,9 @@ func (x *xmlReadBuffer) validateStartElement(startElement 
xml.StartElement, logi
 }
 
 func (x *xmlReadBuffer) validateAttr(attr []xml.Attr, dataType string, 
bitLength uint8, readerArgs ...WithReaderArgs) error {
+       if !x.doValidateAttr {
+               return nil
+       }
        dataTypeValidated := false
        bitLengthValidate := false
        for _, attribute := range attr {
diff --git a/plc4go/internal/plc4go/spi/utils/WriteBufferJsonBased.go 
b/plc4go/internal/plc4go/spi/utils/WriteBufferJsonBased.go
index 237b12c..f902937 100644
--- a/plc4go/internal/plc4go/spi/utils/WriteBufferJsonBased.go
+++ b/plc4go/internal/plc4go/spi/utils/WriteBufferJsonBased.go
@@ -37,8 +37,20 @@ func NewJsonWriteBuffer() WriteBufferJsonBased {
        encoder := json.NewEncoder(&jsonString)
        encoder.SetIndent("", "  ")
        return &jsonWriteBuffer{
-               jsonString: &jsonString,
-               Encoder:    encoder,
+               jsonString:   &jsonString,
+               Encoder:      encoder,
+               doRenderAttr: true,
+       }
+}
+
+func NewConfiguredJsonWriteBuffer(renderAttr bool) WriteBufferJsonBased {
+       var jsonString strings.Builder
+       encoder := json.NewEncoder(&jsonString)
+       encoder.SetIndent("", "  ")
+       return &jsonWriteBuffer{
+               jsonString:   &jsonString,
+               Encoder:      encoder,
+               doRenderAttr: renderAttr,
        }
 }
 
@@ -52,8 +64,10 @@ type jsonWriteBuffer struct {
        bufferCommons
        stack
        *json.Encoder
-       jsonString *strings.Builder
-       rootNode   interface{}
+       jsonString    *strings.Builder
+       rootNode      interface{}
+       doRenderLists bool
+       doRenderAttr  bool
 }
 
 type elementContext struct {
@@ -215,8 +229,11 @@ func (j *jsonWriteBuffer) encodeNode(logicalName string, 
value interface{}, attr
 }
 
 func (j *jsonWriteBuffer) generateAttr(logicalName string, dataType string, 
bitLength uint8, writerArgs ...WithWriterArgs) map[string]interface{} {
-       logicalName = j.sanitizeLogicalName(logicalName)
        attr := make(map[string]interface{})
+       if !j.doRenderAttr {
+               return attr
+       }
+       logicalName = j.sanitizeLogicalName(logicalName)
        attr[fmt.Sprintf("%s__plc4x_%s", logicalName, rwDataTypeKey)] = dataType
        attr[fmt.Sprintf("%s__plc4x_%s", logicalName, rwBitLengthKey)] = 
bitLength
        for _, arg := range writerArgs {
diff --git a/plc4go/internal/plc4go/spi/utils/WriteBufferXmlBased.go 
b/plc4go/internal/plc4go/spi/utils/WriteBufferXmlBased.go
index 8a0bbbd..5931061 100644
--- a/plc4go/internal/plc4go/spi/utils/WriteBufferXmlBased.go
+++ b/plc4go/internal/plc4go/spi/utils/WriteBufferXmlBased.go
@@ -31,13 +31,29 @@ type WriteBufferXmlBased interface {
        GetXmlString() string
 }
 
+//NewXmlWriteBuffer returns a WriteBufferXmlBased which renders all 
information into xml
 func NewXmlWriteBuffer() WriteBufferXmlBased {
        var xmlString strings.Builder
        encoder := xml.NewEncoder(&xmlString)
        encoder.Indent("", "  ")
        return &xmlWriteBuffer{
-               xmlString: &xmlString,
-               Encoder:   encoder,
+               xmlString:     &xmlString,
+               Encoder:       encoder,
+               doRenderLists: true,
+               doRenderAttr:  true,
+       }
+}
+
+//NewConfiguredXmlWriteBuffer returns a WriteBufferXmlBased which renders 
configured information into xml
+func NewConfiguredXmlWriteBuffer(renderLists bool, renderAttr bool) 
WriteBufferXmlBased {
+       var xmlString strings.Builder
+       encoder := xml.NewEncoder(&xmlString)
+       encoder.Indent("", "  ")
+       return &xmlWriteBuffer{
+               xmlString:     &xmlString,
+               Encoder:       encoder,
+               doRenderLists: renderLists,
+               doRenderAttr:  renderAttr,
        }
 }
 
@@ -51,6 +67,8 @@ type xmlWriteBuffer struct {
        bufferCommons
        xmlString *strings.Builder
        *xml.Encoder
+       doRenderLists bool
+       doRenderAttr  bool
 }
 
 //
@@ -59,12 +77,14 @@ type xmlWriteBuffer struct {
 ///////////////////////////////////////
 ///////////////////////////////////////
 
-func (x *xmlWriteBuffer) PushContext(logicalName string, _ ...WithWriterArgs) 
error {
+func (x *xmlWriteBuffer) PushContext(logicalName string, writerArgs 
...WithWriterArgs) error {
        // Pre-emptive flush to avoid overflow when for a long time no context 
gets popped
        if err := x.Flush(); err != nil {
                return err
        }
-       return x.EncodeToken(xml.StartElement{Name: xml.Name{Local: 
x.sanitizeLogicalName(logicalName)}})
+       attrs := make([]xml.Attr, 0)
+       attrs = x.markAsListIfRequired(writerArgs, attrs)
+       return x.EncodeToken(xml.StartElement{Name: xml.Name{Local: 
x.sanitizeLogicalName(logicalName)}, Attr: attrs})
 }
 
 func (x *xmlWriteBuffer) WriteBit(logicalName string, value bool, writerArgs 
...WithWriterArgs) error {
@@ -145,6 +165,9 @@ func (x *xmlWriteBuffer) encodeElement(logicalName string, 
value interface{}, at
 
 func (x *xmlWriteBuffer) generateAttr(dataType string, bitLength uint8, 
writerArgs ...WithWriterArgs) []xml.Attr {
        attrs := make([]xml.Attr, 2)
+       if !x.doRenderAttr {
+               return attrs
+       }
        attrs[0] = xml.Attr{
                Name:  xml.Name{Local: rwDataTypeKey},
                Value: dataType,
@@ -167,3 +190,16 @@ func (x *xmlWriteBuffer) generateAttr(dataType string, 
bitLength uint8, writerAr
        }
        return attrs
 }
+
+func (x *xmlWriteBuffer) markAsListIfRequired(writerArgs []WithWriterArgs, 
attrs []xml.Attr) []xml.Attr {
+       if !x.doRenderLists {
+               return attrs
+       }
+       if x.isToBeRenderedAsList(upcastWriterArgs(writerArgs...)...) {
+               attrs = append(attrs, xml.Attr{
+                       Name:  xml.Name{Local: rwIsListKey},
+                       Value: "true",
+               })
+       }
+       return attrs
+}
diff --git a/plc4go/internal/plc4go/spi/utils/bufferCommons.go 
b/plc4go/internal/plc4go/spi/utils/bufferCommons.go
index fcec099..5ab13d8 100644
--- a/plc4go/internal/plc4go/spi/utils/bufferCommons.go
+++ b/plc4go/internal/plc4go/spi/utils/bufferCommons.go
@@ -31,6 +31,7 @@ const (
        rwFloatKey                = "float"
        rwStringKey               = "string"
        rwEncodingKey             = "encoding"
+       rwIsListKey               = "isList"
 )
 
 type bufferCommons struct {

Reply via email to