Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/goPublic

Commit0bb20c6

Browse files
committed
[release-branch.go1.2] encoding/gob: do not use MarshalText, UnmarshalText
««« CL 22770044 / 23fc3139589cencoding/gob: do not use MarshalText, UnmarshalTextThis seems to be the best of a long list of bad ways to fix this issue.Fixes#6760.R=rCC=golang-devhttps://golang.org/cl/22770044»»»R=golang-devCC=golang-devhttps://golang.org/cl/28110043
1 parent3409e2a commit0bb20c6

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

‎doc/go1.2.html‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,8 @@ <h3 id="minor_library_changes">Minor changes to the library</h3>
736736
even if they are not. That is, it ignores them completely. Previously they would
737737
trigger an error, which could cause unexpected compatibility problems if an
738738
embedded structure added such a field.
739-
The package also now supports the generic encoding interfaces of the
739+
The package also now supports the generic<code>BinaryMarshaler</code> and
740+
<code>BinaryUnmarshaler</code> interfaces of the
740741
<ahref="/pkg/encoding/"><code>encoding</code></a> package
741742
described above.
742743
</li>

‎src/pkg/encoding/gob/doc.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ Functions and channels will not be sent in a gob. Attempting to encode such a va
8686
at top the level will fail. A struct field of chan or func type is treated exactly
8787
like an unexported field and is ignored.
8888
89-
Gob can encode a value of any type implementing the GobEncoder,
90-
encoding.BinaryMarshaler, or encoding.TextMarshalerinterfaces by calling the
91-
corresponding method,in that order of preference.
89+
Gob can encode a value of any type implementing the GobEncoder or
90+
encoding.BinaryMarshalerinterfaces by calling the corresponding method,
91+
in that order of preference.
9292
93-
Gob can decode a value of any type implementing the GobDecoder,
94-
encoding.BinaryUnmarshaler, or encoding.TextUnmarshalerinterfaces by calling
95-
the corresponding method,again in that order of preference.
93+
Gob can decode a value of any type implementing the GobDecoder or
94+
encoding.BinaryUnmarshalerinterfaces by calling the corresponding method,
95+
again in that order of preference.
9696
9797
Encoding Details
9898

‎src/pkg/encoding/gob/gobencdec_test.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"fmt"
1313
"io"
14+
"net"
1415
"strings"
1516
"testing"
1617
"time"
@@ -767,3 +768,17 @@ func TestGobEncodePtrError(t *testing.T) {
767768
t.Fatalf("expected nil, got %v",err2)
768769
}
769770
}
771+
772+
funcTestNetIP(t*testing.T) {
773+
// Encoding of net.IP{1,2,3,4} in Go 1.1.
774+
enc:= []byte{0x07,0x0a,0x00,0x04,0x01,0x02,0x03,0x04}
775+
776+
varip net.IP
777+
err:=NewDecoder(bytes.NewReader(enc)).Decode(&ip)
778+
iferr!=nil {
779+
t.Fatalf("decode: %v",err)
780+
}
781+
ifip.String()!="1.2.3.4" {
782+
t.Errorf("decoded to %v, want 1.2.3.4",ip.String())
783+
}
784+
}

‎src/pkg/encoding/gob/type.go‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,25 @@ func validUserType(rt reflect.Type) (ut *userTypeInfo, err error) {
8888
ut.externalEnc,ut.encIndir=xGob,indir
8989
}elseifok,indir:=implementsInterface(ut.user,binaryMarshalerInterfaceType);ok {
9090
ut.externalEnc,ut.encIndir=xBinary,indir
91-
}elseifok,indir:=implementsInterface(ut.user,textMarshalerInterfaceType);ok {
92-
ut.externalEnc,ut.encIndir=xText,indir
9391
}
9492

93+
// NOTE(rsc): Would like to allow MarshalText here, but results in incompatibility
94+
// with older encodings for net.IP. See golang.org/issue/6760.
95+
// } else if ok, indir := implementsInterface(ut.user, textMarshalerInterfaceType); ok {
96+
// ut.externalEnc, ut.encIndir = xText, indir
97+
// }
98+
9599
ifok,indir:=implementsInterface(ut.user,gobDecoderInterfaceType);ok {
96100
ut.externalDec,ut.decIndir=xGob,indir
97101
}elseifok,indir:=implementsInterface(ut.user,binaryUnmarshalerInterfaceType);ok {
98102
ut.externalDec,ut.decIndir=xBinary,indir
99-
}elseifok,indir:=implementsInterface(ut.user,textUnmarshalerInterfaceType);ok {
100-
ut.externalDec,ut.decIndir=xText,indir
101103
}
102104

105+
// See note above.
106+
// } else if ok, indir := implementsInterface(ut.user, textUnmarshalerInterfaceType); ok {
107+
// ut.externalDec, ut.decIndir = xText, indir
108+
// }
109+
103110
userTypeCache[rt]=ut
104111
return
105112
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp