-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Changes needed for --cid-base
option in go-ipfs.
#8
Closed
Closed
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2cad3c2
Add '--filter' option to cid-fmt utility.
kevina 345f0c7
Move TryOtherCidVersion from go-ipfs/thirdparty/cidv0v1.
kevina 94ff1f0
Add apicid and cidenc packages.
kevina cec6117
Add WithOverride to cidenc package.
kevina 851475d
Add support for storing an encoder in the current context.
kevina 1917e04
Doc. Enhancements.
kevina 9148d7f
Use (Un)MarshalText instead of (Un)MarshalJSON for encoding.
kevina 329c831
gx publish 0.1.3
kevina 07d0223
Remove code that depends on `go-path`.
kevina bfec359
gx publish 0.1.4
kevina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.1.2: QmbfKu17LbMWyGUxHEUns9Wf5Dkm8PT6be4uPhTkk4YvaV | ||
0.1.3: QmVjZoEZg2oxXGFGjbD28x3gGN6ALHAW6BN2LKRUcaJ21i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package apicid | ||
|
||
import ( | ||
cid "github.com/ipfs/go-cid" | ||
"github.com/ipfs/go-cidutil/cidenc" | ||
mbase "github.com/multiformats/go-multibase" | ||
) | ||
|
||
// JSONBase is the base to use when Encoding into JSON. | ||
var JSONBase mbase.Encoder = mbase.MustNewEncoder(mbase.Base58BTC) | ||
|
||
// apicid.Hash is a type to respesnt a CID in the API which marshals | ||
// as a string | ||
type Hash struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just |
||
str string | ||
} | ||
|
||
// FromCid creates an APICid from a Cid | ||
func FromCid(c cid.Cid) Hash { | ||
return Hash{c.Encode(JSONBase)} | ||
} | ||
|
||
// Cid converts an APICid to a CID | ||
func (c Hash) Cid() (cid.Cid, error) { | ||
return cid.Decode(c.str) | ||
} | ||
|
||
func (c Hash) String() string { | ||
return c.Encode(cidenc.Default) | ||
} | ||
|
||
func (c Hash) Encode(enc cidenc.Interface) string { | ||
if c.str == "" { | ||
return "" | ||
} | ||
str, err := enc.Recode(c.str) | ||
if err != nil { | ||
return c.str | ||
} | ||
return str | ||
} | ||
|
||
func (c *Hash) UnmarshalText(b []byte) error { | ||
c.str = string(b) | ||
return nil | ||
} | ||
|
||
func (c Hash) MarshalText() ([]byte, error) { | ||
return []byte(c.str), nil | ||
} | ||
|
||
// Cid is type to represent a normal CID in the API which marshals | ||
// like a normal CID i.e. ({"/": <HASH>}) but may uses cidenc.Default | ||
// for the String() to optionally upgrade a version 0 CID to version 1 | ||
type Cid struct { | ||
cid.Cid | ||
} | ||
|
||
func (c Cid) String() string { | ||
return cidenc.Default.Encode(c.Cid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package apicid | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
cid "github.com/ipfs/go-cid" | ||
) | ||
|
||
func TestJson(t *testing.T) { | ||
cid, _ := cid.Decode("zb2rhak9iRgDiik36KQBRr2qiCJHdyBH7YxFmw7FTdM6zo31m") | ||
hash := FromCid(cid) | ||
data, err := json.Marshal(hash) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(data) != `"zb2rhak9iRgDiik36KQBRr2qiCJHdyBH7YxFmw7FTdM6zo31m"` { | ||
t.Fatalf("json string incorrect: %s\n", data) | ||
} | ||
var hash2 Hash | ||
err = json.Unmarshal(data, &hash2) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if hash != hash2 { | ||
t.Fatal("round trip failed") | ||
} | ||
} | ||
|
||
func TestJsonMap(t *testing.T) { | ||
cid1, _ := cid.Decode("zb2rhak9iRgDiik36KQBRr2qiCJHdyBH7YxFmw7FTdM6zo31m") | ||
cid2, _ := cid.Decode("QmRJggJREPCt7waGQKMXymrXRvrvsSiiPjgFbLK9isuM8K") | ||
hash1 := FromCid(cid1) | ||
hash2 := FromCid(cid2) | ||
m := map[Hash]string{hash1: "a value", hash2: "something else"} | ||
data, err := json.Marshal(m) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
m2 := map[Hash]string{} | ||
err = json.Unmarshal(data, &m2) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if len(m2) != 2 || m[hash1] != m2[hash1] || m[hash2] != m2[hash2] { | ||
t.Fatal("round trip failed") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
api
sounds a bit weird here, I'd probably just move this intocidenc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may sound weird but that is the exact idea I want to capture. This package present a form of the Cid to use in the API struct. The only reason it is here is because this type might need to be used outside of
go-ipfs
.