lib/codec.rb in scale_rb-0.1.3 vs lib/codec.rb in scale_rb-0.1.4
- old
+ new
@@ -14,20 +14,36 @@
end
# TODO: set, bitvec
module ScaleRb
class << self
+ def type_def?(type)
+ type.instance_of?(String) && (
+ bytes?(type) ||
+ boolean?(type) ||
+ string?(type) ||
+ compact?(type) ||
+ int?(type) ||
+ uint?(type) ||
+ option?(type) ||
+ array?(type) ||
+ vec?(type) ||
+ tuple?(type)
+ ) ||
+ type.instance_of?(Hash)
+ end
+
def bytes?(type)
type.downcase == 'bytes'
end
def boolean?(type)
type.downcase == 'bool' || type.downcase == 'boolean'
end
def string?(type)
- type.downcase == 'str' || type.downcase == 'string' || type.downcase == 'text'
+ type.downcase == 'str' || type.downcase == 'string' || type.downcase == 'text' || type.downcase == 'type'
end
def compact?(type)
type.downcase == 'compact' ||
(type[0..7].downcase == 'compact<' && type[-1] == '>')
@@ -95,17 +111,19 @@
# Helper functions
module ScaleRb
class << self
def _get_final_type_from_registry(registry, type)
- mapped_type = registry[type]
- if mapped_type.nil?
- nil
- elsif registry[mapped_type].nil?
- mapped_type
- else
- _get_final_type_from_registry(registry, mapped_type)
- end
+ raise "Wrong lookup type #{type.class}" if !type.instance_of?(String) && !type.instance_of?(Hash)
+
+ return if type.instance_of?(Hash)
+
+ mapped = registry._get(type) # mapped: String(name, type_def) | Hash(type_def: struct, enum) | nil
+
+ return if mapped.nil?
+ return mapped if type_def?(mapped)
+
+ _get_final_type_from_registry(registry, mapped)
end
def _decode_types(types, bytes, registry)
_decode_each(types, bytes) do |type, remaining_bytes|
decode(type, remaining_bytes, registry)