Merge pull request #89 from ngmoco/master

Go string support
This commit is contained in:
INADA Naoki 2012-04-30 10:05:16 -07:00
commit 2a664b9ae6
2 changed files with 6 additions and 2 deletions

View File

@ -289,13 +289,13 @@ func TestPackMap(t *testing.T) {
func TestPack(t *testing.T) { func TestPack(t *testing.T) {
b := &bytes.Buffer{} b := &bytes.Buffer{}
for _, i := range [](interface{}){nil, false, true, 0, 1, 2, 3, 127, -32, -1, -33, 128} { for _, i := range [](interface{}){nil, false, true, 0, 1, 2, 3, 127, -32, -1, -33, 128, "astring"} {
_, err := Pack(b, i) _, err := Pack(b, i)
if err != nil { if err != nil {
t.Error("err != nil") t.Error("err != nil")
} }
} }
if bytes.Compare(b.Bytes(), []byte{0xc0, 0xc2, 0xc3, 0x00, 0x01, 0x02, 0x03, 0x7f, 0xf0, 0xff, 0xd0, 0xef, 0xd1, 0x00, 0x80}) == 0 { if bytes.Compare(b.Bytes(), []byte{0xc0, 0xc2, 0xc3, 0x00, 0x01, 0x02, 0x03, 0x7f, 0xe0, 0xff, 0xd0, 0xdf, 0xd1, 0x00, 0x80, 0xA7, 'a', 's', 't', 'r', 'i', 'n', 'g'}) != 0 {
t.Error("wrong output") t.Error("wrong output")
} }
} }

View File

@ -699,6 +699,8 @@ func PackValue(writer io.Writer, value reflect.Value) (n int, err error) {
return PackArray(writer, _value) return PackArray(writer, _value)
case reflect.Map: case reflect.Map:
return PackMap(writer, _value) return PackMap(writer, _value)
case reflect.String:
return PackBytes(writer, []byte(_value.String()))
case reflect.Interface: case reflect.Interface:
__value := reflect.ValueOf(_value.Interface()) __value := reflect.ValueOf(_value.Interface())
@ -765,6 +767,8 @@ func Pack(writer io.Writer, value interface{}) (n int, err error) {
return PackFloat32Array(writer, _value) return PackFloat32Array(writer, _value)
case []float64: case []float64:
return PackFloat64Array(writer, _value) return PackFloat64Array(writer, _value)
case string:
return PackBytes(writer, []byte(_value))
default: default:
return PackValue(writer, reflect.ValueOf(value)) return PackValue(writer, reflect.ValueOf(value))
} }