File tree Expand file tree Collapse file tree 4 files changed +23
-1
lines changed Expand file tree Collapse file tree 4 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,8 @@ import (
20
20
)
21
21
22
22
// Marshal writes the given proto.Message in JSON format using default options.
23
+ // Do not depend on the output of being stable. It may change over time across
24
+ // different versions of the program.
23
25
func Marshal (m proto.Message ) ([]byte , error ) {
24
26
return MarshalOptions {}.Marshal (m )
25
27
}
@@ -71,7 +73,8 @@ type MarshalOptions struct {
71
73
}
72
74
73
75
// Marshal marshals the given proto.Message in the JSON format using options in
74
- // MarshalOptions.
76
+ // MarshalOptions. Do not depend on the output being stable. It may change over
77
+ // time across different versions of the program.
75
78
func (o MarshalOptions ) Marshal (m proto.Message ) ([]byte , error ) {
76
79
var err error
77
80
o .encoder , err = json .NewEncoder (o .Indent )
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import (
11
11
12
12
"github.com/google/go-cmp/cmp"
13
13
"google.golang.org/protobuf/encoding/protojson"
14
+ "google.golang.org/protobuf/internal/detrand"
14
15
"google.golang.org/protobuf/internal/encoding/pack"
15
16
"google.golang.org/protobuf/internal/flags"
16
17
pimpl "google.golang.org/protobuf/internal/impl"
@@ -28,6 +29,9 @@ import (
28
29
"google.golang.org/protobuf/types/known/wrapperspb"
29
30
)
30
31
32
+ // Disable detrand to enable direct comparisons on outputs.
33
+ func init () { detrand .Disable () }
34
+
31
35
func TestMarshal (t * testing.T ) {
32
36
tests := []struct {
33
37
desc string
Original file line number Diff line number Diff line change 8
8
"strconv"
9
9
"strings"
10
10
11
+ "google.golang.org/protobuf/internal/detrand"
11
12
"google.golang.org/protobuf/internal/errors"
12
13
)
13
14
@@ -132,6 +133,11 @@ func (e *Encoder) prepareNext(next Type) {
132
133
if e .lastType & (Null | Bool | Number | String | EndObject | EndArray ) != 0 &&
133
134
next & (Name | Null | Bool | Number | String | StartObject | StartArray ) != 0 {
134
135
e .out = append (e .out , ',' )
136
+ // For single-line output, add a random extra space after each
137
+ // comma to make output unstable.
138
+ if detrand .Bool () {
139
+ e .out = append (e .out , ' ' )
140
+ }
135
141
}
136
142
return
137
143
}
@@ -160,5 +166,10 @@ func (e *Encoder) prepareNext(next Type) {
160
166
161
167
case e .lastType & Name != 0 :
162
168
e .out = append (e .out , ' ' )
169
+ // For multi-line output, add a random extra space after key: to make
170
+ // output unstable.
171
+ if detrand .Bool () {
172
+ e .out = append (e .out , ' ' )
173
+ }
163
174
}
164
175
}
Original file line number Diff line number Diff line change @@ -11,9 +11,13 @@ import (
11
11
12
12
"github.com/google/go-cmp/cmp"
13
13
"github.com/google/go-cmp/cmp/cmpopts"
14
+ "google.golang.org/protobuf/internal/detrand"
14
15
"google.golang.org/protobuf/internal/encoding/json"
15
16
)
16
17
18
+ // Disable detrand to enable direct comparisons on outputs.
19
+ func init () { detrand .Disable () }
20
+
17
21
// splitLines is a cmpopts.Option for comparing strings with line breaks.
18
22
var splitLines = cmpopts .AcyclicTransformer ("SplitLines" , func (s string ) []string {
19
23
return strings .Split (s , "\n " )
You can’t perform that action at this time.
0 commit comments