| 18 | } |
| 19 | |
| 20 | func appendTypeName(b []byte, t reflect.Type, qualified, elideFunc bool) []byte { |
| 21 | // BUG: Go reflection provides no way to disambiguate two named types |
| 22 | // of the same name and within the same package, |
| 23 | // but declared within the namespace of different functions. |
| 24 | |
| 25 | // Use the "any" alias instead of "interface{}" for better readability. |
| 26 | if t == anyType { |
| 27 | return append(b, "any"...) |
| 28 | } |
| 29 | |
| 30 | // Named type. |
| 31 | if t.Name() != "" { |
| 32 | if qualified && t.PkgPath() != "" { |
| 33 | b = append(b, '"') |
| 34 | b = append(b, t.PkgPath()...) |
| 35 | b = append(b, '"') |
| 36 | b = append(b, '.') |
| 37 | b = append(b, t.Name()...) |
| 38 | } else { |
| 39 | b = append(b, t.String()...) |
| 40 | } |
| 41 | return b |
| 42 | } |
| 43 | |
| 44 | // Unnamed type. |
| 45 | switch k := t.Kind(); k { |
| 46 | case reflect.Bool, reflect.String, reflect.UnsafePointer, |
| 47 | reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, |
| 48 | reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, |
| 49 | reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128: |
| 50 | b = append(b, k.String()...) |
| 51 | case reflect.Chan: |
| 52 | if t.ChanDir() == reflect.RecvDir { |
| 53 | b = append(b, "<-"...) |
| 54 | } |
| 55 | b = append(b, "chan"...) |
| 56 | if t.ChanDir() == reflect.SendDir { |
| 57 | b = append(b, "<-"...) |
| 58 | } |
| 59 | b = append(b, ' ') |
| 60 | b = appendTypeName(b, t.Elem(), qualified, false) |
| 61 | case reflect.Func: |
| 62 | if !elideFunc { |
| 63 | b = append(b, "func"...) |
| 64 | } |
| 65 | b = append(b, '(') |
| 66 | for i := 0; i < t.NumIn(); i++ { |
| 67 | if i > 0 { |
| 68 | b = append(b, ", "...) |
| 69 | } |
| 70 | if i == t.NumIn()-1 && t.IsVariadic() { |
| 71 | b = append(b, "..."...) |
| 72 | b = appendTypeName(b, t.In(i).Elem(), qualified, false) |
| 73 | } else { |
| 74 | b = appendTypeName(b, t.In(i), qualified, false) |
| 75 | } |
| 76 | } |
| 77 | b = append(b, ')') |