-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Implement cbor ipld nodes and a first pass at the 'dag' command #3325
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6b797f1
Implement cbor ipld nodes and a first pass at the 'dag' command
whyrusleeping f7d3f61
clean up some code, update cbor package, and add tests
whyrusleeping 4c5a5c8
more cleanup, update cboripld package
whyrusleeping c6a273f
add more helptext to dag commands
whyrusleeping 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
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,216 @@ | ||
package dagcmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
cmds "github.com/ipfs/go-ipfs/commands" | ||
|
||
node "gx/ipfs/QmU7bFWQ793qmvNy7outdCaMfSDNk8uqhx4VNrxYj5fj5g/go-ipld-node" | ||
cid "gx/ipfs/QmXfiyr2RWEXpVDdaYnD2HNiBk6UBddsvEP4RPfXb6nGqY/go-cid" | ||
ipldcbor "gx/ipfs/QmYRzW9YDHVNCDbfFzbS7TEXAG1swE1yjq1basZ5WnJYH4/go-ipld-cbor" | ||
) | ||
|
||
var DagCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Interact with ipld dag objects.", | ||
ShortDescription: ``, | ||
}, | ||
|
||
Subcommands: map[string]*cmds.Command{ | ||
"put": DagPutCmd, | ||
"get": DagGetCmd, | ||
}, | ||
} | ||
|
||
type OutputObject struct { | ||
Cid *cid.Cid | ||
} | ||
|
||
var DagPutCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Add a dag node to ipfs.", | ||
}, | ||
Arguments: []cmds.Argument{ | ||
cmds.FileArg("object data", true, false, "The object to put").EnableStdin(), | ||
}, | ||
Options: []cmds.Option{ | ||
cmds.StringOption("format", "f", "Format that the object will be.").Default("cbor"), | ||
cmds.StringOption("input-enc", "Format that the object will be.").Default("json"), | ||
}, | ||
Run: func(req cmds.Request, res cmds.Response) { | ||
n, err := req.InvocContext().GetNode() | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
fi, err := req.Files().NextFile() | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
ienc, _, _ := req.Option("input-enc").String() | ||
format, _, _ := req.Option("format").String() | ||
_ = format | ||
switch ienc { | ||
case "json": | ||
var obj map[string]interface{} | ||
err := json.NewDecoder(fi).Decode(&obj) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
nd, err := convertJsonToType(obj, format) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
c, err := n.DAG.Add(nd) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
res.SetOutput(&OutputObject{Cid: c}) | ||
return | ||
/* | ||
case "btc": | ||
data, err := ioutil.ReadAll(fi) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
blk, err := ipldbtc.DecodeBlock(data) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
c, err := n.DAG.Add(blk) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
res.SetOutput(&OutputObject{Cid: c}) | ||
return | ||
*/ | ||
} | ||
}, | ||
Type: OutputObject{}, | ||
Marshalers: cmds.MarshalerMap{ | ||
cmds.Text: func(res cmds.Response) (io.Reader, error) { | ||
oobj, ok := res.Output().(*OutputObject) | ||
if !ok { | ||
return nil, fmt.Errorf("expected a different object in marshaler") | ||
} | ||
|
||
return strings.NewReader(oobj.Cid.String()), nil | ||
}, | ||
}, | ||
} | ||
|
||
var DagGetCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Get a dag node from ipfs.", | ||
}, | ||
Arguments: []cmds.Argument{ | ||
cmds.StringArg("cid", true, false, "The cid of the object to get").EnableStdin(), | ||
}, | ||
Run: func(req cmds.Request, res cmds.Response) { | ||
n, err := req.InvocContext().GetNode() | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
c, err := cid.Decode(req.Arguments()[0]) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
obj, err := n.DAG.Get(req.Context(), c) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
res.SetOutput(obj) | ||
}, | ||
} | ||
|
||
func convertJsonToType(obj map[string]interface{}, format string) (node.Node, error) { | ||
switch format { | ||
case "cbor", "dag-cbor": | ||
return convertJsonToCbor(obj) | ||
case "dag-pb", "protobuf": | ||
return nil, fmt.Errorf("protobuf handling in 'dag' command not yet implemented") | ||
default: | ||
return nil, fmt.Errorf("unknown target format: %s", format) | ||
} | ||
} | ||
|
||
func convertJsonToCbor(from map[string]interface{}) (*ipldcbor.Node, error) { | ||
out, err := convertMapSIToCbor(from) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ipldcbor.WrapMap(out) | ||
} | ||
func convertMapSIToCbor(from map[string]interface{}) (map[interface{}]interface{}, error) { | ||
to := make(map[interface{}]interface{}) | ||
for k, v := range from { | ||
out, err := convertToCborIshObj(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
to[k] = out | ||
} | ||
|
||
return to, nil | ||
} | ||
|
||
func convertToCborIshObj(i interface{}) (interface{}, error) { | ||
switch v := i.(type) { | ||
case map[string]interface{}: | ||
if lnk, ok := v["/"]; ok && len(v) == 1 { | ||
// special case for links | ||
vstr, ok := lnk.(string) | ||
if !ok { | ||
return nil, fmt.Errorf("link should have been a string") | ||
} | ||
|
||
c, err := cid.Decode(vstr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &ipldcbor.Link{Target: c}, nil | ||
} | ||
|
||
return convertMapSIToCbor(v) | ||
case []interface{}: | ||
var out []interface{} | ||
for _, o := range v { | ||
obj, err := convertToCborIshObj(o) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
out = append(out, obj) | ||
} | ||
|
||
return out, nil | ||
default: | ||
return v, nil | ||
} | ||
} |
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
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
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
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.
So "go-libp2p-routing" is no longer used?
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.
Oh, i guess it still is. But its imported indirectly via the go-libp2p-kad-dht, meaning that me accidentally removing this didnt break anything.