1 // Copyright 2019, The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE.md file.
16 "github.com/google/go-cmp/cmp/internal/value"
19 type formatValueOptions struct {
20 // AvoidStringer controls whether to avoid calling custom stringer
21 // methods like error.Error or fmt.Stringer.String.
24 // PrintAddresses controls whether to print the address of all pointers,
25 // slice elements, and maps.
28 // QualifiedNames controls whether FormatType uses the fully qualified name
29 // (including the full package path as opposed to just the package name).
32 // VerbosityLevel controls the amount of output to produce.
33 // A higher value produces more output. A value of zero or lower produces
34 // no output (represented using an ellipsis).
35 // If LimitVerbosity is false, then the level is treated as infinite.
38 // LimitVerbosity specifies that formatting should respect VerbosityLevel.
42 // FormatType prints the type as if it were wrapping s.
43 // This may return s as-is depending on the current type and TypeMode mode.
44 func (opts formatOptions) FormatType(t reflect.Type, s textNode) textNode {
45 // Check whether to emit the type or not.
46 switch opts.TypeMode {
49 case reflect.Struct, reflect.Slice, reflect.Array, reflect.Map:
56 if opts.DiffMode == diffIdentical {
57 return s // elide type for identical nodes
63 // Determine the type label, applying special handling for unnamed types.
64 typeName := value.TypeString(t, opts.QualifiedNames)
66 // According to Go grammar, certain type literals contain symbols that
67 // do not strongly bind to the next lexicographical token (e.g., *T).
69 case reflect.Chan, reflect.Func, reflect.Ptr:
70 typeName = "(" + typeName + ")"
73 return &textWrap{Prefix: typeName, Value: wrapParens(s)}
76 // wrapParens wraps s with a set of parenthesis, but avoids it if the
77 // wrapped node itself is already surrounded by a pair of parenthesis or braces.
78 // It handles unwrapping one level of pointer-reference nodes.
79 func wrapParens(s textNode) textNode {
81 if s2, ok := s.(*textWrap); ok {
82 // Unwrap a single pointer reference node.
83 switch s2.Metadata.(type) {
84 case leafReference, trunkReference, trunkReferences:
86 if s3, ok := refNode.Value.(*textWrap); ok {
91 // Already has delimiters that make parenthesis unnecessary.
92 hasParens := strings.HasPrefix(s2.Prefix, "(") && strings.HasSuffix(s2.Suffix, ")")
93 hasBraces := strings.HasPrefix(s2.Prefix, "{") && strings.HasSuffix(s2.Suffix, "}")
94 if hasParens || hasBraces {
99 refNode.Value = &textWrap{Prefix: "(", Value: refNode.Value, Suffix: ")"}
102 return &textWrap{Prefix: "(", Value: s, Suffix: ")"}
105 // FormatValue prints the reflect.Value, taking extra care to avoid descending
106 // into pointers already in ptrs. As pointers are visited, ptrs is also updated.
107 func (opts formatOptions) FormatValue(v reflect.Value, parentKind reflect.Kind, ptrs *pointerReferences) (out textNode) {
113 // Check slice element for cycles.
114 if parentKind == reflect.Slice {
115 ptrRef, visited := ptrs.Push(v.Addr())
117 return makeLeafReference(ptrRef, false)
120 defer func() { out = wrapTrunkReference(ptrRef, false, out) }()
123 // Check whether there is an Error or String method to call.
124 if !opts.AvoidStringer && v.CanInterface() {
125 // Avoid calling Error or String methods on nil receivers since many
126 // implementations crash when doing so.
127 if (t.Kind() != reflect.Ptr && t.Kind() != reflect.Interface) || !v.IsNil() {
128 var prefix, strVal string
130 // Swallow and ignore any panics from String or Error.
131 defer func() { recover() }()
132 switch v := v.Interface().(type) {
142 return opts.formatString(prefix, strVal)
147 // Check whether to explicitly wrap the result with the type.
151 out = opts.FormatType(t, out)
157 return textLine(fmt.Sprint(v.Bool()))
158 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
159 return textLine(fmt.Sprint(v.Int()))
160 case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
161 return textLine(fmt.Sprint(v.Uint()))
163 if parentKind == reflect.Slice || parentKind == reflect.Array {
164 return textLine(formatHex(v.Uint()))
166 return textLine(fmt.Sprint(v.Uint()))
167 case reflect.Uintptr:
168 return textLine(formatHex(v.Uint()))
169 case reflect.Float32, reflect.Float64:
170 return textLine(fmt.Sprint(v.Float()))
171 case reflect.Complex64, reflect.Complex128:
172 return textLine(fmt.Sprint(v.Complex()))
174 return opts.formatString("", v.String())
175 case reflect.UnsafePointer, reflect.Chan, reflect.Func:
176 return textLine(formatPointer(value.PointerOf(v), true))
179 v := makeAddressable(v) // needed for retrieveUnexportedField
180 maxLen := v.NumField()
181 if opts.LimitVerbosity {
182 maxLen = ((1 << opts.verbosity()) >> 1) << 2 // 0, 4, 8, 16, 32, etc...
183 opts.VerbosityLevel--
185 for i := 0; i < v.NumField(); i++ {
187 if value.IsZero(vv) {
188 continue // Elide fields with zero values
190 if len(list) == maxLen {
191 list.AppendEllipsis(diffStats{})
195 if supportExporters && !isExported(sf.Name) {
196 vv = retrieveUnexportedField(v, sf, true)
198 s := opts.WithTypeMode(autoType).FormatValue(vv, t.Kind(), ptrs)
199 list = append(list, textRecord{Key: sf.Name, Value: s})
201 return &textWrap{Prefix: "{", Value: list, Suffix: "}"}
207 // Check whether this is a []byte of text data.
208 if t.Elem() == reflect.TypeOf(byte(0)) {
210 isPrintSpace := func(r rune) bool { return unicode.IsPrint(r) && unicode.IsSpace(r) }
211 if len(b) > 0 && utf8.Valid(b) && len(bytes.TrimFunc(b, isPrintSpace)) == 0 {
212 out = opts.formatString("", string(b))
213 return opts.WithTypeMode(emitType).FormatType(t, out)
220 if opts.LimitVerbosity {
221 maxLen = ((1 << opts.verbosity()) >> 1) << 2 // 0, 4, 8, 16, 32, etc...
222 opts.VerbosityLevel--
225 for i := 0; i < v.Len(); i++ {
226 if len(list) == maxLen {
227 list.AppendEllipsis(diffStats{})
230 s := opts.WithTypeMode(elideType).FormatValue(v.Index(i), t.Kind(), ptrs)
231 list = append(list, textRecord{Value: s})
234 out = &textWrap{Prefix: "{", Value: list, Suffix: "}"}
235 if t.Kind() == reflect.Slice && opts.PrintAddresses {
236 header := fmt.Sprintf("ptr:%v, len:%d, cap:%d", formatPointer(value.PointerOf(v), false), v.Len(), v.Cap())
237 out = &textWrap{Prefix: pointerDelimPrefix + header + pointerDelimSuffix, Value: out}
245 // Check pointer for cycles.
246 ptrRef, visited := ptrs.Push(v)
248 return makeLeafReference(ptrRef, opts.PrintAddresses)
253 if opts.LimitVerbosity {
254 maxLen = ((1 << opts.verbosity()) >> 1) << 2 // 0, 4, 8, 16, 32, etc...
255 opts.VerbosityLevel--
258 for _, k := range value.SortKeys(v.MapKeys()) {
259 if len(list) == maxLen {
260 list.AppendEllipsis(diffStats{})
263 sk := formatMapKey(k, false, ptrs)
264 sv := opts.WithTypeMode(elideType).FormatValue(v.MapIndex(k), t.Kind(), ptrs)
265 list = append(list, textRecord{Key: sk, Value: sv})
268 out = &textWrap{Prefix: "{", Value: list, Suffix: "}"}
269 out = wrapTrunkReference(ptrRef, opts.PrintAddresses, out)
276 // Check pointer for cycles.
277 ptrRef, visited := ptrs.Push(v)
279 out = makeLeafReference(ptrRef, opts.PrintAddresses)
280 return &textWrap{Prefix: "&", Value: out}
284 skipType = true // Let the underlying value print the type instead
285 out = opts.FormatValue(v.Elem(), t.Kind(), ptrs)
286 out = wrapTrunkReference(ptrRef, opts.PrintAddresses, out)
287 out = &textWrap{Prefix: "&", Value: out}
289 case reflect.Interface:
293 // Interfaces accept different concrete types,
294 // so configure the underlying value to explicitly print the type.
295 skipType = true // Print the concrete type instead
296 return opts.WithTypeMode(emitType).FormatValue(v.Elem(), t.Kind(), ptrs)
298 panic(fmt.Sprintf("%v kind not handled", v.Kind()))
302 func (opts formatOptions) formatString(prefix, s string) textNode {
304 maxLines := strings.Count(s, "\n") + 1
305 if opts.LimitVerbosity {
306 maxLen = (1 << opts.verbosity()) << 5 // 32, 64, 128, 256, etc...
307 maxLines = (1 << opts.verbosity()) << 2 // 4, 8, 16, 32, 64, etc...
310 // For multiline strings, use the triple-quote syntax,
311 // but only use it when printing removed or inserted nodes since
312 // we only want the extra verbosity for those cases.
313 lines := strings.Split(strings.TrimSuffix(s, "\n"), "\n")
314 isTripleQuoted := len(lines) >= 4 && (opts.DiffMode == '-' || opts.DiffMode == '+')
315 for i := 0; i < len(lines) && isTripleQuoted; i++ {
316 lines[i] = strings.TrimPrefix(strings.TrimSuffix(lines[i], "\r"), "\r") // trim leading/trailing carriage returns for legacy Windows endline support
317 isPrintable := func(r rune) bool {
318 return unicode.IsPrint(r) || r == '\t' // specially treat tab as printable
321 isTripleQuoted = !strings.HasPrefix(strings.TrimPrefix(line, prefix), `"""`) && !strings.HasPrefix(line, "...") && strings.TrimFunc(line, isPrintable) == "" && len(line) <= maxLen
325 list = append(list, textRecord{Diff: opts.DiffMode, Value: textLine(prefix + `"""`), ElideComma: true})
326 for i, line := range lines {
327 if numElided := len(lines) - i; i == maxLines-1 && numElided > 1 {
328 comment := commentString(fmt.Sprintf("%d elided lines", numElided))
329 list = append(list, textRecord{Diff: opts.DiffMode, Value: textEllipsis, ElideComma: true, Comment: comment})
332 list = append(list, textRecord{Diff: opts.DiffMode, Value: textLine(line), ElideComma: true})
334 list = append(list, textRecord{Diff: opts.DiffMode, Value: textLine(prefix + `"""`), ElideComma: true})
335 return &textWrap{Prefix: "(", Value: list, Suffix: ")"}
338 // Format the string as a single-line quoted string.
339 if len(s) > maxLen+len(textEllipsis) {
340 return textLine(prefix + formatString(s[:maxLen]) + string(textEllipsis))
342 return textLine(prefix + formatString(s))
345 // formatMapKey formats v as if it were a map key.
346 // The result is guaranteed to be a single line.
347 func formatMapKey(v reflect.Value, disambiguate bool, ptrs *pointerReferences) string {
348 var opts formatOptions
349 opts.DiffMode = diffIdentical
350 opts.TypeMode = elideType
351 opts.PrintAddresses = disambiguate
352 opts.AvoidStringer = disambiguate
353 opts.QualifiedNames = disambiguate
354 s := opts.FormatValue(v, reflect.Map, ptrs).String()
355 return strings.TrimSpace(s)
358 // formatString prints s as a double-quoted or backtick-quoted string.
359 func formatString(s string) string {
360 // Use quoted string if it the same length as a raw string literal.
361 // Otherwise, attempt to use the raw string form.
362 qs := strconv.Quote(s)
363 if len(qs) == 1+len(s)+1 {
367 // Disallow newlines to ensure output is a single line.
368 // Only allow printable runes for readability purposes.
369 rawInvalid := func(r rune) bool {
370 return r == '`' || r == '\n' || !(unicode.IsPrint(r) || r == '\t')
372 if utf8.ValidString(s) && strings.IndexFunc(s, rawInvalid) < 0 {
378 // formatHex prints u as a hexadecimal integer in Go notation.
379 func formatHex(u uint64) string {
388 case u <= 0xffffffff:
390 case u <= 0xffffffffff:
392 case u <= 0xffffffffffff:
394 case u <= 0xffffffffffffff:
396 case u <= 0xffffffffffffffff:
399 return fmt.Sprintf(f, u)