Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
12a4d843
Commit
12a4d843
authored
Jun 29, 2010
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gob: add DecodeValue and EncodeValue
R=rsc CC=golang-dev
https://golang.org/cl/1698045
parent
6d61da63
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
30 additions
and
18 deletions
+30
-18
codec_test.go
src/pkg/gob/codec_test.go
+1
-1
decode.go
src/pkg/gob/decode.go
+4
-4
decoder.go
src/pkg/gob/decoder.go
+10
-3
encode.go
src/pkg/gob/encode.go
+6
-7
encoder.go
src/pkg/gob/encoder.go
+9
-3
No files found.
src/pkg/gob/codec_test.go
View file @
12a4d843
...
...
@@ -1028,7 +1028,7 @@ func TestInvalidField(t *testing.T) {
var
bad0
Bad0
bad0
.
inter
=
17
b
:=
new
(
bytes
.
Buffer
)
err
:=
encode
(
b
,
&
bad0
)
err
:=
encode
(
b
,
reflect
.
NewValue
(
&
bad0
)
)
if
err
==
nil
{
t
.
Error
(
"expected error; got none"
)
}
else
if
strings
.
Index
(
err
.
String
(),
"interface"
)
<
0
{
...
...
src/pkg/gob/decode.go
View file @
12a4d843
...
...
@@ -918,9 +918,9 @@ func (dec *Decoder) getIgnoreEnginePtr(wireId typeId) (enginePtr **decEngine, er
return
}
func
(
dec
*
Decoder
)
decode
(
wireId
typeId
,
e
interface
{}
)
os
.
Error
{
func
(
dec
*
Decoder
)
decode
(
wireId
typeId
,
val
reflect
.
Value
)
os
.
Error
{
// Dereference down to the underlying struct type.
rt
,
indir
:=
indirect
(
reflect
.
Typeof
(
e
))
rt
,
indir
:=
indirect
(
val
.
Type
(
))
enginePtr
,
err
:=
dec
.
getDecEnginePtr
(
wireId
,
rt
)
if
err
!=
nil
{
return
err
...
...
@@ -931,9 +931,9 @@ func (dec *Decoder) decode(wireId typeId, e interface{}) os.Error {
name
:=
rt
.
Name
()
return
os
.
ErrorString
(
"gob: type mismatch: no fields matched compiling decoder for "
+
name
)
}
return
decodeStruct
(
engine
,
st
,
dec
.
state
.
b
,
uintptr
(
reflect
.
NewValue
(
e
)
.
Addr
()),
indir
)
return
decodeStruct
(
engine
,
st
,
dec
.
state
.
b
,
uintptr
(
val
.
Addr
()),
indir
)
}
return
decodeSingle
(
engine
,
rt
,
dec
.
state
.
b
,
uintptr
(
reflect
.
NewValue
(
e
)
.
Addr
()),
indir
)
return
decodeSingle
(
engine
,
rt
,
dec
.
state
.
b
,
uintptr
(
val
.
Addr
()),
indir
)
}
func
init
()
{
...
...
src/pkg/gob/decoder.go
View file @
12a4d843
...
...
@@ -47,7 +47,7 @@ func (dec *Decoder) recvType(id typeId) {
// Type:
wire
:=
new
(
wireType
)
dec
.
state
.
err
=
dec
.
decode
(
tWireType
,
wire
)
dec
.
state
.
err
=
dec
.
decode
(
tWireType
,
reflect
.
NewValue
(
wire
)
)
// Remember we've seen this type.
dec
.
wireType
[
id
]
=
wire
}
...
...
@@ -55,7 +55,7 @@ func (dec *Decoder) recvType(id typeId) {
// Decode reads the next value from the connection and stores
// it in the data represented by the empty interface value.
// The value underlying e must be the correct type for the next
// data item received.
// data item received
, which must be a pointer
.
func
(
dec
*
Decoder
)
Decode
(
e
interface
{})
os
.
Error
{
// If e represents a value, the answer won't get back to the
// caller. Make sure it's a pointer.
...
...
@@ -63,7 +63,14 @@ func (dec *Decoder) Decode(e interface{}) os.Error {
dec
.
state
.
err
=
os
.
ErrorString
(
"gob: attempt to decode into a non-pointer"
)
return
dec
.
state
.
err
}
return
dec
.
DecodeValue
(
reflect
.
NewValue
(
e
))
}
// DecodeValue reads the next value from the connection and stores
// it in the data represented by the reflection value.
// The value must be the correct type for the next
// data item received.
func
(
dec
*
Decoder
)
DecodeValue
(
value
reflect
.
Value
)
os
.
Error
{
// Make sure we're single-threaded through here.
dec
.
mutex
.
Lock
()
defer
dec
.
mutex
.
Unlock
()
...
...
@@ -114,7 +121,7 @@ func (dec *Decoder) Decode(e interface{}) os.Error {
dec
.
state
.
err
=
errBadType
break
}
dec
.
state
.
err
=
dec
.
decode
(
id
,
e
)
dec
.
state
.
err
=
dec
.
decode
(
id
,
valu
e
)
break
}
return
dec
.
state
.
err
...
...
src/pkg/gob/encode.go
View file @
12a4d843
...
...
@@ -798,12 +798,11 @@ func getEncEngine(rt reflect.Type) (*encEngine, os.Error) {
return
info
.
encoder
,
err
}
func
encode
(
b
*
bytes
.
Buffer
,
e
interface
{}
)
os
.
Error
{
func
encode
(
b
*
bytes
.
Buffer
,
value
reflect
.
Value
)
os
.
Error
{
// Dereference down to the underlying object.
rt
,
indir
:=
indirect
(
reflect
.
Typeof
(
e
))
v
:=
reflect
.
NewValue
(
e
)
rt
,
indir
:=
indirect
(
value
.
Type
())
for
i
:=
0
;
i
<
indir
;
i
++
{
v
=
reflect
.
Indirect
(
v
)
v
alue
=
reflect
.
Indirect
(
value
)
}
typeLock
.
Lock
()
engine
,
err
:=
getEncEngine
(
rt
)
...
...
@@ -811,8 +810,8 @@ func encode(b *bytes.Buffer, e interface{}) os.Error {
if
err
!=
nil
{
return
err
}
if
_
,
ok
:=
v
.
(
*
reflect
.
StructValue
);
ok
{
return
encodeStruct
(
engine
,
b
,
v
.
Addr
())
if
_
,
ok
:=
v
alue
.
(
*
reflect
.
StructValue
);
ok
{
return
encodeStruct
(
engine
,
b
,
v
alue
.
Addr
())
}
return
encodeSingle
(
engine
,
b
,
v
.
Addr
())
return
encodeSingle
(
engine
,
b
,
v
alue
.
Addr
())
}
src/pkg/gob/encoder.go
View file @
12a4d843
...
...
@@ -115,7 +115,7 @@ func (enc *Encoder) sendType(origt reflect.Type) (sent bool) {
// Id:
encodeInt
(
enc
.
state
,
-
int64
(
info
.
id
))
// Type:
encode
(
enc
.
state
.
b
,
info
.
wire
)
encode
(
enc
.
state
.
b
,
reflect
.
NewValue
(
info
.
wire
)
)
enc
.
send
()
if
enc
.
state
.
err
!=
nil
{
return
...
...
@@ -140,13 +140,19 @@ func (enc *Encoder) sendType(origt reflect.Type) (sent bool) {
// Encode transmits the data item represented by the empty interface value,
// guaranteeing that all necessary type information has been transmitted first.
func
(
enc
*
Encoder
)
Encode
(
e
interface
{})
os
.
Error
{
return
enc
.
EncodeValue
(
reflect
.
NewValue
(
e
))
}
// EncodeValue transmits the data item represented by the reflection value,
// guaranteeing that all necessary type information has been transmitted first.
func
(
enc
*
Encoder
)
EncodeValue
(
value
reflect
.
Value
)
os
.
Error
{
// Make sure we're single-threaded through here, so multiple
// goroutines can share an encoder.
enc
.
mutex
.
Lock
()
defer
enc
.
mutex
.
Unlock
()
enc
.
state
.
err
=
nil
rt
,
_
:=
indirect
(
reflect
.
Typeof
(
e
))
rt
,
_
:=
indirect
(
value
.
Type
(
))
// Sanity check only: encoder should never come in with data present.
if
enc
.
state
.
b
.
Len
()
>
0
||
enc
.
countState
.
b
.
Len
()
>
0
{
...
...
@@ -181,7 +187,7 @@ func (enc *Encoder) Encode(e interface{}) os.Error {
encodeInt
(
enc
.
state
,
int64
(
enc
.
sent
[
rt
]))
// Encode the object.
err
:=
encode
(
enc
.
state
.
b
,
e
)
err
:=
encode
(
enc
.
state
.
b
,
valu
e
)
if
err
!=
nil
{
enc
.
setError
(
err
)
}
else
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment