2008-10-15 18:11:51 -06: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.
|
|
|
|
|
2008-10-16 17:38:33 -06:00
|
|
|
// Reflection library.
|
2009-03-09 18:47:15 -06: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 17:38:33 -06:00
|
|
|
|
2008-10-15 18:11:51 -06:00
|
|
|
package reflect
|
|
|
|
|
|
|
|
import (
|
2008-10-16 12:12:02 -06:00
|
|
|
"reflect";
|
2008-11-17 13:34:03 -07:00
|
|
|
"strconv";
|
2008-10-15 18:11:51 -06:00
|
|
|
)
|
|
|
|
|
2009-03-09 18:47:15 -06:00
|
|
|
// valueToString returns a textual representation of the reflection value val.
|
|
|
|
// For debugging only.
|
|
|
|
func valueToString(val Value) string {
|
2008-10-17 19:06:29 -06:00
|
|
|
var str string;
|
2009-07-07 12:03:12 -06:00
|
|
|
if val == nil {
|
|
|
|
return "<nil>";
|
|
|
|
}
|
2008-10-17 19:06:29 -06:00
|
|
|
typ := val.Type();
|
2009-07-07 12:03:12 -06:00
|
|
|
switch val := val.(type) {
|
|
|
|
case *IntValue:
|
|
|
|
return strconv.Uitoa64(uint64(val.Get()));
|
|
|
|
case *Int8Value:
|
|
|
|
return strconv.Itoa64(int64(val.Get()));
|
|
|
|
case *Int16Value:
|
|
|
|
return strconv.Itoa64(int64(val.Get()));
|
|
|
|
case *Int32Value:
|
|
|
|
return strconv.Itoa64(int64(val.Get()));
|
|
|
|
case *Int64Value:
|
|
|
|
return strconv.Itoa64(int64(val.Get()));
|
|
|
|
case *UintValue:
|
|
|
|
return strconv.Itoa64(int64(val.Get()));
|
|
|
|
case *Uint8Value:
|
|
|
|
return strconv.Itoa64(int64(val.Get()));
|
|
|
|
case *Uint16Value:
|
|
|
|
return strconv.Itoa64(int64(val.Get()));
|
|
|
|
case *Uint32Value:
|
|
|
|
return strconv.Itoa64(int64(val.Get()));
|
|
|
|
case *Uint64Value:
|
|
|
|
return strconv.Uitoa64(uint64(val.Get()));
|
|
|
|
case *FloatValue:
|
2009-01-15 17:16:42 -07:00
|
|
|
if strconv.FloatSize == 32 {
|
2009-07-07 12:03:12 -06:00
|
|
|
return strconv.Ftoa32(float32(val.Get()), 'g', -1);
|
2008-11-17 13:34:03 -07:00
|
|
|
} else {
|
2009-07-07 12:03:12 -06:00
|
|
|
return strconv.Ftoa64(float64(val.Get()), 'g', -1);
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-07-07 12:03:12 -06:00
|
|
|
case *Float32Value:
|
|
|
|
return strconv.Ftoa32(val.Get(), 'g', -1);
|
|
|
|
case *Float64Value:
|
|
|
|
return strconv.Ftoa64(val.Get(), 'g', -1);
|
|
|
|
case *StringValue:
|
|
|
|
return val.Get();
|
|
|
|
case *BoolValue:
|
|
|
|
if val.Get() {
|
2008-10-31 17:34:47 -06:00
|
|
|
return "true"
|
|
|
|
} else {
|
|
|
|
return "false"
|
|
|
|
}
|
2009-07-07 12:03:12 -06:00
|
|
|
case *PtrValue:
|
|
|
|
v := val;
|
|
|
|
str = typ.String() + "(";
|
|
|
|
if v.IsNil() {
|
|
|
|
str += "0";
|
|
|
|
} else {
|
|
|
|
str += "&" + valueToString(v.Elem());
|
|
|
|
}
|
|
|
|
str += ")";
|
|
|
|
return str;
|
|
|
|
case ArrayOrSliceValue:
|
|
|
|
v := val;
|
|
|
|
str += typ.String();
|
2008-10-22 12:02:56 -06:00
|
|
|
str += "{";
|
2008-11-05 11:17:38 -07:00
|
|
|
for i := 0; i < v.Len(); i++ {
|
2008-10-22 12:02:56 -06:00
|
|
|
if i > 0 {
|
|
|
|
str += ", "
|
|
|
|
}
|
2009-03-09 18:47:15 -06:00
|
|
|
str += valueToString(v.Elem(i));
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
str += "}";
|
|
|
|
return str;
|
2009-07-07 12:03:12 -06:00
|
|
|
case *MapValue:
|
|
|
|
t := typ.(*MapType);
|
|
|
|
v := val;
|
|
|
|
str = t.String();
|
2008-10-22 12:02:56 -06:00
|
|
|
str += "{";
|
|
|
|
str += "<can't iterate on maps>";
|
|
|
|
str += "}";
|
|
|
|
return str;
|
2009-07-07 12:03:12 -06:00
|
|
|
case *ChanValue:
|
|
|
|
str = typ.String();
|
2008-12-19 04:06:19 -07:00
|
|
|
return str;
|
2009-07-07 12:03:12 -06:00
|
|
|
case *StructValue:
|
|
|
|
t := typ.(*StructType);
|
|
|
|
v := val;
|
|
|
|
str += t.String();
|
2008-10-22 12:02:56 -06:00
|
|
|
str += "{";
|
2009-07-07 12:03:12 -06:00
|
|
|
for i, n := 0, v.NumField(); i < n; i++ {
|
2008-10-22 12:02:56 -06:00
|
|
|
if i > 0 {
|
|
|
|
str += ", "
|
|
|
|
}
|
2009-03-09 18:47:15 -06:00
|
|
|
str += valueToString(v.Field(i));
|
2008-10-22 12:02:56 -06:00
|
|
|
}
|
|
|
|
str += "}";
|
|
|
|
return str;
|
2009-07-07 12:03:12 -06:00
|
|
|
case *InterfaceValue:
|
|
|
|
return typ.String() + "(" + valueToString(val.Elem()) + ")";
|
|
|
|
case *FuncValue:
|
|
|
|
v := val;
|
|
|
|
return typ.String() + "(" + strconv.Itoa64(int64(v.Get())) + ")";
|
2008-10-17 19:06:29 -06:00
|
|
|
default:
|
2009-07-07 12:03:12 -06:00
|
|
|
panicln("reflect.valueToString: can't print type ", typ.String());
|
2008-10-17 19:06:29 -06:00
|
|
|
}
|
2009-03-09 18:47:15 -06:00
|
|
|
return "reflect.valueToString: can't happen";
|
2008-10-15 18:11:51 -06:00
|
|
|
}
|