aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/google/go-cmp/cmp/internal/value/name.go
blob: b6c12cefb47e67ec457a1b4beabd4a26d0d929dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2020, The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package value

import (
	"reflect"
	"strconv"
)

// TypeString is nearly identical to reflect.Type.String,
// but has an additional option to specify that full type names be used.
func TypeString(t reflect.Type, qualified bool) string {
	return string(appendTypeName(nil, t, qualified, false))
}

func appendTypeName(b []byte, t reflect.Type, qualified, elideFunc bool) []byte {
	// BUG: Go reflection provides no way to disambiguate two named types
	// of the same name and within the same package,
	// but declared within the namespace of different functions.

	// Named type.
	if t.Name() != "" {
		if qualified && t.PkgPath() != "" {
			b = append(b, '"')
			b = append(b, t.PkgPath()...)
			b = append(b, '"')
			b = append(b, '.')
			b = append(b, t.Name()...)
		} else {
			b = append(b, t.String()...)
		}
		return b
	}

	// Unnamed type.
	switch k := t.Kind(); k {
	case reflect.Bool, reflect.String, reflect.UnsafePointer,
		reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
		reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
		reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
		b = append(b, k.String()...)
	case reflect.Chan:
		if t.ChanDir() == reflect.RecvDir {
			b = append(b, "<-"...)
		}
		b = append(b, "chan"...)
		if t.ChanDir() == reflect.SendDir {
			b = append(b, "<-"...)
		}
		b = append(b, ' ')
		b = appendTypeName(b, t.Elem(), qualified, false)
	case reflect.Func:
		if !elideFunc {
			b = append(b, "func"...)
		}
		b = append(b, '(')
		for i := 0; i < t.NumIn(); i++ {
			if i > 0 {
				b = append(b, ", "...)
			}
			if i == t.NumIn()-1 && t.IsVariadic() {
				b = append(b, "..."...)
				b = appendTypeName(b, t.In(i).Elem(), qualified, false)
			} else {
				b = appendTypeName(b, t.In(i), qualified, false)
			}
		}
		b = append(b, ')')
		switch t.NumOut() {
		case 0:
			// Do nothing
		case 1:
			b = append(b, ' ')
			b = appendTypeName(b, t.Out(0), qualified, false)
		default:
			b = append(b, " ("...)
			for i := 0; i < t.NumOut(); i++ {
				if i > 0 {
					b = append(b, ", "...)
				}
				b = appendTypeName(b, t.Out(i), qualified, false)
			}
			b = append(b, ')')
		}
	case reflect.Struct:
		b = append(b, "struct{ "...)
		for i := 0; i < t.NumField(); i++ {
			if i > 0 {
				b = append(b, "; "...)
			}
			sf := t.Field(i)
			if !sf.Anonymous {
				if qualified && sf.PkgPath != "" {
					b = append(b, '"')
					b = append(b, sf.PkgPath...)
					b = append(b, '"')
					b = append(b, '.')
				}
				b = append(b, sf.Name...)
				b = append(b, ' ')
			}
			b = appendTypeName(b, sf.Type, qualified, false)
			if sf.Tag != "" {
				b = append(b, ' ')
				b = strconv.AppendQuote(b, string(sf.Tag))
			}
		}
		if b[len(b)-1] == ' ' {
			b = b[:len(b)-1]
		} else {
			b = append(b, ' ')
		}
		b = append(b, '}')
	case reflect.Slice, reflect.Array:
		b = append(b, '[')
		if k == reflect.Array {
			b = strconv.AppendUint(b, uint64(t.Len()), 10)
		}
		b = append(b, ']')
		b = appendTypeName(b, t.Elem(), qualified, false)
	case reflect.Map:
		b = append(b, "map["...)
		b = appendTypeName(b, t.Key(), qualified, false)
		b = append(b, ']')
		b = appendTypeName(b, t.Elem(), qualified, false)
	case reflect.Ptr:
		b = append(b, '*')
		b = appendTypeName(b, t.Elem(), qualified, false)
	case reflect.Interface:
		b = append(b, "interface{ "...)
		for i := 0; i < t.NumMethod(); i++ {
			if i > 0 {
				b = append(b, "; "...)
			}
			m := t.Method(i)
			if qualified && m.PkgPath != "" {
				b = append(b, '"')
				b = append(b, m.PkgPath...)
				b = append(b, '"')
				b = append(b, '.')
			}
			b = append(b, m.Name...)
			b = appendTypeName(b, m.Type, qualified, true)
		}
		if b[len(b)-1] == ' ' {
			b = b[:len(b)-1]
		} else {
			b = append(b, ' ')
		}
		b = append(b, '}')
	default:
		panic("invalid kind: " + k.String())
	}
	return b
}