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