Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-ld-format
Commits
c72f1b0d
Commit
c72f1b0d
authored
Jul 11, 2017
by
Jakub Sztandera
Committed by
GitHub
Jul 11, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #22 from ipfs/feat/decoder-redux
rework block decoding framework
parents
4cb85ac9
7de71ad7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
13 deletions
+35
-13
coding.go
coding.go
+33
-11
coding_test.go
coding_test.go
+2
-2
No files found.
coding.go
View file @
c72f1b0d
...
...
@@ -2,31 +2,46 @@ package format
import
(
"fmt"
"sync"
blocks
"github.com/ipfs/go-block-format"
)
// DecodeBlock functions decode blocks into nodes.
// DecodeBlock
Func
functions decode blocks into nodes.
type
DecodeBlockFunc
func
(
block
blocks
.
Block
)
(
Node
,
error
)
// Map from codec types to decoder functions
type
BlockDecoder
map
[
uint64
]
DecodeBlockFunc
type
BlockDecoder
interface
{
Register
(
codec
uint64
,
decoder
DecodeBlockFunc
)
Decode
(
blocks
.
Block
)
(
Node
,
error
)
}
type
safeBlockDecoder
struct
{
// Can be replaced with an RCU if necessary.
lock
sync
.
RWMutex
decoders
map
[
uint64
]
DecodeBlockFunc
}
//
A default set of block de
code
rs
.
//
Register registers decoder for all blocks with the passed
code
c
.
//
// You SHOULD populate this map from `init` functions in packages that support
// decoding various IPLD formats. You MUST NOT modify this map once `main` has
// been called.
var
DefaultBlockDecoder
BlockDecoder
=
map
[
uint64
]
DecodeBlockFunc
{}
// This will silently replace any existing registered block decoders.
func
(
d
*
safeBlockDecoder
)
Register
(
codec
uint64
,
decoder
DecodeBlockFunc
)
{
d
.
lock
.
Lock
()
defer
d
.
lock
.
Unlock
()
d
.
decoders
[
codec
]
=
decoder
}
func
(
b
BlockDecoder
)
Decode
(
block
blocks
.
Block
)
(
Node
,
error
)
{
func
(
d
*
safe
BlockDecoder
)
Decode
(
block
blocks
.
Block
)
(
Node
,
error
)
{
// Short-circuit by cast if we already have a Node.
if
node
,
ok
:=
block
.
(
Node
);
ok
{
return
node
,
nil
}
ty
:=
block
.
Cid
()
.
Type
()
if
decoder
,
ok
:=
b
[
ty
];
ok
{
d
.
lock
.
RLock
()
decoder
,
ok
:=
d
.
decoders
[
ty
]
d
.
lock
.
RUnlock
()
if
ok
{
return
decoder
(
block
)
}
else
{
// TODO: get the *long* name for this format
...
...
@@ -34,7 +49,14 @@ func (b BlockDecoder) Decode(block blocks.Block) (Node, error) {
}
}
// Decode the given block using the default block decoder.
var
DefaultBlockDecoder
BlockDecoder
=
&
safeBlockDecoder
{
decoders
:
make
(
map
[
uint64
]
DecodeBlockFunc
)}
// Decode decodes the given block using the default BlockDecoder.
func
Decode
(
block
blocks
.
Block
)
(
Node
,
error
)
{
return
DefaultBlockDecoder
.
Decode
(
block
)
}
// Register registers block decoders with the default BlockDecoder.
func
Register
(
codec
uint64
,
decoder
DecodeBlockFunc
)
{
DefaultBlockDecoder
.
Register
(
codec
,
decoder
)
}
coding_test.go
View file @
c72f1b0d
...
...
@@ -10,13 +10,13 @@ import (
)
func
init
()
{
DefaultBlockDecod
er
[
cid
.
Raw
]
=
func
(
b
blocks
.
Block
)
(
Node
,
error
)
{
Regist
er
(
cid
.
Raw
,
func
(
b
blocks
.
Block
)
(
Node
,
error
)
{
node
:=
&
EmptyNode
{}
if
b
.
RawData
()
!=
nil
||
!
b
.
Cid
()
.
Equals
(
node
.
Cid
())
{
return
nil
,
errors
.
New
(
"can only decode empty blocks"
)
}
return
node
,
nil
}
}
)
}
func
TestDecode
(
t
*
testing
.
T
)
{
...
...
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