Skip to content

Commit 71220fc

Browse files
authored
Use string formatting for slice of bytes (#294)
If a slice of bytes is mostly text, format them as text instead of as []byte literal with hexadecimal digits. Avoid always printing the type. This is technically invalid Go code, but is unnecessary in many cases since the type is inferred from the parent concrete type. Fixes #272
1 parent 4664e24 commit 71220fc

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

cmp/cmpopts/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func ExampleIgnoreFields_testing() {
3939
// SSID: "CoffeeShopWiFi",
4040
// - IPAddress: s"192.168.0.2",
4141
// + IPAddress: s"192.168.0.1",
42-
// NetMask: {0xff, 0xff, 0x00, 0x00},
42+
// NetMask: s"ffff0000",
4343
// Clients: []cmpopts_test.Client{
4444
// ... // 3 identical elements
4545
// {Hostname: "espresso", ...},

cmp/compare_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,24 @@ using the AllowUnexported option.`, "\n"),
13851385
x: struct{ X interface{} }{int(0)},
13861386
y: struct{ X interface{} }{uint(0)},
13871387
reason: "mismatched underlying value within interface",
1388+
}, {
1389+
label: label + "/SliceOfBytesText",
1390+
x: [][]byte{
1391+
[]byte("hello"), []byte("foo"), []byte("barbaz"), []byte("blahdieblah"),
1392+
},
1393+
y: [][]byte{
1394+
[]byte("foo"), []byte("foo"), []byte("barbaz"), []byte("added"), []byte("here"), []byte("hrmph"),
1395+
},
1396+
reason: "should print text byte slices as strings",
1397+
}, {
1398+
label: label + "/SliceOfBytesBinary",
1399+
x: [][]byte{
1400+
[]byte("\xde\xad\xbe\xef"), []byte("\xffoo"), []byte("barbaz"), []byte("blahdieblah"),
1401+
},
1402+
y: [][]byte{
1403+
[]byte("\xffoo"), []byte("foo"), []byte("barbaz"), []byte("added"), []byte("here"), []byte("hrmph\xff"),
1404+
},
1405+
reason: "should print text byte slices as strings except those with binary",
13881406
}}
13891407
}
13901408

cmp/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func ExampleDiff_testing() {
3737
// SSID: "CoffeeShopWiFi",
3838
// - IPAddress: s"192.168.0.2",
3939
// + IPAddress: s"192.168.0.1",
40-
// NetMask: {0xff, 0xff, 0x00, 0x00},
40+
// NetMask: s"ffff0000",
4141
// Clients: []cmp_test.Client{
4242
// ... // 2 identical elements
4343
// {Hostname: "macchiato", IPAddress: s"192.168.0.153", LastSeen: s"2009-11-10 23:39:43 +0000 UTC"},

cmp/report_compare.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ func (opts formatOptions) FormatDiff(v *valueNode, ptrs *pointerReferences) (out
116116
}
117117

118118
// For leaf nodes, format the value based on the reflect.Values alone.
119-
if v.MaxDepth == 0 {
119+
// As a special case, treat equal []byte as a leaf nodes.
120+
isBytes := v.Type.Kind() == reflect.Slice && v.Type.Elem() == reflect.TypeOf(byte(0))
121+
isEqualBytes := isBytes && v.NumDiff+v.NumIgnored+v.NumTransformed == 0
122+
if v.MaxDepth == 0 || isEqualBytes {
120123
switch opts.DiffMode {
121124
case diffUnknown, diffIdentical:
122125
// Format Equal.

cmp/report_reflect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func (opts formatOptions) FormatValue(v reflect.Value, parentKind reflect.Kind,
211211
if len(b) > 0 && utf8.Valid(b) && len(bytes.TrimFunc(b, isPrintSpace)) == 0 {
212212
out = opts.formatString("", string(b))
213213
skipType = true
214-
return opts.WithTypeMode(emitType).FormatType(t, out)
214+
return opts.FormatType(t, out)
215215
}
216216
}
217217

cmp/testdata/diffs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@
241241
"string",
242242
}),
243243
Bytes: []uint8(Inverse(SplitBytes, [][]uint8{
244-
{0x73, 0x6f, 0x6d, 0x65},
245-
{0x6d, 0x75, 0x6c, 0x74, ...},
246-
{0x6c, 0x69, 0x6e, 0x65},
244+
"some",
245+
"multi",
246+
"line",
247247
{
248248
- 0x62,
249249
+ 0x42,
@@ -1158,6 +1158,30 @@
11581158
+ X: uint(0),
11591159
}
11601160
>>> TestDiff/Reporter/StructFieldAny
1161+
<<< TestDiff/Reporter/SliceOfBytesText
1162+
[][]uint8{
1163+
- "hello",
1164+
"foo",
1165+
+ "foo",
1166+
"barbaz",
1167+
+ "added",
1168+
+ "here",
1169+
- "blahdieblah",
1170+
+ "hrmph",
1171+
}
1172+
>>> TestDiff/Reporter/SliceOfBytesText
1173+
<<< TestDiff/Reporter/SliceOfBytesBinary
1174+
[][]uint8{
1175+
- {0xde, 0xad, 0xbe, 0xef},
1176+
{0xff, 0x6f, 0x6f},
1177+
+ "foo",
1178+
"barbaz",
1179+
+ "added",
1180+
+ "here",
1181+
- "blahdieblah",
1182+
+ {0x68, 0x72, 0x6d, 0x70, 0x68, 0xff},
1183+
}
1184+
>>> TestDiff/Reporter/SliceOfBytesBinary
11611185
<<< TestDiff/EmbeddedStruct/ParentStructA/Inequal
11621186
teststructs.ParentStructA{
11631187
privateStruct: teststructs.privateStruct{

0 commit comments

Comments
 (0)