-
Notifications
You must be signed in to change notification settings - Fork 6
WIP: Changes needed for --cid-base
option in go-ipfs.
#8
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
Closed
Closed
Changes from all 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 hidden or 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.4: QmckgkstbdXagMTQ4e1DW2SzxGcjjudbqEvA5H2Rb7uvAT |
This file contains hidden or 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 hidden or 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 hidden or 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
This file contains hidden or 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,85 @@ | ||
package cidenc | ||
|
||
import ( | ||
"context" | ||
|
||
cid "github.com/ipfs/go-cid" | ||
mbase "github.com/multiformats/go-multibase" | ||
) | ||
|
||
// Encoder is a basic Encoder that will encode Cid's using a specifed | ||
// base and optionally upgrade a CidV0 to CidV1 | ||
type Encoder struct { | ||
Base mbase.Encoder | ||
Upgrade bool | ||
} | ||
|
||
// Interface is a generic interface to the Encoder functionally. | ||
type Interface interface { | ||
Encode(c cid.Cid) string | ||
Recode(v string) (string, error) | ||
} | ||
|
||
// Default is the default encoder | ||
var Default = Encoder{ | ||
Base: mbase.MustNewEncoder(mbase.Base58BTC), | ||
Upgrade: false, | ||
} | ||
|
||
func (enc Encoder) Encode(c cid.Cid) string { | ||
if enc.Upgrade && c.Version() == 0 { | ||
c = cid.NewCidV1(c.Type(), c.Hash()) | ||
} | ||
return c.Encode(enc.Base) | ||
} | ||
|
||
// Recode reencodes the cid string to match the paramaters of the | ||
// encoder | ||
func (enc Encoder) Recode(v string) (string, error) { | ||
skip, err := enc.noopRecode(v) | ||
if skip || err != nil { | ||
return v, err | ||
} | ||
|
||
c, err := cid.Decode(v) | ||
if err != nil { | ||
return v, err | ||
} | ||
|
||
return enc.Encode(c), nil | ||
} | ||
|
||
func (enc Encoder) noopRecode(v string) (bool, error) { | ||
if len(v) < 2 { | ||
return false, cid.ErrCidTooShort | ||
} | ||
ver := cidVer(v) | ||
skip := ver == 0 && !enc.Upgrade || ver == 1 && v[0] == byte(enc.Base.Encoding()) | ||
return skip, nil | ||
} | ||
|
||
func cidVer(v string) int { | ||
if len(v) == 46 && v[:2] == "Qm" { | ||
return 0 | ||
} else { | ||
return 1 | ||
} | ||
} | ||
|
||
type encoderKey struct{} | ||
|
||
// Enable "enables" the encoder in the context using WithValue | ||
func Enable(ctx context.Context, enc Interface) context.Context { | ||
return context.WithValue(ctx, encoderKey{}, enc) | ||
} | ||
|
||
// Get gets an encoder from the context if it exists, otherwise the | ||
// default context is called. | ||
func Get(ctx context.Context) Interface { | ||
enc, ok := ctx.Value(encoderKey{}).(Interface) | ||
if !ok { | ||
// FIXME: Warning? | ||
enc = Default | ||
} | ||
return enc | ||
} |
This file contains hidden or 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,18 @@ | ||
package cidenc | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
mbase "github.com/multiformats/go-multibase" | ||
) | ||
|
||
func TestContext(t *testing.T) { | ||
enc := Encoder{Base: mbase.MustNewEncoder(mbase.Base64)} | ||
ctx := context.Background() | ||
ctx = Enable(ctx, enc) | ||
e, ok := Get(ctx).(Encoder) | ||
if !ok || e.Base.Encoding() != mbase.Base64 { | ||
t.Fatal("Failed to retrive encoder from context") | ||
} | ||
} |
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
.