mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-19 18:05:32 +08:00
Added an xml decoder, Fixes #1612
License: MIT Signed-off-by: Forrest Weston <forrest.weston@gmail.com>
This commit is contained in:

committed by
Forrest Weston

parent
b415e0618b
commit
6e2435211f
@ -3,6 +3,7 @@ package commands
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -689,7 +690,7 @@ func objectPut(n *core.IpfsNode, input io.Reader, encoding string) (*Object, err
|
||||
|
||||
// check that we have data in the Node to add
|
||||
// otherwise we will add the empty object without raising an error
|
||||
if node.Data == "" && len(node.Links) == 0 {
|
||||
if NodeEmpty(node) {
|
||||
return nil, ErrEmptyNode
|
||||
}
|
||||
|
||||
@ -701,6 +702,24 @@ func objectPut(n *core.IpfsNode, input io.Reader, encoding string) (*Object, err
|
||||
case objectEncodingProtobuf:
|
||||
dagnode, err = dag.Decoded(data)
|
||||
|
||||
case objectEncodingXML:
|
||||
node := new(Node)
|
||||
err = xml.Unmarshal(data, node)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// check that we have data in the Node to add
|
||||
// otherwise we will add the empty object without raising an error
|
||||
if NodeEmpty(node) {
|
||||
return nil, ErrEmptyNode
|
||||
}
|
||||
|
||||
dagnode, err = deserializeNode(node)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
default:
|
||||
return nil, ErrUnknownObjectEnc
|
||||
}
|
||||
@ -725,6 +744,7 @@ type objectEncoding string
|
||||
const (
|
||||
objectEncodingJSON objectEncoding = "json"
|
||||
objectEncodingProtobuf = "protobuf"
|
||||
objectEncodingXML = "xml"
|
||||
)
|
||||
|
||||
func getObjectEnc(o interface{}) objectEncoding {
|
||||
@ -779,3 +799,7 @@ func deserializeNode(node *Node) (*dag.Node, error) {
|
||||
|
||||
return dagnode, nil
|
||||
}
|
||||
|
||||
func NodeEmpty(node *Node) bool {
|
||||
return (node.Data == "" && len(node.Links) == 0)
|
||||
}
|
||||
|
1
test/sharness/t0051-object-data/brokenPut.xml
Normal file
1
test/sharness/t0051-object-data/brokenPut.xml
Normal file
@ -0,0 +1 @@
|
||||
<Noodles><Spaghetti>This is not a valid dag object fail</Spaghetti></Noodles>
|
1
test/sharness/t0051-object-data/testPut.xml
Normal file
1
test/sharness/t0051-object-data/testPut.xml
Normal file
@ -0,0 +1 @@
|
||||
<Node><Data>Test xml for sharness test</Data></Node>
|
@ -70,6 +70,43 @@ test_object_cmd() {
|
||||
test_cmp expected_putOut actual_putOut
|
||||
'
|
||||
|
||||
test_expect_success "'ipfs object put file.xml' succeeds" '
|
||||
ipfs object put ../t0051-object-data/testPut.xml --inputenc=xml > actual_putOut
|
||||
'
|
||||
|
||||
test_expect_success "'ipfs object put file.xml' output looks good" '
|
||||
HASH="QmQzNKUHy4HyEUGkqKe3q3t796ffPLQXYCkHCcXUNT5JNK" &&
|
||||
printf "added $HASH" > expected_putOut &&
|
||||
test_cmp expected_putOut actual_putOut
|
||||
'
|
||||
|
||||
test_expect_success "'ipfs object put' from stdin succeeds" '
|
||||
cat ../t0051-object-data/testPut.xml | ipfs object put --inputenc=xml > actual_putStdinOut
|
||||
'
|
||||
|
||||
test_expect_success "'ipfs object put broken.xml' should fail" '
|
||||
test_expect_code 1 ipfs object put ../t0051-object-data/brokenPut.xml --inputenc=xml 2>actual_putBrokenErr >actual_putBroken
|
||||
'
|
||||
|
||||
test_expect_success "'ipfs object put broken.hxml' output looks good" '
|
||||
touch expected_putBroken &&
|
||||
printf "Error: no data or links in this node\n" > expected_putBrokenErr &&
|
||||
test_cmp expected_putBroken actual_putBroken &&
|
||||
test_cmp expected_putBrokenErr actual_putBrokenErr
|
||||
'
|
||||
test_expect_success "'ipfs object get --enc=xml' succeeds" '
|
||||
ipfs object get --enc=xml $HASH >utf8_xml
|
||||
'
|
||||
|
||||
test_expect_success "'ipfs object put --inputenc=xml' succeeds" '
|
||||
ipfs object put --inputenc=xml <utf8_xml >actual
|
||||
'
|
||||
|
||||
test_expect_failure "'ipfs object put --inputenc=xml' output looks good" '
|
||||
echo "added $HASH" >expected &&
|
||||
test_cmp expected actual
|
||||
'
|
||||
|
||||
test_expect_success "'ipfs object put file.pb' succeeds" '
|
||||
ipfs object put --inputenc=protobuf ../t0051-object-data/testPut.pb > actual_putOut
|
||||
'
|
||||
|
Reference in New Issue
Block a user