Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
og-rek
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
og-rek
Commits
90bef817
Commit
90bef817
authored
Feb 14, 2017
by
Kamil Kisiel
Committed by
GitHub
Feb 14, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #26 from navytux/fix1
Misc fixes (error + opStop handling)
parents
28bae785
995fce81
Changes
4
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
94 additions
and
11 deletions
+94
-11
encode.go
encode.go
+6
-3
encode_test.go
encode_test.go
+35
-1
ogorek.go
ogorek.go
+8
-5
ogorek_test.go
ogorek_test.go
+45
-2
No files found.
encode.go
View file @
90bef817
...
...
@@ -29,9 +29,12 @@ func NewEncoder(w io.Writer) *Encoder {
// Encode writes the pickle encoding of v to w, the encoder's writer
func
(
e
*
Encoder
)
Encode
(
v
interface
{})
error
{
rv
:=
reflectValueOf
(
v
)
e
.
encode
(
rv
)
e
.
w
.
Write
([]
byte
{
opStop
})
return
nil
err
:=
e
.
encode
(
rv
)
if
err
!=
nil
{
return
err
}
_
,
err
=
e
.
w
.
Write
([]
byte
{
opStop
})
return
err
}
func
(
e
*
Encoder
)
encode
(
rv
reflect
.
Value
)
error
{
...
...
encode_test.go
View file @
90bef817
...
...
@@ -2,6 +2,7 @@ package ogórek
import
(
"bytes"
"io"
"reflect"
"testing"
)
...
...
@@ -38,7 +39,10 @@ func TestEncode(t *testing.T) {
for
_
,
tt
:=
range
tests
{
p
:=
&
bytes
.
Buffer
{}
e
:=
NewEncoder
(
p
)
e
.
Encode
(
tt
.
input
)
err
:=
e
.
Encode
(
tt
.
input
)
if
err
!=
nil
{
t
.
Errorf
(
"%s: encode error: %v"
,
tt
.
name
,
err
)
}
d
:=
NewDecoder
(
bytes
.
NewReader
(
p
.
Bytes
()))
output
,
_
:=
d
.
Decode
()
...
...
@@ -52,5 +56,35 @@ func TestEncode(t *testing.T) {
t
.
Errorf
(
"%s: got
\n
%q
\n
expected
\n
%q"
,
tt
.
name
,
output
,
want
)
}
for
l
:=
int64
(
p
.
Len
())
-
1
;
l
>=
0
;
l
--
{
p
.
Reset
()
e
:=
NewEncoder
(
LimitWriter
(
p
,
l
))
err
=
e
.
Encode
(
tt
.
input
)
if
err
!=
io
.
EOF
{
t
.
Errorf
(
"%s: encoder did not handle write error @%v: got %#v"
,
tt
.
name
,
l
,
err
)
}
}
}
}
// like io.LimitedReader but for writes
// XXX it would be good to have it in stdlib
type
LimitedWriter
struct
{
W
io
.
Writer
N
int64
}
func
(
l
*
LimitedWriter
)
Write
(
p
[]
byte
)
(
n
int
,
err
error
)
{
if
l
.
N
<=
0
{
return
0
,
io
.
EOF
}
if
int64
(
len
(
p
))
>
l
.
N
{
p
=
p
[
0
:
l
.
N
]
}
n
,
err
=
l
.
W
.
Write
(
p
)
l
.
N
-=
int64
(
n
)
return
}
func
LimitWriter
(
w
io
.
Writer
,
n
int64
)
io
.
Writer
{
return
&
LimitedWriter
{
w
,
n
}
}
ogorek.go
View file @
90bef817
...
...
@@ -119,20 +119,23 @@ func NewDecoder(r io.Reader) Decoder {
func
(
d
Decoder
)
Decode
()
(
interface
{},
error
)
{
insn
:=
0
loop
:
for
{
insn
++
key
,
err
:=
d
.
r
.
ReadByte
()
if
err
==
io
.
EOF
{
break
}
else
if
err
!=
nil
{
if
err
!=
nil
{
if
err
==
io
.
EOF
&&
insn
!=
0
{
err
=
io
.
ErrUnexpectedEOF
}
return
nil
,
err
}
insn
++
switch
key
{
case
opMark
:
d
.
mark
()
case
opStop
:
break
break
loop
case
opPop
:
d
.
pop
()
case
opPopMark
:
...
...
ogorek_test.go
View file @
90bef817
This diff is collapsed.
Click to expand it.
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