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>
This commit is contained in:
Joe Tsai 2019-09-11 15:31:45 -07:00
parent 2df47c918b
commit 3c4ab8c6f1
3 changed files with 18 additions and 22 deletions

View File

@ -38,11 +38,11 @@ func TestMarshal(t *testing.T) {
}{{ }{{
desc: "proto2 optional scalars not set", desc: "proto2 optional scalars not set",
input: &pb2.Scalars{}, input: &pb2.Scalars{},
want: "\n", want: "",
}, { }, {
desc: "proto3 scalars not set", desc: "proto3 scalars not set",
input: &pb3.Scalars{}, input: &pb3.Scalars{},
want: "\n", want: "",
}, { }, {
desc: "proto2 optional scalars set to zero values", desc: "proto2 optional scalars set to zero values",
input: &pb2.Scalars{ input: &pb2.Scalars{
@ -97,7 +97,7 @@ opt_string: ""
SBytes: []byte{}, SBytes: []byte{},
SString: "", SString: "",
}, },
want: "\n", want: "",
}, { }, {
desc: "proto2 optional scalars set to some values", desc: "proto2 optional scalars set to some values",
input: &pb2.Scalars{ input: &pb2.Scalars{
@ -174,7 +174,7 @@ opt_string: "谷歌"
}, { }, {
desc: "proto2 enum not set", desc: "proto2 enum not set",
input: &pb2.Enums{}, input: &pb2.Enums{},
want: "\n", want: "",
}, { }, {
desc: "proto2 enum set to zero value", desc: "proto2 enum set to zero value",
input: &pb2.Enums{ input: &pb2.Enums{
@ -214,14 +214,14 @@ opt_nested_enum: -101
}, { }, {
desc: "proto3 enum not set", desc: "proto3 enum not set",
input: &pb3.Enums{}, input: &pb3.Enums{},
want: "\n", want: "",
}, { }, {
desc: "proto3 enum set to zero value", desc: "proto3 enum set to zero value",
input: &pb3.Enums{ input: &pb3.Enums{
SEnum: pb3.Enum_ZERO, SEnum: pb3.Enum_ZERO,
SNestedEnum: pb3.Enums_CERO, SNestedEnum: pb3.Enums_CERO,
}, },
want: "\n", want: "",
}, { }, {
desc: "proto3 enum", desc: "proto3 enum",
input: &pb3.Enums{ input: &pb3.Enums{
@ -252,7 +252,7 @@ s_nested_enum: 47
}, { }, {
desc: "proto2 nested message not set", desc: "proto2 nested message not set",
input: &pb2.Nests{}, input: &pb2.Nests{},
want: "\n", want: "",
}, { }, {
desc: "proto2 nested message set to empty", desc: "proto2 nested message set to empty",
input: &pb2.Nests{ input: &pb2.Nests{
@ -305,7 +305,7 @@ OptGroup: {}
}, { }, {
desc: "proto3 nested message not set", desc: "proto3 nested message not set",
input: &pb3.Nests{}, input: &pb3.Nests{},
want: "\n", want: "",
}, { }, {
desc: "proto3 nested message set to empty", desc: "proto3 nested message set to empty",
input: &pb3.Nests{ input: &pb3.Nests{
@ -340,7 +340,7 @@ OptGroup: {}
}, { }, {
desc: "oneof not set", desc: "oneof not set",
input: &pb3.Oneofs{}, input: &pb3.Oneofs{},
want: "\n", want: "",
}, { }, {
desc: "oneof set to empty string", desc: "oneof set to empty string",
input: &pb3.Oneofs{ input: &pb3.Oneofs{
@ -390,7 +390,7 @@ OptGroup: {}
}, { }, {
desc: "repeated fields not set", desc: "repeated fields not set",
input: &pb2.Repeats{}, input: &pb2.Repeats{},
want: "\n", want: "",
}, { }, {
desc: "repeated fields set to empty slices", desc: "repeated fields set to empty slices",
input: &pb2.Repeats{ input: &pb2.Repeats{
@ -403,7 +403,7 @@ OptGroup: {}
RptDouble: []float64{}, RptDouble: []float64{},
RptBytes: [][]byte{}, RptBytes: [][]byte{},
}, },
want: "\n", want: "",
}, { }, {
desc: "repeated fields set to some values", desc: "repeated fields set to some values",
input: &pb2.Repeats{ input: &pb2.Repeats{
@ -472,7 +472,7 @@ rpt_nested_enum: DIEZ
RptNested: []*pb2.Nested{}, RptNested: []*pb2.Nested{},
Rptgroup: []*pb2.Nests_RptGroup{}, Rptgroup: []*pb2.Nests_RptGroup{},
}, },
want: "\n", want: "",
}, { }, {
desc: "repeated messages", desc: "repeated messages",
input: &pb2.Nests{ input: &pb2.Nests{
@ -529,7 +529,7 @@ RptGroup: {}
}, { }, {
desc: "map fields not set", desc: "map fields not set",
input: &pb3.Maps{}, input: &pb3.Maps{},
want: "\n", want: "",
}, { }, {
desc: "map fields set to empty", desc: "map fields set to empty",
input: &pb3.Maps{ input: &pb3.Maps{
@ -539,7 +539,7 @@ RptGroup: {}
StrToNested: map[string]*pb3.Nested{}, StrToNested: map[string]*pb3.Nested{},
StrToOneofs: map[string]*pb3.Oneofs{}, StrToOneofs: map[string]*pb3.Oneofs{},
}, },
want: "\n", want: "",
}, { }, {
desc: "map fields 1", desc: "map fields 1",
input: &pb3.Maps{ input: &pb3.Maps{
@ -681,7 +681,7 @@ str_to_oneofs: {
}, { }, {
desc: "required fields not set", desc: "required fields not set",
input: &pb2.Requireds{}, input: &pb2.Requireds{},
want: "\n", want: "",
wantErr: true, wantErr: true,
}, { }, {
desc: "required fields partially set", desc: "required fields partially set",
@ -751,7 +751,7 @@ req_nested: {}
input: &pb2.IndirectRequired{ input: &pb2.IndirectRequired{
RptNested: []*pb2.NestedWithRequired{}, RptNested: []*pb2.NestedWithRequired{},
}, },
want: "\n", want: "",
}, { }, {
desc: "indirect required field in repeated", desc: "indirect required field in repeated",
input: &pb2.IndirectRequired{ input: &pb2.IndirectRequired{
@ -775,7 +775,7 @@ req_nested: {}
input: &pb2.IndirectRequired{ input: &pb2.IndirectRequired{
StrToNested: map[string]*pb2.NestedWithRequired{}, StrToNested: map[string]*pb2.NestedWithRequired{},
}, },
want: "\n", want: "",
}, { }, {
desc: "indirect required field in map", desc: "indirect required field in map",
input: &pb2.IndirectRequired{ input: &pb2.IndirectRequired{

View File

@ -5,7 +5,6 @@
package text package text
import ( import (
"bytes"
"regexp" "regexp"
"strings" "strings"
@ -49,9 +48,6 @@ func Marshal(v Value, indent string, delims [2]byte, outputASCII bool) ([]byte,
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(indent) > 0 {
return append(bytes.TrimRight(p.out, "\n"), '\n'), nil
}
return p.out, nil return p.out, nil
} }

View File

@ -42,7 +42,7 @@ func Test(t *testing.T) {
}{{ }{{
in: "", in: "",
wantVal: V(Msg{}), wantVal: V(Msg{}),
wantOutIndent: "\n", wantOutIndent: "",
}, { }, {
in: S("%s# hello%s", space, space), in: S("%s# hello%s", space, space),
wantVal: V(Msg{}), wantVal: V(Msg{}),