msgpack-inspect 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ module MessagePack
2
+ module Inspect
3
+ module SimpleYAMLDumper
4
+ def self.dump(object)
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,356 @@
1
+ module MessagePack
2
+ module Inspect
3
+ module Streamer
4
+ def self.get(format)
5
+ case format
6
+ when :yaml
7
+ YAMLStreamer
8
+ when :json
9
+ JSONStreamer
10
+ when :jsonl
11
+ JSONLStreamer
12
+ when nil
13
+ NullStreamer
14
+ else
15
+ raise ArgumentError, "unknown format #{format}"
16
+ end
17
+ end
18
+ end
19
+
20
+ module NullStreamer
21
+ def self.objects(io, depth)
22
+ yield
23
+ end
24
+ def self.object(io, depth, index)
25
+ yield
26
+ end
27
+ end
28
+
29
+ module YAMLStreamer
30
+ def self.objects(io, depth)
31
+ io.puts "---" if depth == 0
32
+ yield
33
+ end
34
+
35
+ def self.object(io, depth, index)
36
+ yield
37
+ end
38
+
39
+ def indent(head = false)
40
+ if head
41
+ " " * (@depth - 1) + "- "
42
+ else
43
+ " " * @depth
44
+ end
45
+ end
46
+
47
+ def write(*attrs)
48
+ attrs.each do |attr|
49
+ case attr
50
+ when :format
51
+ @io.puts %!#{indent(@heading)}format: "#{@format.to_s}"!
52
+ when :header
53
+ @io.puts %!#{indent}header: "0x#{hex(@header)}"!
54
+ when :data
55
+ @io.puts %!#{indent}data: "0x#{@data}"!
56
+ when :value
57
+ if @value.nil?
58
+ @io.puts %!#{indent}value: null!
59
+ else
60
+ @io.puts %!#{indent}value: #{@value.inspect}!
61
+ end
62
+ when :length
63
+ @io.puts %!#{indent}length: #{@length}!
64
+ when :exttype
65
+ @io.puts %!#{indent}exttype: #{@exttype}!
66
+ end
67
+ end
68
+ end
69
+
70
+ def attributes(io)
71
+ write(:format, :header)
72
+
73
+ super
74
+
75
+ write(:error) if @error
76
+
77
+ case @format
78
+ when :fixint, :uint8, :uint16, :uint32, :uint64, :int8, :int16, :int32, :int64
79
+ write(:data, :value)
80
+ when :fixmap, :map16, :map32
81
+ write(:length)
82
+ when :fixarray, :array16, :array32
83
+ write(:length)
84
+ when :fixstr, :str8, :str16, :str32
85
+ write(:length, :data, :value)
86
+ when :nil
87
+ write(:data, :value)
88
+ when :false
89
+ write(:data, :value)
90
+ when :true
91
+ write(:data, :value)
92
+ when :bin8, :bin16, :bin32
93
+ write(:length, :data, :value)
94
+ when :ext8, :ext16, :ext32, :fixext1, :fixext2, :fixext4, :fixext8, :fixext16
95
+ if @value
96
+ write(:exttype, :length, :data, :value)
97
+ else
98
+ write(:exttype, :length, :data)
99
+ end
100
+ when :float32, :float64
101
+ write(:data, :value)
102
+ when :never_used
103
+ write(:data)
104
+ end
105
+ end
106
+
107
+ def elements(&block)
108
+ if @length == 0
109
+ @io.puts %!#{indent}children: []!
110
+ return
111
+ end
112
+
113
+ @io.puts %!#{indent}children:!
114
+ super
115
+ end
116
+
117
+ def element_key
118
+ @io.puts %!#{indent} - key:!
119
+ super
120
+ end
121
+
122
+ def element_value
123
+ @io.puts %!#{indent} value:!
124
+ super
125
+ end
126
+ end
127
+
128
+ module JSONStreamer
129
+ def self.objects(io, depth)
130
+ io.puts "["
131
+ retval = yield
132
+ io.puts "" # newline after tailing object without comma
133
+ io.print " " * depth, "]"
134
+ retval
135
+ end
136
+
137
+ def self.object(io, depth, index)
138
+ if index > 0
139
+ io.puts ","
140
+ end
141
+ retval = yield
142
+ io.puts "" # write newline after last attribute of object
143
+ io.print " " * (depth - 1), " }"
144
+ retval
145
+ end
146
+
147
+ def indent(head = @first_line)
148
+ if head
149
+ " " * (@depth - 1) + " { "
150
+ else
151
+ " " * @depth
152
+ end
153
+ end
154
+
155
+ def write(*attrs)
156
+ attrs.each do |attr|
157
+ @io.puts "," unless @first_line
158
+ case attr
159
+ when :format
160
+ @io.print %!#{indent}"format": "#{@format.to_s}"!
161
+ when :header
162
+ @io.print %!#{indent}"header": "0x#{hex(@header)}"!
163
+ when :data
164
+ @io.print %!#{indent}"data": "0x#{@data}"!
165
+ when :value
166
+ if @value.nil?
167
+ @io.print %!#{indent}"value": null!
168
+ else
169
+ @io.print %!#{indent}"value": #{@value.inspect}!
170
+ end
171
+ when :length
172
+ @io.print %!#{indent}"length": #{@length}!
173
+ when :exttype
174
+ @io.print %!#{indent}"exttype": #{@exttype}!
175
+ end
176
+ @first_line = false
177
+ end
178
+ end
179
+
180
+ def attributes(io)
181
+ write(:format, :header)
182
+
183
+ super
184
+
185
+ write(:error) if @error
186
+
187
+ case @format
188
+ when :fixint, :uint8, :uint16, :uint32, :uint64, :int8, :int16, :int32, :int64
189
+ write(:data, :value)
190
+ when :fixmap, :map16, :map32
191
+ write(:length)
192
+ when :fixarray, :array16, :array32
193
+ write(:length)
194
+ when :fixstr, :str8, :str16, :str32
195
+ write(:length, :data, :value)
196
+ when :nil
197
+ write(:data, :value)
198
+ when :false
199
+ write(:data, :value)
200
+ when :true
201
+ write(:data, :value)
202
+ when :bin8, :bin16, :bin32
203
+ write(:length, :data, :value)
204
+ when :ext8, :ext16, :ext32, :fixext1, :fixext2, :fixext4, :fixext8, :fixext16
205
+ if @value
206
+ write(:exttype, :length, :data, :value)
207
+ else
208
+ write(:exttype, :length, :data)
209
+ end
210
+ when :float32, :float64
211
+ write(:data, :value)
212
+ when :never_used
213
+ write(:data)
214
+ end
215
+ end
216
+
217
+ def elements(&block)
218
+ @io.puts ","
219
+
220
+ if @length == 0
221
+ @io.print %!#{indent}"children": []!
222
+ return
223
+ end
224
+
225
+ @io.puts %!#{indent}"children": [!
226
+ super
227
+ @io.puts "" # newline after last element of array/hash
228
+ @io.print %!#{indent}]!
229
+ end
230
+
231
+ def element_key
232
+ if @first_kv_pair
233
+ @first_kv_pair = false
234
+ else
235
+ @io.puts ","
236
+ end
237
+ @io.puts %!#{indent} { "key":!
238
+ super
239
+ end
240
+
241
+ def element_value
242
+ @io.puts "," # tailing key object
243
+ @io.puts %!#{indent} "value":!
244
+ super
245
+ @io.puts "" # newline after value object
246
+ @io.print %!#{indent} }!
247
+ end
248
+ end
249
+
250
+ module JSONLStreamer
251
+ def self.objects(io, depth)
252
+ yield
253
+ end
254
+
255
+ def self.object(io, depth, index)
256
+ if depth > 1 && index > 0
257
+ io.print ","
258
+ end
259
+ retval = yield
260
+ io.print "}"
261
+ io.puts "" if depth == 1
262
+ retval
263
+ end
264
+
265
+ def write(*attrs)
266
+ attrs.each do |attr|
267
+ @io.print "," unless @first_obj
268
+ case attr
269
+ when :format
270
+ @io.print %!{"format":"#{@format.to_s}"!
271
+ when :header
272
+ @io.print %!"header":"0x#{hex(@header)}"!
273
+ when :data
274
+ @io.print %!"data":"0x#{@data}"!
275
+ when :value
276
+ if @value.nil?
277
+ @io.print %!"value":null!
278
+ else
279
+ @io.print %!"value":#{@value.inspect}!
280
+ end
281
+ when :length
282
+ @io.print %!"length":#{@length}!
283
+ when :exttype
284
+ @io.print %!"exttype":#{@exttype}!
285
+ end
286
+ @first_obj = false
287
+ end
288
+ end
289
+
290
+ def attributes(io)
291
+ write(:format, :header)
292
+
293
+ super
294
+
295
+ write(:error) if @error
296
+
297
+ case @format
298
+ when :fixint, :uint8, :uint16, :uint32, :uint64, :int8, :int16, :int32, :int64
299
+ write(:data, :value)
300
+ when :fixmap, :map16, :map32
301
+ write(:length)
302
+ when :fixarray, :array16, :array32
303
+ write(:length)
304
+ when :fixstr, :str8, :str16, :str32
305
+ write(:length, :data, :value)
306
+ when :nil
307
+ write(:data, :value)
308
+ when :false
309
+ write(:data, :value)
310
+ when :true
311
+ write(:data, :value)
312
+ when :bin8, :bin16, :bin32
313
+ write(:length, :data, :value)
314
+ when :ext8, :ext16, :ext32, :fixext1, :fixext2, :fixext4, :fixext8, :fixext16
315
+ if @value
316
+ write(:exttype, :length, :data, :value)
317
+ else
318
+ write(:exttype, :length, :data)
319
+ end
320
+ when :float32, :float64
321
+ write(:data, :value)
322
+ when :never_used
323
+ write(:data)
324
+ end
325
+ end
326
+
327
+ def elements(&block)
328
+ @io.print ","
329
+ if @length == 0
330
+ @io.print %!"children":[]!
331
+ return
332
+ end
333
+
334
+ @io.print %!"children":[!
335
+ super
336
+ @io.print %!]!
337
+ end
338
+
339
+ def element_key
340
+ if @first_kv_pair
341
+ @first_kv_pair = false
342
+ else
343
+ @io.print ","
344
+ end
345
+ @io.print %!{"key":!
346
+ super
347
+ end
348
+
349
+ def element_value
350
+ @io.print %!,"value":!
351
+ super
352
+ @io.print %!}!
353
+ end
354
+ end
355
+ end
356
+ end
@@ -1,5 +1,5 @@
1
1
  module MessagePack
2
2
  module Inspect
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
data/mrbgem.rake ADDED
@@ -0,0 +1,13 @@
1
+ require_relative 'mrblib/msgpack/inspect/version'
2
+
3
+ spec = MRuby::Gem::Specification.new('msgpack-inspect') do |spec|
4
+ spec.bins = ['msgpack-inspect']
5
+ spec.add_dependency 'mruby-io', mgem: 'mruby-io'
6
+ spec.add_dependency 'mruby-print', :core => 'mruby-print'
7
+ spec.add_dependency 'mruby-mtest', :mgem => 'mruby-mtest'
8
+ end
9
+
10
+ spec.license = 'Apache-2.0'
11
+ spec.author = 'Satoshi Tagomori'
12
+ spec.summary = 'MessagePack inspection tool written in Ruby'
13
+ spec.version = MessagePack::Inspect::VERSION
@@ -0,0 +1,66 @@
1
+ def show_usage
2
+ msg = <<-EOL
3
+ Usage: msgpack-inspect [options] FILE"
4
+
5
+ Options:
6
+ -f, --format FORMAT Output format of inspection result (yaml/json) [default: yaml]
7
+ -r, --require LIB (Not supported in binary executable)
8
+ -h, --help Show this message
9
+ -v, --version Show version of this software
10
+ EOL
11
+ puts msg
12
+ end
13
+
14
+ def __main__(argv)
15
+ # argv includes $0 in mruby
16
+ argv.shift
17
+ format = :yaml
18
+ filename = nil
19
+
20
+ parsing_option = true
21
+ while parsing_option && !argv.empty?
22
+ case argv[0]
23
+ when '-f', '--format'
24
+ argv.shift
25
+ format = argv.shift.to_sym
26
+ when '-r', '--require'
27
+ show_usage
28
+ raise "This release doesn't support -r/--require option."
29
+ when '-h', '--help'
30
+ show_usage
31
+ return
32
+ when '-v', '--version'
33
+ puts "msgpack-inspect #{MessagePack::Inspect::VERSION} (mruby binary)"
34
+ return
35
+ else
36
+ arg = argv.shift
37
+ if arg == '--'
38
+ parsing_option = false
39
+ elsif arg.start_with?('-')
40
+ show_usage
41
+ raise "Unknown option specified: #{arg}"
42
+ else
43
+ filename = arg
44
+ parsing_option = false
45
+ end
46
+ end
47
+ end
48
+ if !filename && argv.size > 0
49
+ filename = argv.shift
50
+ end
51
+ if !filename
52
+ show_usage
53
+ raise "Input file path not specified."
54
+ end
55
+
56
+ io = if filename == '-'
57
+ STDIN
58
+ else
59
+ File.open(filename, 'rb')
60
+ end
61
+ unless MessagePack::Inspect::FORMATS.include?(format)
62
+ show_usage
63
+ raise "Unsupported format: #{format}"
64
+ end
65
+ puts MessagePack::Inspect.inspect(io).dump(format)
66
+ end