From e87dcc3178e58b75e3489d2afff4834f13ae1c52 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Mon, 30 Nov 2020 04:54:19 +0100 Subject: [PATCH] string-keys-to-int working. --- README.md | 10 ++++++++++ src/string-keys-to-int.cr | 32 ++++++++------------------------ 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 76f221d..3029f65 100644 --- a/README.md +++ b/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 +``` + diff --git a/src/string-keys-to-int.cr b/src/string-keys-to-int.cr index 52b37e5..8dddadb 100644 --- a/src/string-keys-to-int.cr +++ b/src/string-keys-to-int.cr @@ -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