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-cidutil
Commits
581df307
Commit
581df307
authored
6 years ago
by
Kevin Atkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add WithCodec and GetCodec methods to format interface.
parent
fcbf84a5
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
0 deletions
+37
-0
format.go
format.go
+37
-0
No files found.
format.go
View file @
581df307
...
...
@@ -6,6 +6,8 @@ import (
type
Format
interface
{
Sum
(
data
[]
byte
)
(
*
Cid
,
error
)
GetCodec
()
uint64
WithCodec
(
uint64
)
Format
}
type
FormatV0
struct
{}
...
...
@@ -34,6 +36,21 @@ func PrefixToFormat(p Prefix) Format {
}
}
func
(
p
Prefix
)
GetCodec
()
uint64
{
return
p
.
Codec
}
func
(
p
Prefix
)
WithCodec
(
c
uint64
)
Format
{
if
c
==
p
.
Codec
{
return
p
}
p
.
Codec
=
c
if
c
!=
DagProtobuf
{
p
.
Version
=
1
}
return
p
}
func
(
p
FormatV0
)
Sum
(
data
[]
byte
)
(
*
Cid
,
error
)
{
hash
,
err
:=
mh
.
Sum
(
data
,
mh
.
SHA2_256
,
-
1
)
if
err
!=
nil
{
...
...
@@ -42,6 +59,17 @@ func (p FormatV0) Sum(data []byte) (*Cid, error) {
return
NewCidV0
(
hash
),
nil
}
func
(
p
FormatV0
)
GetCodec
()
uint64
{
return
DagProtobuf
}
func
(
p
FormatV0
)
WithCodec
(
c
uint64
)
Format
{
if
c
==
DagProtobuf
{
return
p
}
return
FormatV1
{
Codec
:
c
,
HashFun
:
mh
.
SHA2_256
}
}
func
(
p
FormatV1
)
Sum
(
data
[]
byte
)
(
*
Cid
,
error
)
{
mhLen
:=
p
.
HashLen
if
mhLen
<=
0
{
...
...
@@ -53,3 +81,12 @@ func (p FormatV1) Sum(data []byte) (*Cid, error) {
}
return
NewCidV1
(
p
.
Codec
,
hash
),
nil
}
func
(
p
FormatV1
)
GetCodec
()
uint64
{
return
p
.
Codec
}
func
(
p
FormatV1
)
WithCodec
(
c
uint64
)
Format
{
p
.
Codec
=
c
return
p
}
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