encoding/prototext: drop trailing newline for empty
This is more consistent with the indent documentation:
If indent is a non-empty string, it causes every entry in a List or Message
to be preceded by the indent and trailed by a newline.
Since an empty message has no entries, there should be no newlines.
Change-Id: I5d57165aaf94ca6b184bb35bf05d5d68f5ee9dd5
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/194877
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/encoding/prototext/encode_test.go b/encoding/prototext/encode_test.go
index 15e98a1..839c4f9 100644
--- a/encoding/prototext/encode_test.go
+++ b/encoding/prototext/encode_test.go
@@ -38,11 +38,11 @@
}{{
desc: "proto2 optional scalars not set",
input: &pb2.Scalars{},
- want: "\n",
+ want: "",
}, {
desc: "proto3 scalars not set",
input: &pb3.Scalars{},
- want: "\n",
+ want: "",
}, {
desc: "proto2 optional scalars set to zero values",
input: &pb2.Scalars{
@@ -97,7 +97,7 @@
SBytes: []byte{},
SString: "",
},
- want: "\n",
+ want: "",
}, {
desc: "proto2 optional scalars set to some values",
input: &pb2.Scalars{
@@ -174,7 +174,7 @@
}, {
desc: "proto2 enum not set",
input: &pb2.Enums{},
- want: "\n",
+ want: "",
}, {
desc: "proto2 enum set to zero value",
input: &pb2.Enums{
@@ -214,14 +214,14 @@
}, {
desc: "proto3 enum not set",
input: &pb3.Enums{},
- want: "\n",
+ want: "",
}, {
desc: "proto3 enum set to zero value",
input: &pb3.Enums{
SEnum: pb3.Enum_ZERO,
SNestedEnum: pb3.Enums_CERO,
},
- want: "\n",
+ want: "",
}, {
desc: "proto3 enum",
input: &pb3.Enums{
@@ -252,7 +252,7 @@
}, {
desc: "proto2 nested message not set",
input: &pb2.Nests{},
- want: "\n",
+ want: "",
}, {
desc: "proto2 nested message set to empty",
input: &pb2.Nests{
@@ -305,7 +305,7 @@
}, {
desc: "proto3 nested message not set",
input: &pb3.Nests{},
- want: "\n",
+ want: "",
}, {
desc: "proto3 nested message set to empty",
input: &pb3.Nests{
@@ -340,7 +340,7 @@
}, {
desc: "oneof not set",
input: &pb3.Oneofs{},
- want: "\n",
+ want: "",
}, {
desc: "oneof set to empty string",
input: &pb3.Oneofs{
@@ -390,7 +390,7 @@
}, {
desc: "repeated fields not set",
input: &pb2.Repeats{},
- want: "\n",
+ want: "",
}, {
desc: "repeated fields set to empty slices",
input: &pb2.Repeats{
@@ -403,7 +403,7 @@
RptDouble: []float64{},
RptBytes: [][]byte{},
},
- want: "\n",
+ want: "",
}, {
desc: "repeated fields set to some values",
input: &pb2.Repeats{
@@ -472,7 +472,7 @@
RptNested: []*pb2.Nested{},
Rptgroup: []*pb2.Nests_RptGroup{},
},
- want: "\n",
+ want: "",
}, {
desc: "repeated messages",
input: &pb2.Nests{
@@ -529,7 +529,7 @@
}, {
desc: "map fields not set",
input: &pb3.Maps{},
- want: "\n",
+ want: "",
}, {
desc: "map fields set to empty",
input: &pb3.Maps{
@@ -539,7 +539,7 @@
StrToNested: map[string]*pb3.Nested{},
StrToOneofs: map[string]*pb3.Oneofs{},
},
- want: "\n",
+ want: "",
}, {
desc: "map fields 1",
input: &pb3.Maps{
@@ -681,7 +681,7 @@
}, {
desc: "required fields not set",
input: &pb2.Requireds{},
- want: "\n",
+ want: "",
wantErr: true,
}, {
desc: "required fields partially set",
@@ -751,7 +751,7 @@
input: &pb2.IndirectRequired{
RptNested: []*pb2.NestedWithRequired{},
},
- want: "\n",
+ want: "",
}, {
desc: "indirect required field in repeated",
input: &pb2.IndirectRequired{
@@ -775,7 +775,7 @@
input: &pb2.IndirectRequired{
StrToNested: map[string]*pb2.NestedWithRequired{},
},
- want: "\n",
+ want: "",
}, {
desc: "indirect required field in map",
input: &pb2.IndirectRequired{
diff --git a/internal/encoding/text/encode.go b/internal/encoding/text/encode.go
index 8f146ae..6757046 100644
--- a/internal/encoding/text/encode.go
+++ b/internal/encoding/text/encode.go
@@ -5,7 +5,6 @@
package text
import (
- "bytes"
"regexp"
"strings"
@@ -49,9 +48,6 @@
if err != nil {
return nil, err
}
- if len(indent) > 0 {
- return append(bytes.TrimRight(p.out, "\n"), '\n'), nil
- }
return p.out, nil
}
diff --git a/internal/encoding/text/text_test.go b/internal/encoding/text/text_test.go
index 64771f3..ba08868 100644
--- a/internal/encoding/text/text_test.go
+++ b/internal/encoding/text/text_test.go
@@ -42,7 +42,7 @@
}{{
in: "",
wantVal: V(Msg{}),
- wantOutIndent: "\n",
+ wantOutIndent: "",
}, {
in: S("%s# hello%s", space, space),
wantVal: V(Msg{}),