bug fixed for PK marshal & unmarshl

This commit is contained in:
Lunny Xiao 2015-03-14 22:16:36 +08:00
parent c6d2321d63
commit be6e7ac47d
2 changed files with 20 additions and 8 deletions

17
pk.go
View File

@ -1,7 +1,8 @@
package core
import (
"encoding/json"
"bytes"
"encoding/gob"
)
type PK []interface{}
@ -12,14 +13,14 @@ func NewPK(pks ...interface{}) *PK {
}
func (p *PK) ToString() (string, error) {
bs, err := json.Marshal(*p)
if err != nil {
return "", nil
}
return string(bs), nil
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
err := enc.Encode(*p)
return buf.String(), err
}
func (p *PK) FromString(content string) error {
return json.Unmarshal([]byte(content), p)
dec := gob.NewDecoder(bytes.NewBufferString(content))
err := dec.Decode(p)
return err
}

View File

@ -2,6 +2,7 @@ package core
import (
"fmt"
"reflect"
"testing"
)
@ -19,4 +20,14 @@ func TestPK(t *testing.T) {
t.Error(err)
}
fmt.Println(s)
if len(*p) != len(*s) {
t.Fatal("p", *p, "should be equal", *s)
}
for i, ori := range *p {
if ori != (*s)[i] {
t.Fatal("ori", ori, reflect.ValueOf(ori), "should be equal", (*s)[i], reflect.ValueOf((*s)[i]))
}
}
}