]> Cypherpunks repositories - gostls13.git/commitdiff
internal/reflectlite: include Kind in ValueError message
authorOri Rawlings <orirawlings@gmail.com>
Fri, 29 May 2020 03:41:38 +0000 (22:41 -0500)
committerIan Lance Taylor <iant@golang.org>
Wed, 7 Oct 2020 00:15:09 +0000 (00:15 +0000)
The implementation has been ported from reflect, but to avoid
introducing a dependency on strconv, Kind.String() falls back to
"invalid" if the Kind is unknown rather than "kind" + strconv.Itoa(int(k))

Fixes #39286

Change-Id: I82277242a6c41d0146dabd9d20339fe72d562500
Reviewed-on: https://go-review.googlesource.com/c/go/+/235522
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Trust: Dmitri Shuralyov <dmitshur@golang.org>

src/internal/reflectlite/type.go
src/internal/reflectlite/value.go

index eb7f1a4b78e7193903a34ef3ac08c9c3a1232799..15ba30da36cf6b7449dfd4616e5e5cbd7a334e16 100644 (file)
@@ -384,6 +384,44 @@ const (
        kindMask        = (1 << 5) - 1
 )
 
+// String returns the name of k.
+func (k Kind) String() string {
+       if int(k) < len(kindNames) {
+               return kindNames[k]
+       }
+       return kindNames[0]
+}
+
+var kindNames = []string{
+       Invalid:       "invalid",
+       Bool:          "bool",
+       Int:           "int",
+       Int8:          "int8",
+       Int16:         "int16",
+       Int32:         "int32",
+       Int64:         "int64",
+       Uint:          "uint",
+       Uint8:         "uint8",
+       Uint16:        "uint16",
+       Uint32:        "uint32",
+       Uint64:        "uint64",
+       Uintptr:       "uintptr",
+       Float32:       "float32",
+       Float64:       "float64",
+       Complex64:     "complex64",
+       Complex128:    "complex128",
+       Array:         "array",
+       Chan:          "chan",
+       Func:          "func",
+       Interface:     "interface",
+       Map:           "map",
+       Ptr:           "ptr",
+       Slice:         "slice",
+       String:        "string",
+       Struct:        "struct",
+       UnsafePointer: "unsafe.Pointer",
+}
+
 func (t *uncommonType) methods() []method {
        if t.mcount == 0 {
                return nil
index 85beea606c80043f4ec1686cfcbf717f1b9f5745..0365eeeabf634f0ad2c1a88709b49bbc66ae70bb 100644 (file)
@@ -160,7 +160,10 @@ type ValueError struct {
 }
 
 func (e *ValueError) Error() string {
-       return "reflect: call of " + e.Method + " on zero Value"
+       if e.Kind == 0 {
+               return "reflect: call of " + e.Method + " on zero Value"
+       }
+       return "reflect: call of " + e.Method + " on " + e.Kind.String() + " Value"
 }
 
 // methodName returns the name of the calling method,