mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
gob: when sending an interface value, indirect down to the
underlying type; otherwise encoding fails when sending a pointer value. R=rsc CC=golang-dev https://golang.org/cl/2922041
This commit is contained in:
parent
836529a63c
commit
9f7f69d300
2 changed files with 39 additions and 2 deletions
|
|
@ -1225,7 +1225,6 @@ func TestInterfaceBasic(t *testing.T) {
|
|||
"hello",
|
||||
[]byte("sailor"),
|
||||
}
|
||||
// Register the types.
|
||||
err := NewEncoder(b).Encode(item1)
|
||||
if err != nil {
|
||||
t.Error("expected no encode error; got", err)
|
||||
|
|
@ -1248,6 +1247,44 @@ func TestInterfaceBasic(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
type String string
|
||||
|
||||
type PtrInterfaceItem struct {
|
||||
str interface{} // basic
|
||||
Str interface{} // derived
|
||||
}
|
||||
|
||||
// We'll send pointers; should receive values.
|
||||
// Also check that we can register T but send *T.
|
||||
func TestInterfacePointer(t *testing.T) {
|
||||
b := new(bytes.Buffer)
|
||||
str1 := "howdy"
|
||||
str2 := String("kiddo")
|
||||
item1 := &PtrInterfaceItem{
|
||||
&str1,
|
||||
&str2,
|
||||
}
|
||||
// Register the type.
|
||||
Register(str2)
|
||||
err := NewEncoder(b).Encode(item1)
|
||||
if err != nil {
|
||||
t.Error("expected no encode error; got", err)
|
||||
}
|
||||
|
||||
item2 := &PtrInterfaceItem{}
|
||||
err = NewDecoder(b).Decode(&item2)
|
||||
if err != nil {
|
||||
t.Fatal("decode:", err)
|
||||
}
|
||||
// Hand test for correct types and values.
|
||||
if v, ok := item2.str.(string); !ok || v != str1 {
|
||||
t.Errorf("basic string failed: %q should be %q", v, str1)
|
||||
}
|
||||
if v, ok := item2.Str.(String); !ok || v != str2 {
|
||||
t.Errorf("derived type String failed: %q should be %q", v, str2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIgnoreInterface(t *testing.T) {
|
||||
iVal := Int(3)
|
||||
fVal := Float(5)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue