Commit 2b083727 authored by Rob Pike's avatar Rob Pike

add []byte as a special case: it will have a special, efficient encoding.

R=rsc
DELTA=16  (9 added, 1 deleted, 6 changed)
OCL=30846
CL=30846
parent 104d0246
...@@ -57,6 +57,7 @@ var tUint Type ...@@ -57,6 +57,7 @@ var tUint Type
var tFloat32 Type var tFloat32 Type
var tFloat64 Type var tFloat64 Type
var tString Type var tString Type
var tBytes Type
// Array type // Array type
type arrayType struct { type arrayType struct {
...@@ -157,9 +158,12 @@ func newTypeObject(name string, rt reflect.Type) Type { ...@@ -157,9 +158,12 @@ func newTypeObject(name string, rt reflect.Type) Type {
case reflect.StringKind: case reflect.StringKind:
return tString return tString
case reflect.ArrayKind: case reflect.ArrayKind:
// TODO(r): worth a special case for array of bytes?
at := rt.(reflect.ArrayType); at := rt.(reflect.ArrayType);
if at.IsSlice() { if at.IsSlice() {
// []byte == []uint8 is a special case
if at.Elem().Kind() == reflect.Uint8Kind {
return tBytes
}
return newSliceType(name, newType("", at.Elem())); return newSliceType(name, newType("", at.Elem()));
} else { } else {
return newArrayType(name, newType("", at.Elem()), at.Len()); return newArrayType(name, newType("", at.Elem()), at.Len());
...@@ -236,5 +240,7 @@ func init() { ...@@ -236,5 +240,7 @@ func init() {
tUint = bootstrapType("uint", uint(0)); tUint = bootstrapType("uint", uint(0));
tFloat32 = bootstrapType("float32", float32(0)); tFloat32 = bootstrapType("float32", float32(0));
tFloat64 = bootstrapType("float64", float64(0)); tFloat64 = bootstrapType("float64", float64(0));
// The string for tBytes is "bytes" not "[]byte" to signify its specialness.
tBytes = bootstrapType("bytes", make([]byte, 0));
tString= bootstrapType("string", ""); tString= bootstrapType("string", "");
} }
...@@ -30,6 +30,7 @@ var basicTypes = []typeT { ...@@ -30,6 +30,7 @@ var basicTypes = []typeT {
typeT { tUint, "uint" }, typeT { tUint, "uint" },
typeT { tFloat32, "float32" }, typeT { tFloat32, "float32" },
typeT { tFloat64, "float64" }, typeT { tFloat64, "float64" },
typeT { tBytes, "bytes" },
typeT { tString, "string" }, typeT { tString, "string" },
} }
...@@ -115,18 +116,19 @@ type Foo struct { ...@@ -115,18 +116,19 @@ type Foo struct {
a int; a int;
b int32; // will become int b int32; // will become int
c string; c string;
d *float; // will become float32 d []byte;
e ****float64; // will become float64 e *float; // will become float32
f *Bar; f ****float64; // will become float64
g *Bar; // should not interpolate the definition of Bar again g *Bar;
h *Foo; // will not explode h *Bar; // should not interpolate the definition of Bar again
i *Foo; // will not explode
} }
func TestStructType(t *testing.T) { func TestStructType(t *testing.T) {
sstruct := GetType("Foo", Foo{}); sstruct := GetType("Foo", Foo{});
str := sstruct.String(); str := sstruct.String();
// If we can print it correctly, we built it correctly. // If we can print it correctly, we built it correctly.
expected := "Foo = struct { a int; b int; c string; d float32; e float64; f Bar = struct { x string; }; g Bar; h Foo; }"; expected := "Foo = struct { a int; b int; c string; d bytes; e float32; f float64; g Bar = struct { x string; }; h Bar; i Foo; }";
if str != expected { if str != expected {
t.Errorf("struct printed as %q; expected %q", str, expected); t.Errorf("struct printed as %q; expected %q", str, expected);
} }
......
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