Class: ScaleRb::Metadata::Registry
- Inherits:
-
Object
- Object
- ScaleRb::Metadata::Registry
- Includes:
- Types
- Defined in:
- lib/scale_rb/metadata/registry.rb
Constant Summary
Constants included from Types
Types::DecodeResult, Types::HashMap, Types::Hex, Types::PortableType, Types::Primitive, Types::Registry, Types::Ti, Types::TypedArray, Types::U8, Types::U8Array, Types::UnsignedInteger, Types::VariantKind
Instance Attribute Summary collapse
-
#keys ⇒ Object
readonly
- % keys
-
Integer -> String.
-
#lookup ⇒ Object
readonly
- Map name to index of type in ‘types` array % lookup
-
String -> Integer.
-
#old_types ⇒ Object
readonly
Returns the value of attribute old_types.
-
#types ⇒ Object
readonly
- % types
-
Array<PortableType>.
Instance Method Summary collapse
- #[](identifier) ⇒ Object
-
#as_primitive(name) ⇒ Object
- % as_primitive
-
String -> PrimitiveType | nil.
- #build ⇒ Object
-
#build_from_definition(name, definition) ⇒ Object
- % build_from_definition
-
String -> OldTypeDefinition -> PortableType | TypeAlias.
-
#build_portable_type(ast_type) ⇒ Object
- % build_portable_type
-
NamedType | ArrayType | TupleType -> PortableType __ :build_portable_type, { ast_type: TypedArray[TypeExp::ArrayType | TypeExp::TupleType | TypeExp::NamedType] } => PortableType.
-
#build_portable_type_from_named_type(named_type) ⇒ Object
- % build_portable_type_from_named_type
-
NamedType -> PortableType.
-
#initialize(old_types) ⇒ Registry
constructor
- % initialize
-
Hash<Symbol, Any> -> void.
- #inspect ⇒ Object
- #to_s ⇒ Object
-
#use(old_type_exp) ⇒ Object
- % use
-
String -> Integer.
Constructor Details
#initialize(old_types) ⇒ Registry
- % initialize
-
Hash<Symbol, Any> -> void
25 26 27 28 29 30 31 32 |
# File 'lib/scale_rb/metadata/registry.rb', line 25 def initialize(old_types) @old_types = old_types @lookup = {} @keys = {} @types = [] build() end |
Instance Attribute Details
#keys ⇒ Object (readonly)
- % keys
-
Integer -> String
17 18 19 |
# File 'lib/scale_rb/metadata/registry.rb', line 17 def keys @keys end |
#lookup ⇒ Object (readonly)
Map name to index of type in ‘types` array
- % lookup
-
String -> Integer
14 15 16 |
# File 'lib/scale_rb/metadata/registry.rb', line 14 def lookup @lookup end |
#old_types ⇒ Object (readonly)
Returns the value of attribute old_types.
22 23 24 |
# File 'lib/scale_rb/metadata/registry.rb', line 22 def old_types @old_types end |
#types ⇒ Object (readonly)
- % types
-
Array<PortableType>
20 21 22 |
# File 'lib/scale_rb/metadata/registry.rb', line 20 def types @types end |
Instance Method Details
#[](identifier) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/scale_rb/metadata/registry.rb', line 40 def [](identifier) if identifier.is_a?(::Integer) @types[identifier] elsif identifier.is_a?(::String) @types[use(identifier)] else raise "Unknown identifier type: #{identifier.class}" end end |
#as_primitive(name) ⇒ Object
- % as_primitive
-
String -> PrimitiveType | nil
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/scale_rb/metadata/registry.rb', line 128 def as_primitive(name) case name.downcase when /^i\d+$/ PrimitiveType.new(primitive: "I#{name[1..]}".to_sym) when /^u\d+$/ PrimitiveType.new(primitive: "U#{name[1..]}".to_sym) when /^bool$/ PrimitiveType.new(primitive: :Bool) when /^str$/, /^text$/ PrimitiveType.new(primitive: :Str) else nil end end |
#build ⇒ Object
34 35 36 37 38 |
# File 'lib/scale_rb/metadata/registry.rb', line 34 def build() @old_types.keys.each do |name| use(name.to_s) end end |
#build_from_definition(name, definition) ⇒ Object
- % build_from_definition
-
String -> OldTypeDefinition -> PortableType | TypeAlias
type OldTypeDefinition = String | OldEnumDefinition | OldStructDefinition type OldEnumDefinition =
_enum: String[] | Hash<Symbol, Any>,
type OldStructDefinition =
_struct: Hash<Symbol, Any>
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/scale_rb/metadata/registry.rb', line 152 def build_from_definition(name, definition) # rubocop:disable Metrics/MethodLength case definition when String # TypeAlias.new(name, use(definition)) alias_type_id = use(definition) # p "alias_type_id: #{alias_type_id}" types[alias_type_id] when Hash if definition[:_enum] _build_portable_type_from_enum_definition(definition) elsif definition[:_set] raise 'Sets are not supported' else _build_portable_type_from_struct_definition(definition) end end end |
#build_portable_type(ast_type) ⇒ Object
- % build_portable_type
-
NamedType | ArrayType | TupleType -> PortableType
__ :build_portable_type, { ast_type: TypedArray[TypeExp::ArrayType | TypeExp::TupleType | TypeExp::NamedType] } => PortableType
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/scale_rb/metadata/registry.rb', line 81 def build_portable_type(ast_type) case ast_type when TypeExp::ArrayType ArrayType.new(use(ast_type.item), ast_type.len, registry: self) when TypeExp::TupleType TupleType.new(ast_type.params.map { |param| use(param) }) when TypeExp::NamedType build_portable_type_from_named_type(ast_type) else raise "Unknown type: #{ast_type.class}" end end |
#build_portable_type_from_named_type(named_type) ⇒ Object
- % build_portable_type_from_named_type
-
NamedType -> PortableType
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/scale_rb/metadata/registry.rb', line 95 def build_portable_type_from_named_type(named_type) name = named_type.name params = named_type.params definition = @old_types[name.to_sym] return build_from_definition(name, definition) if definition primitive = as_primitive(name) return primitive if primitive case name when 'Vec' item_index = use(params[0].to_s) SequenceType.new(type: item_index, registry: self) when 'Option' item_index = use(params[0].to_s) VariantType.option(item_index, self) when 'Result' ok_index = use(params[0].to_s) err_index = use(params[1].to_s) VariantType.result(ok_index, err_index, self) when 'Compact' # item_index = use(params[0].to_s) # CompactType.new(type: item_index, registry: self) CompactType.new when 'Null' UnitType.new else raise "Unknown type: #{name}" end end |
#inspect ⇒ Object
50 51 52 |
# File 'lib/scale_rb/metadata/registry.rb', line 50 def inspect "registry(#{@types.length} types)" end |
#to_s ⇒ Object
54 55 56 57 58 |
# File 'lib/scale_rb/metadata/registry.rb', line 54 def to_s @types.map.with_index do |type, index| "#{@keys[index]} => #{type.to_s}" end.join("\n") end |
#use(old_type_exp) ⇒ Object
- % use
-
String -> Integer
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/scale_rb/metadata/registry.rb', line 61 def use(old_type_exp) raise "Empty old_type_exp: #{old_type_exp}" if old_type_exp.nil? || old_type_exp.strip == '' ast_type = TypeExp.parse(old_type_exp) raise "No AST type for #{old_type_exp}" if ast_type.nil? key = ast_type.to_s ti = lookup[key] return ti if ti ti = @types.length @types[ti] = "Placeholder" @lookup[key] = ti @keys[ti] = key @types[ti] = build_portable_type(ast_type) ti end |