string-keys-to-int working.
This commit is contained in:
parent
795acc1d33
commit
e87dcc3178
10
README.md
10
README.md
@ -25,3 +25,13 @@ json-to-cbor < file.json > file.cbor
|
||||
hs < file.cbor
|
||||
```
|
||||
|
||||
# string-keys-to-int usage
|
||||
|
||||
JSON format has a limitation on the type of hash keys: it has to be a string.
|
||||
CBOR doesn't come with such limitation.
|
||||
`string-keys-to-int` automatically converts string keys into integers, whenever all the keys in a hash can be converted.
|
||||
|
||||
```sh
|
||||
string-keys-to-int < file.cbor
|
||||
```
|
||||
|
||||
|
@ -1,30 +1,16 @@
|
||||
require "cbor"
|
||||
require "option_parser"
|
||||
|
||||
#
|
||||
# JSON format has a limitation on the type of hash keys: it has to be a string.
|
||||
# CBOR doesn't come with such limitation.
|
||||
# string-keys-to-int: automatically converts string keys into integers, whenever all
|
||||
# the keys in a hash can be converted.
|
||||
#
|
||||
|
||||
class Context
|
||||
class_property do_nothing = false
|
||||
class_property print_data = false
|
||||
class_property debug = false
|
||||
end
|
||||
|
||||
OptionParser.parse do |parser|
|
||||
parser.banner = "usage: string-keys-to-int < file.cbor"
|
||||
|
||||
parser.on "-n", "--nothing",
|
||||
"Do nothing, just print when keys would have been converted." do
|
||||
Context.do_nothing = true
|
||||
end
|
||||
|
||||
parser.on "-p", "--print-data",
|
||||
"Do nothing, print keys to convert, input and output data." do
|
||||
Context.do_nothing = true
|
||||
Context.print_data = true
|
||||
parser.on "-d", "--debug",
|
||||
"Debug: print keys to convert, input and output data." do
|
||||
Context.debug = true
|
||||
end
|
||||
|
||||
parser.on "-h", "--help", "Displays this help and exits." do
|
||||
@ -50,7 +36,7 @@ end
|
||||
def change(data)
|
||||
# 1. change keys
|
||||
if are_hash_keys_all_int? data
|
||||
if Context.do_nothing
|
||||
if Context.debug
|
||||
puts "keys to convert: #{data.as(Hash).keys}"
|
||||
end
|
||||
|
||||
@ -84,19 +70,17 @@ until STDIN.read(buffer) == 0
|
||||
while data = decoder.read_value
|
||||
break if data == 0
|
||||
|
||||
if Context.print_data
|
||||
if Context.debug
|
||||
puts "input data:"
|
||||
pp data
|
||||
end
|
||||
|
||||
change data
|
||||
|
||||
if Context.print_data
|
||||
if Context.debug
|
||||
puts "output data:"
|
||||
pp data
|
||||
end
|
||||
|
||||
unless Context.do_nothing
|
||||
else
|
||||
STDOUT.write data.to_cbor
|
||||
STDOUT.flush
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user