Commit ed2ac9b8 authored by Rob Pike's avatar Rob Pike

casify reflect.

R=rsc
DELTA=513  (0 added, 2 deleted, 511 changed)
OCL=22954
CL=22956
parent aedfb397
...@@ -87,8 +87,6 @@ func valuedump(s, t string) { ...@@ -87,8 +87,6 @@ func valuedump(s, t string) {
assert(reflect.ValueToString(v), t); assert(reflect.ValueToString(v), t);
} }
export type empty interface {}
export type T struct { a int; b float64; c string; d *int } export type T struct { a int; b float64; c string; d *int }
export func TestAll(tt *testing.T) { // TODO(r): wrap up better export func TestAll(tt *testing.T) { // TODO(r): wrap up better
...@@ -342,14 +340,14 @@ export func TestBigUnnamedStruct(t *testing.T) { ...@@ -342,14 +340,14 @@ export func TestBigUnnamedStruct(t *testing.T) {
} }
} }
type Big struct { type big struct {
a, b, c, d, e int64 a, b, c, d, e int64
} }
export func TestBigStruct(t *testing.T) { export func TestBigStruct(t *testing.T) {
b := Big{1, 2, 3, 4, 5}; b := big{1, 2, 3, 4, 5};
v := NewValue(b); v := NewValue(b);
b1 := v.Interface().(Big); b1 := v.Interface().(big);
if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d || b1.e != b.e { if b1.a != b.a || b1.b != b.b || b1.c != b.c || b1.d != b.d || b1.e != b.e {
t.Errorf("NewValue(%v).Interface().(Big) = %v", b, b1); t.Errorf("NewValue(%v).Interface().(big) = %v", b, b1);
} }
} }
...@@ -15,7 +15,7 @@ import ( ...@@ -15,7 +15,7 @@ import (
export func TypeToString(typ Type, expand bool) string export func TypeToString(typ Type, expand bool) string
export func ValueToString(val Value) string export func ValueToString(val Value) string
func DoubleQuote(s string) string { func doubleQuote(s string) string {
out := "\""; out := "\"";
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
c := s[i]; c := s[i];
...@@ -38,12 +38,12 @@ func DoubleQuote(s string) string { ...@@ -38,12 +38,12 @@ func DoubleQuote(s string) string {
return out; return out;
} }
type HasFields interface { type hasFields interface {
Field(i int) (name string, typ Type, tag string, offset int); Field(i int) (name string, typ Type, tag string, offset int);
Len() int; Len() int;
} }
func TypeFieldsToString(t HasFields, sep string) string { func typeFieldsToString(t hasFields, sep string) string {
var str string; var str string;
for i := 0; i < t.Len(); i++ { for i := 0; i < t.Len(); i++ {
str1, typ, tag, offset := t.Field(i); str1, typ, tag, offset := t.Field(i);
...@@ -52,7 +52,7 @@ func TypeFieldsToString(t HasFields, sep string) string { ...@@ -52,7 +52,7 @@ func TypeFieldsToString(t HasFields, sep string) string {
} }
str1 += TypeToString(typ, false); str1 += TypeToString(typ, false);
if tag != "" { if tag != "" {
str1 += " " + DoubleQuote(tag); str1 += " " + doubleQuote(tag);
} }
if i < t.Len() - 1 { if i < t.Len() - 1 {
str1 += sep + " "; str1 += sep + " ";
...@@ -62,7 +62,7 @@ func TypeFieldsToString(t HasFields, sep string) string { ...@@ -62,7 +62,7 @@ func TypeFieldsToString(t HasFields, sep string) string {
return str; return str;
} }
func TypeToString(typ Type, expand bool) string { export func TypeToString(typ Type, expand bool) string {
var str string; var str string;
if name := typ.Name(); !expand && name != "" { if name := typ.Name(); !expand && name != "" {
return name return name
...@@ -105,14 +105,14 @@ func TypeToString(typ Type, expand bool) string { ...@@ -105,14 +105,14 @@ func TypeToString(typ Type, expand bool) string {
} }
return str + TypeToString(c.Elem(), false); return str + TypeToString(c.Elem(), false);
case StructKind: case StructKind:
return "struct{" + TypeFieldsToString(typ, ";") + "}"; return "struct{" + typeFieldsToString(typ, ";") + "}";
case InterfaceKind: case InterfaceKind:
return "interface{" + TypeFieldsToString(typ, ";") + "}"; return "interface{" + typeFieldsToString(typ, ";") + "}";
case FuncKind: case FuncKind:
f := typ.(FuncType); f := typ.(FuncType);
str = "(" + TypeFieldsToString(f.In(), ",") + ")"; str = "(" + typeFieldsToString(f.In(), ",") + ")";
if f.Out() != nil { if f.Out() != nil {
str += "(" + TypeFieldsToString(f.Out(), ",") + ")"; str += "(" + typeFieldsToString(f.Out(), ",") + ")";
} }
return str; return str;
default: default:
...@@ -126,7 +126,7 @@ func integer(v int64) string { ...@@ -126,7 +126,7 @@ func integer(v int64) string {
return strconv.Itoa64(v); return strconv.Itoa64(v);
} }
func ValueToString(val Value) string { export func ValueToString(val Value) string {
var str string; var str string;
typ := val.Type(); typ := val.Type();
switch(val.Kind()) { switch(val.Kind()) {
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment