string-keys-to-int: WIP
This commit is contained in:
parent
c094ed7dfb
commit
15a94a1609
@ -14,6 +14,8 @@ targets:
|
||||
main: src/json-to-cbor.cr
|
||||
hs:
|
||||
main: src/hs.cr
|
||||
string-keys-to-int:
|
||||
main: src/string-keys-to-int.cr
|
||||
bm-json-vs-cbor:
|
||||
main: tests/json-vs-cbor.cr
|
||||
|
||||
|
54
src/string-keys-to-int.cr
Normal file
54
src/string-keys-to-int.cr
Normal file
@ -0,0 +1,54 @@
|
||||
require "cbor"
|
||||
|
||||
if ARGV.size >= 1 && ARGV[0] == "-h"
|
||||
puts "usage: cbor-from-string-to-int-hash-keys < file.cbor"
|
||||
puts "usage: cbor-from-string-to-int-hash-keys [attribute] < file"
|
||||
exit 0
|
||||
end
|
||||
|
||||
def are_hash_keys_all_int?(data)
|
||||
case d = data
|
||||
when Hash
|
||||
keys = d.keys
|
||||
if keys.all? {|v| v.is_a?(String) }
|
||||
keys.all? {|v| v.as(String).to_i64 rescue false }
|
||||
else
|
||||
false
|
||||
end
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def change(data)
|
||||
# 1. change keys
|
||||
if are_hash_keys_all_int? data
|
||||
puts "YES, DO IT, keys: #{data.as(Hash).keys}"
|
||||
end
|
||||
|
||||
# 2. dig
|
||||
case d = data
|
||||
when Hash
|
||||
keys = d.keys
|
||||
keys.each do |k|
|
||||
change d[k]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
buffer = Bytes.new 1_000_000 # 1 MB
|
||||
|
||||
until STDIN.read(buffer) == 0
|
||||
decoder = CBOR::Decoder.new(buffer)
|
||||
while data = decoder.read_value
|
||||
break if data == 0
|
||||
|
||||
# pp data
|
||||
|
||||
change data
|
||||
|
||||
# STDOUT.write data.to_cbor
|
||||
# STDOUT.flush
|
||||
end
|
||||
buffer = Bytes.new 1_000_000 # 1 MB
|
||||
end
|
Loading…
Reference in New Issue
Block a user