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 e181a01f4e fix(plc4go/zerolog): JSON mode should now produce
unindented json
e181a01f4e is described below
commit e181a01f4e96e91e8cba538ffd7a6555de09446e
Author: Sebastian Rühl <[email protected]>
AuthorDate: Fri Nov 21 09:44:20 2025 +0100
fix(plc4go/zerolog): JSON mode should now produce unindented json
---
plc4go/pkg/api/logging/ZerologInterfaceMarshal.go | 13 ++++--
plc4go/spi/utils/WriteBufferBoxBased.go | 24 ++++++++++++
plc4go/spi/utils/WriteBufferJsonBased.go | 41 +++++++++++++++----
plc4go/spi/utils/WriteBufferJsonBased_test.go | 30 --------------
plc4go/spi/utils/WriteBufferXmlBased.go | 48 +++++++++++++++++------
plc4go/spi/utils/WriteBufferXmlBased_test.go | 32 ---------------
6 files changed, 104 insertions(+), 84 deletions(-)
diff --git a/plc4go/pkg/api/logging/ZerologInterfaceMarshal.go
b/plc4go/pkg/api/logging/ZerologInterfaceMarshal.go
index bc8d249843..26793d1e20 100644
--- a/plc4go/pkg/api/logging/ZerologInterfaceMarshal.go
+++ b/plc4go/pkg/api/logging/ZerologInterfaceMarshal.go
@@ -55,7 +55,9 @@ var ZerologMessageInterfaceMarshalFunc = func(v interface{})
([]byte, error) {
if plcMessage, ok := v.(utils.Serializable); ok {
switch ZerologInterfacePLCMessageFormat {
case PLCMessageAsJSON:
- jsonWriteBuffer := utils.NewJsonWriteBuffer()
+ jsonWriteBuffer := utils.NewJsonWriteBuffer(
+ utils.WithJsonWriteBufferDefaultIdent(false),
+ )
if err :=
plcMessage.SerializeWithWriteBuffer(context.Background(), jsonWriteBuffer); err
!= nil {
return nil, errors.Wrap(err, "error serializing
PLCMessage")
}
@@ -67,14 +69,19 @@ var ZerologMessageInterfaceMarshalFunc = func(v
interface{}) ([]byte, error) {
case PLCMessageAsString:
boxWriteBuffer := utils.NewWriteBufferBoxBased(
utils.WithWriteBufferBoxBasedMergeSingleBoxes(),
- utils.WithWriteBufferBoxBasedOmitEmptyBoxes())
+ utils.WithWriteBufferBoxBasedOmitEmptyBoxes(),
+ utils.WithWriteBufferBoxBasedDesiredWidth(200),
+
utils.WithWriteBufferBoxBasedPrintPosLengthFooter(),
+ )
if err :=
plcMessage.SerializeWithWriteBuffer(context.Background(), boxWriteBuffer); err
!= nil {
return nil, errors.Wrap(err, "error serializing
PLCMessage")
}
boxString := boxWriteBuffer.GetBox().String()
return json.Marshal(boxString)
case PLCMessageAsXML:
- xmlWriteBuffer := utils.NewXmlWriteBuffer()
+ xmlWriteBuffer := utils.NewXmlWriteBuffer(
+ utils.WithXmlWriteBufferDefaultIdent(false),
+ )
if err :=
plcMessage.SerializeWithWriteBuffer(context.Background(), xmlWriteBuffer); err
!= nil {
return nil, errors.Wrap(err, "error serializing
PLCMessage")
}
diff --git a/plc4go/spi/utils/WriteBufferBoxBased.go
b/plc4go/spi/utils/WriteBufferBoxBased.go
index 32ba6d32ae..7f97954328 100644
--- a/plc4go/spi/utils/WriteBufferBoxBased.go
+++ b/plc4go/spi/utils/WriteBufferBoxBased.go
@@ -61,6 +61,30 @@ func WithWriteBufferBoxBasedOmitEmptyBoxes()
func(*boxedWriteBuffer) {
}
}
+// WithWriteBufferBoxBasedDesiredWidth sets the desired width of the output.
+// If the output exceeds this width, it will be wrapped.
+func WithWriteBufferBoxBasedDesiredWidth(width int) func(*boxedWriteBuffer) {
+ return func(wb *boxedWriteBuffer) {
+ wb.desiredWidth = width
+ wb.currentWidth = width - 2
+ }
+}
+
+// WithWriteBufferBoxBasedAsciiBoxWriter sets the AsciiBoxWriter to use for
rendering the output.
+func WithWriteBufferBoxBasedAsciiBoxWriter(writer AsciiBoxWriter)
func(*boxedWriteBuffer) {
+ return func(wb *boxedWriteBuffer) {
+ wb.asciiBoxWriter = writer
+ }
+}
+
+// WithWriteBufferBoxBasedAsciiBoxWriterLight sets the AsciiBoxWriter to use
for rendering the light output.
+func WithWriteBufferBoxBasedAsciiBoxWriterLight(writer AsciiBoxWriter)
func(*boxedWriteBuffer) {
+ return func(wb *boxedWriteBuffer) {
+ wb.asciiBoxWriterLight = writer
+ }
+}
+
+// WithWriteBufferBoxBasedPrintPosLengthFooter enables printing the position
and length of the current box at the end of the box.
func WithWriteBufferBoxBasedPrintPosLengthFooter() func(*boxedWriteBuffer) {
return func(wb *boxedWriteBuffer) {
wb.printPosLengthFooter = true
diff --git a/plc4go/spi/utils/WriteBufferJsonBased.go
b/plc4go/spi/utils/WriteBufferJsonBased.go
index a426fea2ff..31f4febae3 100644
--- a/plc4go/spi/utils/WriteBufferJsonBased.go
+++ b/plc4go/spi/utils/WriteBufferJsonBased.go
@@ -35,18 +35,45 @@ type WriteBufferJsonBased interface {
GetJsonString() (string, error)
}
-func NewJsonWriteBuffer() WriteBufferJsonBased {
- return NewJsonWriteBufferWithOptions(true)
-}
-
-func NewJsonWriteBufferWithOptions(renderAttr bool) WriteBufferJsonBased {
+// NewJsonWriteBuffer creates a new WriteBufferJsonBased with renders all
information into json
+func NewJsonWriteBuffer(opts ...func(*jsonWriteBuffer)) WriteBufferJsonBased {
var jsonString strings.Builder
encoder := json.NewEncoder(&jsonString)
encoder.SetIndent("", " ")
- return &jsonWriteBuffer{
+ j := &jsonWriteBuffer{
jsonString: &jsonString,
Encoder: encoder,
- doRenderAttr: renderAttr,
+ doRenderAttr: true,
+ }
+ for _, opt := range opts {
+ opt(j)
+ }
+ return j
+}
+
+// WithJsonWriteBufferDefaultIdent configures the jsonWriteBuffer to use
default indentation
+func WithJsonWriteBufferDefaultIdent(defaultIndent bool)
func(*jsonWriteBuffer) {
+ return func(x *jsonWriteBuffer) {
+ if defaultIndent {
+ x.Encoder.SetIndent("", " ")
+ return
+ } else {
+ x.Encoder.SetIndent("", "")
+ }
+ }
+}
+
+// WithJsonWriteBufferIdent configures the jsonWriteBuffer to use the given
indentation
+func WithJsonWriteBufferIdent(indent string) func(*jsonWriteBuffer) {
+ return func(x *jsonWriteBuffer) {
+ x.Encoder.SetIndent("", indent)
+ }
+}
+
+// WithJsonWriteBufferRenderAttr configures the jsonWriteBuffer to render
attributes
+func WithJsonWriteBufferRenderAttr(renderAttr bool) func(*jsonWriteBuffer) {
+ return func(x *jsonWriteBuffer) {
+ x.doRenderAttr = renderAttr
}
}
diff --git a/plc4go/spi/utils/WriteBufferJsonBased_test.go
b/plc4go/spi/utils/WriteBufferJsonBased_test.go
index 1961be0bbb..0db6c8f69b 100644
--- a/plc4go/spi/utils/WriteBufferJsonBased_test.go
+++ b/plc4go/spi/utils/WriteBufferJsonBased_test.go
@@ -56,36 +56,6 @@ func TestNewJsonWriteBuffer(t *testing.T) {
}
}
-func TestNewJsonWriteBufferWithOptions(t *testing.T) {
- type args struct {
- renderAttr bool
- }
- tests := []struct {
- name string
- args args
- want WriteBufferJsonBased
- }{
- {
- name: "create it",
- want: func() WriteBufferJsonBased {
- var jsonString strings.Builder
- encoder := json.NewEncoder(&jsonString)
- encoder.SetIndent("", " ")
- return &jsonWriteBuffer{
- jsonString: &jsonString,
- Encoder: encoder,
- doRenderAttr: false,
- }
- }(),
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- assert.Equalf(t, tt.want,
NewJsonWriteBufferWithOptions(tt.args.renderAttr),
"NewJsonWriteBufferWithOptions(%v)", tt.args.renderAttr)
- })
- }
-}
-
func Test_jsonWriteBuffer_GetJsonString(t *testing.T) {
type fields struct {
BufferCommons BufferCommons
diff --git a/plc4go/spi/utils/WriteBufferXmlBased.go
b/plc4go/spi/utils/WriteBufferXmlBased.go
index 5772963fde..804401d6ed 100644
--- a/plc4go/spi/utils/WriteBufferXmlBased.go
+++ b/plc4go/spi/utils/WriteBufferXmlBased.go
@@ -35,28 +35,52 @@ type WriteBufferXmlBased interface {
}
// NewXmlWriteBuffer returns a WriteBufferXmlBased which renders all
information into xml
-func NewXmlWriteBuffer() WriteBufferXmlBased {
+func NewXmlWriteBuffer(opts ...func(*xmlWriteBuffer)) WriteBufferXmlBased {
var xmlString strings.Builder
encoder := xml.NewEncoder(&xmlString)
encoder.Indent("", " ")
- return &xmlWriteBuffer{
+ x := &xmlWriteBuffer{
xmlString: &xmlString,
Encoder: encoder,
doRenderLists: true,
doRenderAttr: true,
}
+ for _, opt := range opts {
+ opt(x)
+ }
+ return x
}
-// 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,
+// WithXmlWriteBufferDefaultIdent configures the xmlWriteBuffer to use default
indentation
+func WithXmlWriteBufferDefaultIdent(defaultIndent bool) func(*xmlWriteBuffer) {
+ return func(x *xmlWriteBuffer) {
+ if defaultIndent {
+ x.Encoder.Indent("", " ")
+ return
+ } else {
+ x.Encoder.Indent("", "")
+ }
+ }
+}
+
+// WithXmlWriteBufferIdent configures the xmlWriteBuffer to use the given
indentation
+func WithXmlWriteBufferIdent(indent string) func(*xmlWriteBuffer) {
+ return func(x *xmlWriteBuffer) {
+ x.Encoder.Indent("", indent)
+ }
+}
+
+// WithXmlWriteBufferRenderLists configures the xmlWriteBuffer to render lists
+func WithXmlWriteBufferRenderLists(renderLists bool) func(*xmlWriteBuffer) {
+ return func(x *xmlWriteBuffer) {
+ x.doRenderLists = renderLists
+ }
+}
+
+// WithXmlWriteBufferRenderAttr configures the xmlWriteBuffer to render
attributes
+func WithXmlWriteBufferRenderAttr(renderAttr bool) func(*xmlWriteBuffer) {
+ return func(x *xmlWriteBuffer) {
+ x.doRenderAttr = renderAttr
}
}
diff --git a/plc4go/spi/utils/WriteBufferXmlBased_test.go
b/plc4go/spi/utils/WriteBufferXmlBased_test.go
index 15dd1d5a8d..31da442768 100644
--- a/plc4go/spi/utils/WriteBufferXmlBased_test.go
+++ b/plc4go/spi/utils/WriteBufferXmlBased_test.go
@@ -30,38 +30,6 @@ import (
"github.com/stretchr/testify/assert"
)
-func TestNewConfiguredXmlWriteBuffer(t *testing.T) {
- type args struct {
- renderLists bool
- renderAttr bool
- }
- tests := []struct {
- name string
- args args
- want WriteBufferXmlBased
- }{
- {
- name: "create it",
- want: func() WriteBufferXmlBased {
- var xmlString strings.Builder
- encoder := xml.NewEncoder(&xmlString)
- encoder.Indent("", " ")
- return &xmlWriteBuffer{
- xmlString: &xmlString,
- Encoder: encoder,
- doRenderLists: false,
- doRenderAttr: false,
- }
- }(),
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- assert.Equalf(t, tt.want,
NewConfiguredXmlWriteBuffer(tt.args.renderLists, tt.args.renderAttr),
"NewConfiguredXmlWriteBuffer(%v, %v)", tt.args.renderLists, tt.args.renderAttr)
- })
- }
-}
-
func TestNewXmlWriteBuffer(t *testing.T) {
tests := []struct {
name string