Skip to content
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

feat: Allow specifing how object data is encoded #5139

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions core/commands/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,23 @@ This command outputs data in the following encodings:
* "protobuf"
* "json"
* "xml"
(Specified by the "--encoding" or "--enc" flag)`,
(Specified by the "--encoding" or "--enc" flag)

The encoding of the object's data field can be specifed by using the
--data-encoding flag

Supported values are:
* "text" (default)
* "base64"
`,
},

Arguments: []cmdkit.Argument{
cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.StringOption("data-encoding", "Encoding type of the data field, either \"text\" or \"base64\".").WithDefault("text"),
},
Run: func(req oldcmds.Request, res oldcmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
Expand All @@ -219,6 +230,12 @@ This command outputs data in the following encodings:

fpath := path.Path(req.Arguments()[0])

datafieldenc, _, err := req.Option("data-encoding").String()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

object, err := core.Resolve(req.Context(), n.Namesys, n.Resolver, fpath)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
Expand All @@ -231,9 +248,15 @@ This command outputs data in the following encodings:
return
}

data, err := encodeData(pbo.Data(), datafieldenc)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

node := &Node{
Links: make([]Link, len(object.Links())),
Data: string(pbo.Data()),
Data: data,
}

for i, link := range object.Links() {
Expand Down Expand Up @@ -702,3 +725,14 @@ func unwrapOutput(i interface{}) (interface{}, error) {

return <-ch, nil
}

func encodeData(data []byte, encoding string) (string, error) {
switch encoding {
case "text":
return string(data), nil
case "base64":
return base64.StdEncoding.EncodeToString(data), nil
}

return "", fmt.Errorf("unkown data field encoding")
}
17 changes: 17 additions & 0 deletions test/sharness/t0051-object.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ test_object_cmd() {
test_cmp ../t0051-object-data/expected_getOut actual_getOut
'

test_expect_success "'ipfs object get' can specify data encoding as base64" '
ipfs object get --data-encoding base64 $HASH > obj_out &&
echo "{\"Links\":[],\"Data\":\"CAISCkhlbGxvIE1hcnMYCg==\"}" > obj_exp &&
test_cmp obj_out obj_exp
'

test_expect_success "'ipfs object get' can specify data encoding as text" '
echo "{\"Links\":[],\"Data\":\"Hello Mars\"}" | ipfs object put &&
ipfs object get --data-encoding text QmS3hVY6eYrMQ6L22agwrx3YHBEsc3LJxVXCtyQHqRBukH > obj_out &&
echo "{\"Links\":[],\"Data\":\"Hello Mars\"}" > obj_exp &&
test_cmp obj_out obj_exp
'

test_expect_failure "'ipfs object get' requires known data encoding" '
ipfs object get --data-encoding nonsensical-encoding $HASH
'

test_expect_success "'ipfs object stat' succeeds" '
ipfs object stat $HASH >actual_stat
'
Expand Down