msfl 0.0.1.pre.rc7 → 0.0.1.pre.rc8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/msfl/parsers/json.rb +25 -1
- data/msfl.gemspec +1 -1
- data/spec/msfl/parsers/json_spec.rb +129 -0
- 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: f158def0dd3c1a90afd52ac221d85b95e68f940b
|
4
|
+
data.tar.gz: 218ac6a18e360fb3710f6528739001ab2454305f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1a4bdaec608a350ca1aa02861ab1978c8f534fa6a54575740e9bd898f2070271902835eef85454025df7e0139ad4ad0307ab65ee2c791ca2a988ed177debfc8
|
7
|
+
data.tar.gz: 4e6b90ab7c4dc28cdee6eb83dc106b18958e1df6a123e18cf8342b826b48ab2a9d342430b0cb7494083bb6684aa1f81df99589850e26dd215095f6c4ce28f142
|
data/lib/msfl/parsers/json.rb
CHANGED
@@ -12,6 +12,7 @@ module MSFL
|
|
12
12
|
json_to_parse = '{}' if json_to_parse.nil? || json_to_parse == "" || json_to_parse == "null"
|
13
13
|
obj = ::JSON.parse(json_to_parse)
|
14
14
|
obj = arrays_to_sets obj
|
15
|
+
obj = convert_keys_to_symbols obj
|
15
16
|
obj
|
16
17
|
end
|
17
18
|
|
@@ -24,7 +25,7 @@ module MSFL
|
|
24
25
|
if obj.respond_to? :each
|
25
26
|
if obj.is_a?(::Hash)
|
26
27
|
result = {}
|
27
|
-
obj.each { |key, val| result[key.to_sym] = arrays_to_sets val }
|
28
|
+
obj.each { |key, val| result["#{key}".to_sym] = arrays_to_sets val }
|
28
29
|
elsif obj.is_a?(Types::Set)
|
29
30
|
result = Types::Set.new obj.map { |value| arrays_to_sets value }
|
30
31
|
end
|
@@ -32,6 +33,29 @@ module MSFL
|
|
32
33
|
result ||= obj
|
33
34
|
result
|
34
35
|
end
|
36
|
+
|
37
|
+
# Deeply converts all hash keys to symbols
|
38
|
+
#
|
39
|
+
# @param obj [Object] the object on which to deeply convert hash keys to symbols
|
40
|
+
# @return [Object] the object with its hash keys deeply converted to symbols
|
41
|
+
def self.convert_keys_to_symbols(obj)
|
42
|
+
result = obj
|
43
|
+
if obj.is_a? Hash
|
44
|
+
result = Hash.new
|
45
|
+
obj.each do |k, v|
|
46
|
+
value = convert_keys_to_symbols v
|
47
|
+
result["#{k}".to_sym] = value
|
48
|
+
end
|
49
|
+
elsif obj.is_a? Types::Set
|
50
|
+
result = Types::Set.new
|
51
|
+
obj.each do |v|
|
52
|
+
result << convert_keys_to_symbols(v)
|
53
|
+
end
|
54
|
+
elsif obj.is_a? Array # Generally this will not be the case as the expectation is that arrays_to_sets has already run
|
55
|
+
result = convert_keys_to_symbols(arrays_to_sets(obj))
|
56
|
+
end
|
57
|
+
result
|
58
|
+
end
|
35
59
|
end
|
36
60
|
end
|
37
61
|
end
|
data/msfl.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'msfl'
|
3
|
-
s.version = '0.0.1-
|
3
|
+
s.version = '0.0.1-rc8'
|
4
4
|
s.date = '2015-03-26'
|
5
5
|
s.summary = "MSFL in Ruby"
|
6
6
|
s.description = "Serializers, validators, and other tasty goodness for the Mattermark Semantic Filter Language in Ruby."
|
@@ -154,4 +154,133 @@ describe "MSFL::Parsers::JSON" do
|
|
154
154
|
end
|
155
155
|
end
|
156
156
|
end
|
157
|
+
|
158
|
+
describe ".convert_keys_to_symbols" do
|
159
|
+
|
160
|
+
subject(:mut) { MSFL::Parsers::JSON.convert_keys_to_symbols arg }
|
161
|
+
|
162
|
+
let(:arg) { Object.new }
|
163
|
+
|
164
|
+
[55, "five", :fourty, nil].each do |item|
|
165
|
+
context "when the argument is a #{item.class}" do
|
166
|
+
|
167
|
+
let(:arg) { item }
|
168
|
+
|
169
|
+
it "is equal to the argument" do
|
170
|
+
expect(mut).to eq arg
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
context "when the argument is a Hash" do
|
176
|
+
|
177
|
+
let(:arg) { a_hash }
|
178
|
+
|
179
|
+
[44, "fooz", :bar].each do |key|
|
180
|
+
|
181
|
+
let(:a_hash) { { key => "some value" } }
|
182
|
+
|
183
|
+
let(:expected) { { "#{key}".to_sym => "some value" } }
|
184
|
+
|
185
|
+
it "coerces the keys to symbols" do
|
186
|
+
expect(mut).to eq expected
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
context "when the hash's values include at least one Hash" do
|
191
|
+
|
192
|
+
let(:a_hash) { { "cat" => { "sally" => "some value" }, 1337 => "peter", "bob" => 111 } }
|
193
|
+
|
194
|
+
let(:expected) { { cat: { sally: "some value" }, :"1337" => "peter", bob: 111 } }
|
195
|
+
|
196
|
+
it "recursively coerces the keys to symbols" do
|
197
|
+
expect(mut).to eq expected
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
context "when the hash's values include at least one Array" do
|
202
|
+
|
203
|
+
let(:a_hash) { { "inner_array" => array_in_arg, abc: 123, 123 => :abc } }
|
204
|
+
|
205
|
+
let(:expected) do
|
206
|
+
{ inner_array: MSFL::Types::Set.new([ MSFL::Types::Set.new([ 4, :b, { sad: "bear" } ]), "baz" ]),
|
207
|
+
abc: 123,
|
208
|
+
:"123" => :abc }
|
209
|
+
end
|
210
|
+
|
211
|
+
let(:array_in_arg) { [ [4, :b, { "sad" => "bear"} ], "baz"] }
|
212
|
+
|
213
|
+
it "recursively coerces hash keys to symbols" do
|
214
|
+
expect(mut).to eq expected
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
context "when the hash's values include at least on MSFL::Types::Set" do
|
219
|
+
|
220
|
+
let(:a_hash) { { "inner_set" => set_in_arg, abc: 123, 123 => :abc } }
|
221
|
+
|
222
|
+
let(:expected) do
|
223
|
+
{ inner_set: MSFL::Types::Set.new([ MSFL::Types::Set.new([ 4, :b, { sad: "bear" } ]), "baz" ]),
|
224
|
+
abc: 123,
|
225
|
+
:"123" => :abc }
|
226
|
+
end
|
227
|
+
|
228
|
+
let(:set_in_arg) { MSFL::Types::Set.new([ MSFL::Types::Set.new([4, :b, { "sad" => "bear"} ]), "baz"]) }
|
229
|
+
|
230
|
+
it "recursively coerces hash keys to symbols" do
|
231
|
+
expect(mut).to eq expected
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
context "when the argument is an Array" do
|
237
|
+
|
238
|
+
let(:arg) { an_array }
|
239
|
+
|
240
|
+
let(:an_array) { [:foo, "bar", nil, 99, "bar"] }
|
241
|
+
|
242
|
+
it "coerces Arrays into MSFL::Types::Set objects" do
|
243
|
+
expect(mut).to be_a MSFL::Types::Set
|
244
|
+
end
|
245
|
+
|
246
|
+
context "when the array's values are unique scalars" do
|
247
|
+
|
248
|
+
let(:an_array) { [:foo, "bar", nil, 99] }
|
249
|
+
|
250
|
+
it "is unchanged" do
|
251
|
+
expect(mut).to eq(MSFL::Types::Set.new an_array)
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
context "when the array includes at least one Hash" do
|
256
|
+
|
257
|
+
let(:an_array) { ["bar", hash_in_array, 84] }
|
258
|
+
|
259
|
+
let(:hash_in_array) { { "cat" => 1221, dog: "fur", "lol" => [ ], 1337 => 1337, "noob" => [ MSFL::Types::Set.new([123]), { 123 => 456, "onetwo" => 34 } ] } }
|
260
|
+
|
261
|
+
let(:expected) do
|
262
|
+
MSFL::Types::Set.new(
|
263
|
+
[
|
264
|
+
"bar",
|
265
|
+
{
|
266
|
+
cat: 1221,
|
267
|
+
dog: "fur",
|
268
|
+
lol: MSFL::Types::Set.new([]),
|
269
|
+
:"1337" => 1337,
|
270
|
+
noob: MSFL::Types::Set.new(
|
271
|
+
[
|
272
|
+
MSFL::Types::Set.new([123]),
|
273
|
+
{ :"123" => 456, onetwo: 34 }
|
274
|
+
])
|
275
|
+
},
|
276
|
+
84
|
277
|
+
])
|
278
|
+
end
|
279
|
+
|
280
|
+
it "recursively coerces hash keys to symbols" do
|
281
|
+
expect(mut).to eq expected
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
157
286
|
end
|