Added an xml decoder, Fixes #1612
License: MIT Signed-off-by: Forrest Weston <forrest.weston@gmail.com>
Forrest Weston committed
Oct 7, 2015 at 10:45 UTC
6e2435211fa0dc20b40eaf47618512667cf59af1
4 files changed
+78
-15
core/commands/object.go
+25
-1
@@ -3,6 +3,7 @@ package commands
3
import (
4
"bytes"
5
"encoding/json"
6
+ "encoding/xml"
7
"errors"
8
"fmt"
9
"io"
@@ -689,7 +690,7 @@ func objectPut(n *core.IpfsNode, input io.Reader, encoding string) (*Object, err
690
691
// check that we have data in the Node to add
692
// otherwise we will add the empty object without raising an error
692
- if node.Data == "" && len(node.Links) == 0 {
693
+ if NodeEmpty(node) {
694
return nil, ErrEmptyNode
695
}
696
@@ -701,6 +702,24 @@ func objectPut(n *core.IpfsNode, input io.Reader, encoding string) (*Object, err
702
case objectEncodingProtobuf:
703
dagnode, err = dag.Decoded(data)
704
705
+ case objectEncodingXML:
706
+ node := new(Node)
707
+ err = xml.Unmarshal(data, node)
708
+ if err != nil {
709
+ return nil, err
710
+ }
711
+
712
+ // check that we have data in the Node to add
713
+ // otherwise we will add the empty object without raising an error
714
+ if NodeEmpty(node) {
715
+ return nil, ErrEmptyNode
716
+ }
717
+
718
+ dagnode, err = deserializeNode(node)
719
+ if err != nil {
720
+ return nil, err
721
+ }
722
+
723
default:
724
return nil, ErrUnknownObjectEnc
725
}
@@ -725,6 +744,7 @@ type objectEncoding string
744
const (
745
objectEncodingJSON objectEncoding = "json"
746
objectEncodingProtobuf = "protobuf"
747
+ objectEncodingXML = "xml"
748
)
749
750
func getObjectEnc(o interface{}) objectEncoding {
@@ -779,3 +799,7 @@ func deserializeNode(node *Node) (*dag.Node, error) {
799
800
return dagnode, nil
801
}
802
+
803
+func NodeEmpty(node *Node) bool {
804
+ return (node.Data == "" && len(node.Links) == 0)
805
+}
test/sharness/t0051-object-data/brokenPut.xml
new
+1
@@ -0,0 +1 @@
1
+<Noodles><Spaghetti>This is not a valid dag object fail</Spaghetti></Noodles>
test/sharness/t0051-object-data/testPut.xml
new
+1
@@ -0,0 +1 @@
1
+<Node><Data>Test xml for sharness test</Data></Node>
test/sharness/t0051-object.sh
+51
-14
@@ -32,25 +32,25 @@ test_object_cmd() {
32
printf "Hello Mars" >expected_in &&
33
ipfs add expected_in >actual_Addout
34
'
35
-
35
+
36
test_expect_success "'ipfs add testData' output looks good" '
37
HASH="QmWkHFpYBZ9mpPRreRbMhhYWXfUhBAue3JkbbpFqwowSRb" &&
38
echo "added $HASH expected_in" >expected_Addout &&
39
test_cmp expected_Addout actual_Addout
40
'
41
-
41
+
42
test_expect_success "'ipfs object get' succeeds" '
43
ipfs object get $HASH >actual_getOut
44
'
45
-
45
+
46
test_expect_success "'ipfs object get' output looks good" '
47
test_cmp ../t0051-object-data/expected_getOut actual_getOut
48
'
49
-
49
+
50
test_expect_success "'ipfs object stat' succeeds" '
51
ipfs object stat $HASH >actual_stat
52
'
53
-
53
+
54
test_expect_success "'ipfs object get' output looks good" '
55
echo "NumLinks: 0" > expected_stat &&
56
echo "BlockSize: 18" >> expected_stat &&
@@ -63,47 +63,84 @@ test_object_cmd() {
63
test_expect_success "'ipfs object put file.json' succeeds" '
64
ipfs object put ../t0051-object-data/testPut.json > actual_putOut
65
'
66
-
66
+
67
test_expect_success "'ipfs object put file.json' output looks good" '
68
HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" &&
69
printf "added $HASH" > expected_putOut &&
70
test_cmp expected_putOut actual_putOut
71
'
72
-
72
+
73
+ test_expect_success "'ipfs object put file.xml' succeeds" '
74
+ ipfs object put ../t0051-object-data/testPut.xml --inputenc=xml > actual_putOut
75
+ '
76
+
77
+ test_expect_success "'ipfs object put file.xml' output looks good" '
78
+ HASH="QmQzNKUHy4HyEUGkqKe3q3t796ffPLQXYCkHCcXUNT5JNK" &&
79
+ printf "added $HASH" > expected_putOut &&
80
+ test_cmp expected_putOut actual_putOut
81
+ '
82
+
83
+ test_expect_success "'ipfs object put' from stdin succeeds" '
84
+ cat ../t0051-object-data/testPut.xml | ipfs object put --inputenc=xml > actual_putStdinOut
85
+ '
86
+
87
+ test_expect_success "'ipfs object put broken.xml' should fail" '
88
+ test_expect_code 1 ipfs object put ../t0051-object-data/brokenPut.xml --inputenc=xml 2>actual_putBrokenErr >actual_putBroken
89
+ '
90
+
91
+ test_expect_success "'ipfs object put broken.hxml' output looks good" '
92
+ touch expected_putBroken &&
93
+ printf "Error: no data or links in this node\n" > expected_putBrokenErr &&
94
+ test_cmp expected_putBroken actual_putBroken &&
95
+ test_cmp expected_putBrokenErr actual_putBrokenErr
96
+ '
97
+ test_expect_success "'ipfs object get --enc=xml' succeeds" '
98
+ ipfs object get --enc=xml $HASH >utf8_xml
99
+ '
100
+
101
+ test_expect_success "'ipfs object put --inputenc=xml' succeeds" '
102
+ ipfs object put --inputenc=xml <utf8_xml >actual
103
+ '
104
+
105
+ test_expect_failure "'ipfs object put --inputenc=xml' output looks good" '
106
+ echo "added $HASH" >expected &&
107
+ test_cmp expected actual
108
+ '
109
+
110
test_expect_success "'ipfs object put file.pb' succeeds" '
111
ipfs object put --inputenc=protobuf ../t0051-object-data/testPut.pb > actual_putOut
112
'
76
-
113
+
114
test_expect_success "'ipfs object put file.pb' output looks good" '
115
HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" &&
116
printf "added $HASH" > expected_putOut &&
117
test_cmp expected_putOut actual_putOut
118
'
82
-
119
+
120
test_expect_success "'ipfs object put' from stdin succeeds" '
121
cat ../t0051-object-data/testPut.json | ipfs object put > actual_putStdinOut
122
'
86
-
123
+
124
test_expect_success "'ipfs object put' from stdin output looks good" '
125
HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" &&
126
printf "added $HASH" > expected_putStdinOut &&
127
test_cmp expected_putStdinOut actual_putStdinOut
128
'
92
-
129
+
130
test_expect_success "'ipfs object put' from stdin (pb) succeeds" '
131
cat ../t0051-object-data/testPut.pb | ipfs object put --inputenc=protobuf > actual_putPbStdinOut
132
'
96
-
133
+
134
test_expect_success "'ipfs object put' from stdin (pb) output looks good" '
135
HASH="QmUTSAdDi2xsNkDtLqjFgQDMEn5di3Ab9eqbrt4gaiNbUD" &&
136
printf "added $HASH" > expected_putStdinOut &&
137
test_cmp expected_putStdinOut actual_putPbStdinOut
138
'
102
-
139
+
140
test_expect_success "'ipfs object put broken.json' should fail" '
141
test_expect_code 1 ipfs object put ../t0051-object-data/brokenPut.json 2>actual_putBrokenErr >actual_putBroken
142
'
106
-
143
+
144
test_expect_success "'ipfs object put broken.hjson' output looks good" '
145
touch expected_putBroken &&
146
printf "Error: no data or links in this node\n" > expected_putBrokenErr &&