hana 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.rdoc +4 -0
- data/lib/hana.rb +23 -4
- data/test/json-patch-tests/spec_tests.json +50 -21
- data/test/json-patch-tests/tests.json +26 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b259b5422ec5303610246f9c0359dddab090f41d
|
4
|
+
data.tar.gz: 0e05b0271035942c551969550f2d6fc549d6d9c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4236a94452c5aa0672009813ab7082248fcd7a0be36bb23c372321da44134ff193f5beb6864a48383739e28eb430c8dd46d6533cbdbb89a0b10d8d5e65f78690
|
7
|
+
data.tar.gz: 330794b059b22dc3d2a088ff95eba30445f36fa8c08f13f2f959b92c4b74fab7c037385160b0b67b9b2c7f4413f83e3b487596525f03e09e8739be0ba211e77c
|
data/CHANGELOG.rdoc
CHANGED
data/lib/hana.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module Hana
|
2
|
-
VERSION = '1.
|
2
|
+
VERSION = '1.2.0'
|
3
3
|
|
4
4
|
class Pointer
|
5
5
|
include Enumerable
|
@@ -18,7 +18,13 @@ module Hana
|
|
18
18
|
ESC = {'^/' => '/', '^^' => '^', '~0' => '~', '~1' => '/'} # :nodoc:
|
19
19
|
|
20
20
|
def self.eval list, object
|
21
|
-
list.inject(object) { |o, part|
|
21
|
+
list.inject(object) { |o, part|
|
22
|
+
if Array === o
|
23
|
+
raise Patch::IndexException unless part =~ /\A\d+\Z/
|
24
|
+
part = part.to_i
|
25
|
+
end
|
26
|
+
o[part]
|
27
|
+
}
|
22
28
|
end
|
23
29
|
|
24
30
|
def self.parse path
|
@@ -50,6 +56,9 @@ module Hana
|
|
50
56
|
class ObjectOperationOnArrayException < Exception
|
51
57
|
end
|
52
58
|
|
59
|
+
class IndexException < Exception
|
60
|
+
end
|
61
|
+
|
53
62
|
def initialize is
|
54
63
|
@is = is
|
55
64
|
end
|
@@ -72,7 +81,11 @@ module Hana
|
|
72
81
|
dest = Pointer.eval list, doc
|
73
82
|
obj = ins['value']
|
74
83
|
|
75
|
-
|
84
|
+
if key
|
85
|
+
add_op dest, key, obj
|
86
|
+
else
|
87
|
+
dest.replace obj
|
88
|
+
end
|
76
89
|
end
|
77
90
|
|
78
91
|
def move ins, doc
|
@@ -105,7 +118,11 @@ module Hana
|
|
105
118
|
end
|
106
119
|
|
107
120
|
def test ins, doc
|
108
|
-
|
121
|
+
begin
|
122
|
+
expected = Pointer.new(ins['path']).eval doc
|
123
|
+
rescue Patch::IndexException
|
124
|
+
raise FailedTestException.new(ins['value'], ins['path'])
|
125
|
+
end
|
109
126
|
|
110
127
|
unless expected == ins['value']
|
111
128
|
raise FailedTestException.new(ins['value'], ins['path'])
|
@@ -132,6 +149,8 @@ module Hana
|
|
132
149
|
end
|
133
150
|
|
134
151
|
def check_index obj, key
|
152
|
+
return -1 if key == '-'
|
153
|
+
|
135
154
|
raise ObjectOperationOnArrayException unless key =~ /\A-?\d+\Z/
|
136
155
|
idx = key.to_i
|
137
156
|
raise OutOfBoundsException if idx > obj.length || idx < 0
|
@@ -1,4 +1,12 @@
|
|
1
1
|
[
|
2
|
+
{
|
3
|
+
"comment": "4.1. add with missing object",
|
4
|
+
"doc": { "q": { "bar": 2 } },
|
5
|
+
"patch": [ {"op": "add", "path": "/a/b", "value": 1} ],
|
6
|
+
"error":
|
7
|
+
"path /a does not exist -- missing objects are not created recursively"
|
8
|
+
},
|
9
|
+
|
2
10
|
{
|
3
11
|
"comment": "A.1. Adding an Object Member",
|
4
12
|
"doc": {
|
@@ -14,7 +22,7 @@
|
|
14
22
|
},
|
15
23
|
|
16
24
|
{
|
17
|
-
"comment": "A.2. Adding an Array Element",
|
25
|
+
"comment": "A.2. Adding an Array Element",
|
18
26
|
"doc": {
|
19
27
|
"foo": [ "bar", "baz" ]
|
20
28
|
},
|
@@ -58,7 +66,7 @@
|
|
58
66
|
"doc": {
|
59
67
|
"baz": "qux",
|
60
68
|
"foo": "bar"
|
61
|
-
},
|
69
|
+
},
|
62
70
|
"patch": [
|
63
71
|
{ "op": "replace", "path": "/baz", "value": "boo" }
|
64
72
|
],
|
@@ -78,10 +86,10 @@
|
|
78
86
|
"qux": {
|
79
87
|
"corge": "grault"
|
80
88
|
}
|
81
|
-
},
|
89
|
+
},
|
82
90
|
"patch": [
|
83
91
|
{ "op": "move", "from": "/foo/waldo", "path": "/qux/thud" }
|
84
|
-
],
|
92
|
+
],
|
85
93
|
"expected": {
|
86
94
|
"foo": {
|
87
95
|
"bar": "baz"
|
@@ -97,10 +105,10 @@
|
|
97
105
|
"comment": "A.7. Moving an Array Element",
|
98
106
|
"doc": {
|
99
107
|
"foo": [ "all", "grass", "cows", "eat" ]
|
100
|
-
},
|
108
|
+
},
|
101
109
|
"patch": [
|
102
110
|
{ "op": "move", "from": "/foo/1", "path": "/foo/3" }
|
103
|
-
],
|
111
|
+
],
|
104
112
|
"expected": {
|
105
113
|
"foo": [ "all", "cows", "eat", "grass" ]
|
106
114
|
}
|
@@ -131,7 +139,7 @@
|
|
131
139
|
"patch": [
|
132
140
|
{ "op": "test", "path": "/baz", "value": "bar" }
|
133
141
|
],
|
134
|
-
"error": "string not equivalent"
|
142
|
+
"error": "string not equivalent"
|
135
143
|
},
|
136
144
|
|
137
145
|
{
|
@@ -150,21 +158,21 @@
|
|
150
158
|
}
|
151
159
|
}
|
152
160
|
},
|
153
|
-
|
161
|
+
|
154
162
|
{
|
155
163
|
"comment": "A.11. Ignoring Unrecognized Elements",
|
156
164
|
"doc": {
|
157
165
|
"foo":"bar"
|
158
|
-
},
|
166
|
+
},
|
159
167
|
"patch": [
|
160
168
|
{ "op": "add", "path": "/baz", "value": "qux", "xyz": 123 }
|
161
169
|
],
|
162
170
|
"expected": {
|
163
|
-
"foo":"bar",
|
171
|
+
"foo":"bar",
|
164
172
|
"baz":"qux"
|
165
173
|
}
|
166
174
|
},
|
167
|
-
|
175
|
+
|
168
176
|
{
|
169
177
|
"comment": "A.12. Adding to a Non-existant Target",
|
170
178
|
"doc": {
|
@@ -175,9 +183,9 @@
|
|
175
183
|
],
|
176
184
|
"error": "add to a non-existant target"
|
177
185
|
},
|
178
|
-
|
186
|
+
|
179
187
|
{
|
180
|
-
"comment": "Invalid JSON Patch Document",
|
188
|
+
"comment": "A.13 Invalid JSON Patch Document",
|
181
189
|
"doc": {
|
182
190
|
"foo": "bar"
|
183
191
|
},
|
@@ -186,18 +194,39 @@
|
|
186
194
|
],
|
187
195
|
"error": "operation has two 'op' members"
|
188
196
|
},
|
189
|
-
|
190
|
-
{
|
191
|
-
"comment": "~ Escape Ordering",
|
197
|
+
|
198
|
+
{
|
199
|
+
"comment": "A.14. ~ Escape Ordering",
|
192
200
|
"doc": {
|
193
201
|
"/": 9,
|
194
|
-
"~1": 10
|
202
|
+
"~1": 10
|
195
203
|
},
|
196
|
-
"patch": [{"op": "test", "path": "/~01", "value":
|
204
|
+
"patch": [{"op": "test", "path": "/~01", "value": 10}],
|
197
205
|
"expected": {
|
198
206
|
"/": 9,
|
199
|
-
"~1": 10
|
207
|
+
"~1": 10
|
208
|
+
}
|
209
|
+
},
|
210
|
+
|
211
|
+
{
|
212
|
+
"comment": "A.15. Comparing Strings and Numbers",
|
213
|
+
"doc": {
|
214
|
+
"/": 9,
|
215
|
+
"~1": 10
|
216
|
+
},
|
217
|
+
"patch": [{"op": "test", "path": "/~01", "value": "10"}],
|
218
|
+
"error": "number is not equal to string"
|
219
|
+
},
|
220
|
+
|
221
|
+
{
|
222
|
+
"comment": "A.16. Adding an Array Value",
|
223
|
+
"doc": {
|
224
|
+
"foo": ["bar"]
|
225
|
+
},
|
226
|
+
"patch": [{ "op": "add", "path": "/foo/-", "value": ["abc", "def"] }],
|
227
|
+
"expected": {
|
228
|
+
"foo": ["bar", ["abc", "def"]]
|
200
229
|
}
|
201
230
|
}
|
202
|
-
|
203
|
-
]
|
231
|
+
|
232
|
+
]
|
@@ -108,9 +108,19 @@
|
|
108
108
|
"expected": ["bar", "foo", "sil"] },
|
109
109
|
|
110
110
|
{ "doc": ["foo", "sil"],
|
111
|
-
"patch": [{"op":"
|
111
|
+
"patch": [{"op":"add", "path": "/2", "value": "bar"}],
|
112
112
|
"expected": ["foo", "sil", "bar"] },
|
113
113
|
|
114
|
+
{ "comment": "test against implementation-specific numeric parsing",
|
115
|
+
"doc": {"1e0": "foo"},
|
116
|
+
"patch": [{"op": "test", "path": "/1e0", "value": "foo"}],
|
117
|
+
"expected": {"1e0": "foo"} },
|
118
|
+
|
119
|
+
{ "comment": "test with bad number should fail",
|
120
|
+
"doc": ["foo", "bar"],
|
121
|
+
"patch": [{"op": "test", "path": "/1e0", "value": "bar"}],
|
122
|
+
"error": "test op shouldn't get array element 1" },
|
123
|
+
|
114
124
|
{ "doc": ["foo", "sil"],
|
115
125
|
"patch": [{"op": "add", "path": "/bar", "value": 42}],
|
116
126
|
"error": "Object operation on array target" },
|
@@ -238,5 +248,20 @@
|
|
238
248
|
"patch": [{"op": "copy", "from": "/baz/0", "path": "/boo"}],
|
239
249
|
"expected": {"baz":[{"qux":"hello"}],"bar":1,"boo":{"qux":"hello"}} },
|
240
250
|
|
251
|
+
{ "comment": "replacing the root of the document is possible with add",
|
252
|
+
"doc": {"foo": "bar"},
|
253
|
+
"patch": [{"op": "add", "path": "", "value": {"baz": "qux"}}],
|
254
|
+
"expected": {"baz":"qux"}},
|
255
|
+
|
256
|
+
{ "comment": "Adding to \"/-\" adds to the end of the array",
|
257
|
+
"doc": [ 1, 2 ],
|
258
|
+
"patch": [ { "op": "add", "path": "/-", "value": { "foo": [ "bar", "baz" ] } } ],
|
259
|
+
"expected": [ 1, 2, { "foo": [ "bar", "baz" ] } ]},
|
260
|
+
|
261
|
+
{ "comment": "Adding to \"/-\" adds to the end of the array, even n levels down",
|
262
|
+
"doc": [ 1, 2, [ 3, [ 4, 5 ] ] ],
|
263
|
+
"patch": [ { "op": "add", "path": "/2/1/-", "value": { "foo": [ "bar", "baz" ] } } ],
|
264
|
+
"expected": [ 1, 2, [ 3, [ 4, 5, { "foo": [ "bar", "baz" ] } ] ] ]},
|
265
|
+
|
241
266
|
{ "comment": "tests complete" }
|
242
267
|
]
|