bug fixed for PK marshal & unmarshl
This commit is contained in:
parent
c6d2321d63
commit
be6e7ac47d
17
pk.go
17
pk.go
|
@ -1,7 +1,8 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"bytes"
|
||||||
|
"encoding/gob"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PK []interface{}
|
type PK []interface{}
|
||||||
|
@ -12,14 +13,14 @@ func NewPK(pks ...interface{}) *PK {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PK) ToString() (string, error) {
|
func (p *PK) ToString() (string, error) {
|
||||||
bs, err := json.Marshal(*p)
|
buf := new(bytes.Buffer)
|
||||||
if err != nil {
|
enc := gob.NewEncoder(buf)
|
||||||
return "", nil
|
err := enc.Encode(*p)
|
||||||
}
|
return buf.String(), err
|
||||||
|
|
||||||
return string(bs), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PK) FromString(content string) error {
|
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
|
||||||
}
|
}
|
||||||
|
|
11
pk_test.go
11
pk_test.go
|
@ -2,6 +2,7 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -19,4 +20,14 @@ func TestPK(t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
fmt.Println(s)
|
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]))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue