2008-10-15 17:11:51 -07:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
|
2009-03-09 17:47:15 -07:00
|
|
|
// Formatting of reflection types and values for debugging.
|
|
|
|
|
// Not defined as methods so they do not need to be linked into most binaries;
|
|
|
|
|
// the functions are not used by the library itself, only in tests.
|
2008-10-16 16:38:33 -07:00
|
|
|
|
2009-08-12 13:18:37 -07:00
|
|
|
package reflect_test
|
2008-10-15 17:11:51 -07:00
|
|
|
|
|
|
|
|
import (
|
2009-12-15 15:40:16 -08:00
|
|
|
. "reflect"
|
|
|
|
|
"strconv"
|
2008-10-15 17:11:51 -07:00
|
|
|
)
|
|
|
|
|
|
2009-03-09 17:47:15 -07:00
|
|
|
// valueToString returns a textual representation of the reflection value val.
|
|
|
|
|
// For debugging only.
|
|
|
|
|
func valueToString(val Value) string {
|
2009-12-15 15:40:16 -08:00
|
|
|
var str string
|
2009-07-07 11:03:12 -07:00
|
|
|
if val == nil {
|
2009-11-09 12:07:39 -08:00
|
|
|
return "<nil>"
|
2009-07-07 11:03:12 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
typ := val.Type()
|
2009-07-07 11:03:12 -07:00
|
|
|
switch val := val.(type) {
|
|
|
|
|
case *IntValue:
|
2010-06-20 12:16:25 -07:00
|
|
|
return strconv.Itoa64(val.Get())
|
2009-07-07 11:03:12 -07:00
|
|
|
case *UintValue:
|
2010-06-20 12:16:25 -07:00
|
|
|
return strconv.Uitoa64(val.Get())
|
2009-07-07 11:03:12 -07:00
|
|
|
case *FloatValue:
|
2010-06-20 12:16:25 -07:00
|
|
|
return strconv.Ftoa64(float64(val.Get()), 'g', -1)
|
|
|
|
|
case *ComplexValue:
|
|
|
|
|
c := val.Get()
|
|
|
|
|
return strconv.Ftoa64(float64(real(c)), 'g', -1) + "+" + strconv.Ftoa64(float64(imag(c)), 'g', -1) + "i"
|
2009-07-07 11:03:12 -07:00
|
|
|
case *StringValue:
|
2009-11-09 12:07:39 -08:00
|
|
|
return val.Get()
|
2009-07-07 11:03:12 -07:00
|
|
|
case *BoolValue:
|
|
|
|
|
if val.Get() {
|
2009-11-09 12:07:39 -08:00
|
|
|
return "true"
|
2008-10-31 16:34:47 -07:00
|
|
|
} else {
|
2009-11-09 12:07:39 -08:00
|
|
|
return "false"
|
2008-10-31 16:34:47 -07:00
|
|
|
}
|
2009-07-07 11:03:12 -07:00
|
|
|
case *PtrValue:
|
2009-12-15 15:40:16 -08:00
|
|
|
v := val
|
|
|
|
|
str = typ.String() + "("
|
2009-07-07 11:03:12 -07:00
|
|
|
if v.IsNil() {
|
2009-11-09 12:07:39 -08:00
|
|
|
str += "0"
|
2009-07-07 11:03:12 -07:00
|
|
|
} else {
|
2009-11-09 12:07:39 -08:00
|
|
|
str += "&" + valueToString(v.Elem())
|
2009-07-07 11:03:12 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
str += ")"
|
|
|
|
|
return str
|
2009-07-07 11:03:12 -07:00
|
|
|
case ArrayOrSliceValue:
|
2009-12-15 15:40:16 -08:00
|
|
|
v := val
|
|
|
|
|
str += typ.String()
|
|
|
|
|
str += "{"
|
2008-11-05 10:17:38 -08:00
|
|
|
for i := 0; i < v.Len(); i++ {
|
2008-10-22 11:02:56 -07:00
|
|
|
if i > 0 {
|
2009-11-09 12:07:39 -08:00
|
|
|
str += ", "
|
2008-10-22 11:02:56 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
str += valueToString(v.Elem(i))
|
2008-10-22 11:02:56 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
str += "}"
|
|
|
|
|
return str
|
2009-07-07 11:03:12 -07:00
|
|
|
case *MapValue:
|
2009-12-15 15:40:16 -08:00
|
|
|
t := typ.(*MapType)
|
|
|
|
|
str = t.String()
|
|
|
|
|
str += "{"
|
|
|
|
|
str += "<can't iterate on maps>"
|
|
|
|
|
str += "}"
|
|
|
|
|
return str
|
2009-07-07 11:03:12 -07:00
|
|
|
case *ChanValue:
|
2009-12-15 15:40:16 -08:00
|
|
|
str = typ.String()
|
|
|
|
|
return str
|
2009-07-07 11:03:12 -07:00
|
|
|
case *StructValue:
|
2009-12-15 15:40:16 -08:00
|
|
|
t := typ.(*StructType)
|
|
|
|
|
v := val
|
|
|
|
|
str += t.String()
|
|
|
|
|
str += "{"
|
2009-07-07 11:03:12 -07:00
|
|
|
for i, n := 0, v.NumField(); i < n; i++ {
|
2008-10-22 11:02:56 -07:00
|
|
|
if i > 0 {
|
2009-11-09 12:07:39 -08:00
|
|
|
str += ", "
|
2008-10-22 11:02:56 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
str += valueToString(v.Field(i))
|
2008-10-22 11:02:56 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
str += "}"
|
|
|
|
|
return str
|
2009-07-07 11:03:12 -07:00
|
|
|
case *InterfaceValue:
|
2009-11-09 12:07:39 -08:00
|
|
|
return typ.String() + "(" + valueToString(val.Elem()) + ")"
|
2009-07-07 11:03:12 -07:00
|
|
|
case *FuncValue:
|
2009-12-15 15:40:16 -08:00
|
|
|
v := val
|
|
|
|
|
return typ.String() + "(" + strconv.Itoa64(int64(v.Get())) + ")"
|
2008-10-17 18:06:29 -07:00
|
|
|
default:
|
2010-03-24 16:46:53 -07:00
|
|
|
panic("valueToString: can't print type " + typ.String())
|
2008-10-17 18:06:29 -07:00
|
|
|
}
|
2009-12-15 15:40:16 -08:00
|
|
|
return "valueToString: can't happen"
|
2008-10-15 17:11:51 -07:00
|
|
|
}
|