name.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2020, 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 file.
  4. package value
  5. import (
  6. "reflect"
  7. "strconv"
  8. )
  9. var anyType = reflect.TypeOf((*interface{})(nil)).Elem()
  10. // TypeString is nearly identical to reflect.Type.String,
  11. // but has an additional option to specify that full type names be used.
  12. func TypeString(t reflect.Type, qualified bool) string {
  13. return string(appendTypeName(nil, t, qualified, false))
  14. }
  15. func appendTypeName(b []byte, t reflect.Type, qualified, elideFunc bool) []byte {
  16. // BUG: Go reflection provides no way to disambiguate two named types
  17. // of the same name and within the same package,
  18. // but declared within the namespace of different functions.
  19. // Use the "any" alias instead of "interface{}" for better readability.
  20. if t == anyType {
  21. return append(b, "any"...)
  22. }
  23. // Named type.
  24. if t.Name() != "" {
  25. if qualified && t.PkgPath() != "" {
  26. b = append(b, '"')
  27. b = append(b, t.PkgPath()...)
  28. b = append(b, '"')
  29. b = append(b, '.')
  30. b = append(b, t.Name()...)
  31. } else {
  32. b = append(b, t.String()...)
  33. }
  34. return b
  35. }
  36. // Unnamed type.
  37. switch k := t.Kind(); k {
  38. case reflect.Bool, reflect.String, reflect.UnsafePointer,
  39. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  40. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
  41. reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
  42. b = append(b, k.String()...)
  43. case reflect.Chan:
  44. if t.ChanDir() == reflect.RecvDir {
  45. b = append(b, "<-"...)
  46. }
  47. b = append(b, "chan"...)
  48. if t.ChanDir() == reflect.SendDir {
  49. b = append(b, "<-"...)
  50. }
  51. b = append(b, ' ')
  52. b = appendTypeName(b, t.Elem(), qualified, false)
  53. case reflect.Func:
  54. if !elideFunc {
  55. b = append(b, "func"...)
  56. }
  57. b = append(b, '(')
  58. for i := 0; i < t.NumIn(); i++ {
  59. if i > 0 {
  60. b = append(b, ", "...)
  61. }
  62. if i == t.NumIn()-1 && t.IsVariadic() {
  63. b = append(b, "..."...)
  64. b = appendTypeName(b, t.In(i).Elem(), qualified, false)
  65. } else {
  66. b = appendTypeName(b, t.In(i), qualified, false)
  67. }
  68. }
  69. b = append(b, ')')
  70. switch t.NumOut() {
  71. case 0:
  72. // Do nothing
  73. case 1:
  74. b = append(b, ' ')
  75. b = appendTypeName(b, t.Out(0), qualified, false)
  76. default:
  77. b = append(b, " ("...)
  78. for i := 0; i < t.NumOut(); i++ {
  79. if i > 0 {
  80. b = append(b, ", "...)
  81. }
  82. b = appendTypeName(b, t.Out(i), qualified, false)
  83. }
  84. b = append(b, ')')
  85. }
  86. case reflect.Struct:
  87. b = append(b, "struct{ "...)
  88. for i := 0; i < t.NumField(); i++ {
  89. if i > 0 {
  90. b = append(b, "; "...)
  91. }
  92. sf := t.Field(i)
  93. if !sf.Anonymous {
  94. if qualified && sf.PkgPath != "" {
  95. b = append(b, '"')
  96. b = append(b, sf.PkgPath...)
  97. b = append(b, '"')
  98. b = append(b, '.')
  99. }
  100. b = append(b, sf.Name...)
  101. b = append(b, ' ')
  102. }
  103. b = appendTypeName(b, sf.Type, qualified, false)
  104. if sf.Tag != "" {
  105. b = append(b, ' ')
  106. b = strconv.AppendQuote(b, string(sf.Tag))
  107. }
  108. }
  109. if b[len(b)-1] == ' ' {
  110. b = b[:len(b)-1]
  111. } else {
  112. b = append(b, ' ')
  113. }
  114. b = append(b, '}')
  115. case reflect.Slice, reflect.Array:
  116. b = append(b, '[')
  117. if k == reflect.Array {
  118. b = strconv.AppendUint(b, uint64(t.Len()), 10)
  119. }
  120. b = append(b, ']')
  121. b = appendTypeName(b, t.Elem(), qualified, false)
  122. case reflect.Map:
  123. b = append(b, "map["...)
  124. b = appendTypeName(b, t.Key(), qualified, false)
  125. b = append(b, ']')
  126. b = appendTypeName(b, t.Elem(), qualified, false)
  127. case reflect.Ptr:
  128. b = append(b, '*')
  129. b = appendTypeName(b, t.Elem(), qualified, false)
  130. case reflect.Interface:
  131. b = append(b, "interface{ "...)
  132. for i := 0; i < t.NumMethod(); i++ {
  133. if i > 0 {
  134. b = append(b, "; "...)
  135. }
  136. m := t.Method(i)
  137. if qualified && m.PkgPath != "" {
  138. b = append(b, '"')
  139. b = append(b, m.PkgPath...)
  140. b = append(b, '"')
  141. b = append(b, '.')
  142. }
  143. b = append(b, m.Name...)
  144. b = appendTypeName(b, m.Type, qualified, true)
  145. }
  146. if b[len(b)-1] == ' ' {
  147. b = b[:len(b)-1]
  148. } else {
  149. b = append(b, ' ')
  150. }
  151. b = append(b, '}')
  152. default:
  153. panic("invalid kind: " + k.String())
  154. }
  155. return b
  156. }