Class: Expressir::Express::Builders::TypeBuilder
- Inherits:
-
Object
- Object
- Expressir::Express::Builders::TypeBuilder
- Includes:
- Helpers
- Defined in:
- lib/expressir/express/builders/type_builder.rb
Overview
Builds data type nodes (enumeration, select, array, bag, list, set, etc.).
Instance Method Summary collapse
- #build_aggregation_type(ast_data, type_class) ⇒ Object
-
#build_array_type(ast_data) ⇒ Object
Aggregation types.
- #build_bag_type(ast_data) ⇒ Object
-
#build_binary_type(ast_data) ⇒ Object
Binary type.
-
#build_enumeration_item(ast_data) ⇒ Object
Enumeration item.
-
#build_enumeration_type(ast_data) ⇒ Object
Enumeration type.
-
#build_general_aggregation_type(ast_data, type_class) ⇒ Object
General aggregation types (used in parameter types) These use parameter_type instead of instantiable_type.
- #build_general_array_type(ast_data) ⇒ Object
- #build_general_bag_type(ast_data) ⇒ Object
- #build_general_list_type(ast_data) ⇒ Object
- #build_general_set_type(ast_data) ⇒ Object
- #build_list_type(ast_data) ⇒ Object
-
#build_real_type(ast_data) ⇒ Object
Real type.
-
#build_select_type(ast_data) ⇒ Object
Select type.
- #build_set_type(ast_data) ⇒ Object
-
#build_string_type(ast_data) ⇒ Object
String type.
-
#build_type_wrapper(ast_data) ⇒ Object
Type wrappers - choice types that dispatch to inner type.
Methods included from Helpers
#apply_qualifier, #extract_id_ref, #extract_interval_op, #extract_nested_text, #extract_operator, #extract_rel_op, #extract_text, #extract_unary_op, #first_value
Instance Method Details
#build_aggregation_type(ast_data, type_class) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/expressir/express/builders/type_builder.rb', line 85 def build_aggregation_type(ast_data, type_class) bound_spec = ast_data[:bound_spec] || {} bound1 = Builder.build_optional(bound_spec[:bound1]) bound2 = Builder.build_optional(bound_spec[:bound2]) optional = !ast_data[:t_optional].nil? unique = !ast_data[:t_unique].nil? base_type = Builder.build_optional(ast_data[:instantiable_type]) type_class.new( bound1: bound1, bound2: bound2, optional: optional, unique: unique, base_type: base_type, ) end |
#build_array_type(ast_data) ⇒ Object
Aggregation types
53 54 55 |
# File 'lib/expressir/express/builders/type_builder.rb', line 53 def build_array_type(ast_data) build_aggregation_type(ast_data, Expressir::Model::DataTypes::Array) end |
#build_bag_type(ast_data) ⇒ Object
57 58 59 |
# File 'lib/expressir/express/builders/type_builder.rb', line 57 def build_bag_type(ast_data) build_aggregation_type(ast_data, Expressir::Model::DataTypes::Bag) end |
#build_binary_type(ast_data) ⇒ Object
Binary type
34 35 36 37 38 39 40 |
# File 'lib/expressir/express/builders/type_builder.rb', line 34 def build_binary_type(ast_data) width = Builder.build_optional(ast_data[:width_spec]&.dig(:width)) fixed = !ast_data[:width_spec]&.dig(:t_fixed).nil? kwargs = { width: width } kwargs[:fixed] = fixed if fixed Expressir::Model::DataTypes::Binary.new(**kwargs) end |
#build_enumeration_item(ast_data) ⇒ Object
Enumeration item
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/expressir/express/builders/type_builder.rb', line 159 def build_enumeration_item(ast_data) id = nil if ast_data.is_a?(Hash) if ast_data[:enumeration_id] enum_id_data = ast_data[:enumeration_id] id = if enum_id_data.is_a?(Hash) extract_text(enum_id_data[:str]) || extract_text(enum_id_data[:simple_id]&.dig(:str)) else enum_id_data.to_s end elsif ast_data[:simple_id] id = extract_text(ast_data[:simple_id]&.dig(:str)) || extract_text(ast_data[:str]) elsif ast_data[:str] id = extract_text(ast_data[:str]) end elsif ast_data.is_a?(String) id = ast_data end Expressir::Model::DataTypes::EnumerationItem.new(id: id) end |
#build_enumeration_type(ast_data) ⇒ Object
Enumeration type
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/expressir/express/builders/type_builder.rb', line 118 def build_enumeration_type(ast_data) extensible = !ast_data[:t_extensible].nil? items = [] based_on = nil if ast_data[:enumeration_items].is_a?(Hash) items_data = ast_data[:enumeration_items][:list_of_enumeration_item] || ast_data[:enumeration_items][:enumeration_item] if items_data items_data = items_data[:enumeration_item] if items_data.is_a?(Hash) && items_data[:enumeration_item] items_data = [items_data] unless items_data.is_a?(Array) items = items_data.filter_map do |item| actual_item = item.is_a?(Hash) ? (item[:enumeration_item] || item) : item Builder.build({ enumeration_item: actual_item }) end end elsif ast_data[:enumeration_extension].is_a?(Hash) # Wrap type_ref data so the correct handler is called type_ref_data = ast_data[:enumeration_extension][:type_ref] based_on = Builder.build({ type_ref: type_ref_data }) if type_ref_data if ast_data[:enumeration_extension][:enumeration_items].is_a?(Hash) items_data = ast_data[:enumeration_extension][:enumeration_items][:list_of_enumeration_item] || ast_data[:enumeration_extension][:enumeration_items][:enumeration_item] if items_data items_data = items_data[:enumeration_item] if items_data.is_a?(Hash) && items_data[:enumeration_item] items_data = [items_data] unless items_data.is_a?(Array) items = items_data.filter_map do |item| actual_item = item.is_a?(Hash) ? (item[:enumeration_item] || item) : item Builder.build({ enumeration_item: actual_item }) end end end end kwargs = { based_on: based_on, items: items } kwargs[:extensible] = extensible if extensible Expressir::Model::DataTypes::Enumeration.new(**kwargs) end |
#build_general_aggregation_type(ast_data, type_class) ⇒ Object
General aggregation types (used in parameter types) These use parameter_type instead of instantiable_type
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/expressir/express/builders/type_builder.rb', line 104 def build_general_aggregation_type(ast_data, type_class) bound_spec = ast_data[:bound_spec] || {} bound1 = Builder.build_optional(bound_spec[:bound1]) bound2 = Builder.build_optional(bound_spec[:bound2]) base_type = Builder.build_optional(ast_data[:parameter_type]) type_class.new( bound1: bound1, bound2: bound2, base_type: base_type, ) end |
#build_general_array_type(ast_data) ⇒ Object
81 82 83 |
# File 'lib/expressir/express/builders/type_builder.rb', line 81 def build_general_array_type(ast_data) build_general_aggregation_type(ast_data, Expressir::Model::DataTypes::Array) end |
#build_general_bag_type(ast_data) ⇒ Object
77 78 79 |
# File 'lib/expressir/express/builders/type_builder.rb', line 77 def build_general_bag_type(ast_data) build_general_aggregation_type(ast_data, Expressir::Model::DataTypes::Bag) end |
#build_general_list_type(ast_data) ⇒ Object
73 74 75 |
# File 'lib/expressir/express/builders/type_builder.rb', line 73 def build_general_list_type(ast_data) build_general_aggregation_type(ast_data, Expressir::Model::DataTypes::List) end |
#build_general_set_type(ast_data) ⇒ Object
69 70 71 |
# File 'lib/expressir/express/builders/type_builder.rb', line 69 def build_general_set_type(ast_data) build_general_aggregation_type(ast_data, Expressir::Model::DataTypes::Set) end |
#build_list_type(ast_data) ⇒ Object
61 62 63 |
# File 'lib/expressir/express/builders/type_builder.rb', line 61 def build_list_type(ast_data) build_aggregation_type(ast_data, Expressir::Model::DataTypes::List) end |
#build_real_type(ast_data) ⇒ Object
Real type
43 44 45 46 47 48 49 50 |
# File 'lib/expressir/express/builders/type_builder.rb', line 43 def build_real_type(ast_data) precision = Builder.build_optional(ast_data[:precision_spec]) if precision Expressir::Model::DataTypes::Real.new(precision: precision) else Expressir::Model::DataTypes::Real.new end end |
#build_select_type(ast_data) ⇒ Object
Select type
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/expressir/express/builders/type_builder.rb', line 181 def build_select_type(ast_data) extensible = !ast_data[:t_extensible].nil? generic_entity = !ast_data[:t_generic_entity].nil? items = [] based_on = nil if ast_data[:select_list].is_a?(Hash) named_types = ast_data[:select_list][:list_of_named_types] items = Builder.build_children(named_types) if named_types elsif ast_data[:select_extension].is_a?(Hash) # Wrap type_ref data so the correct handler is called if ast_data[:select_extension][:type_ref] based_on = Builder.build({ type_ref: ast_data[:select_extension][:type_ref] }) end if ast_data[:select_extension][:select_list].is_a?(Hash) named_types = ast_data[:select_extension][:select_list][:list_of_named_types] items = Builder.build_children(named_types) if named_types end end kwargs = { based_on: based_on, items: items.compact } kwargs[:extensible] = extensible if extensible kwargs[:generic_entity] = generic_entity if generic_entity Expressir::Model::DataTypes::Select.new(**kwargs) end |
#build_set_type(ast_data) ⇒ Object
65 66 67 |
# File 'lib/expressir/express/builders/type_builder.rb', line 65 def build_set_type(ast_data) build_aggregation_type(ast_data, Expressir::Model::DataTypes::Set) end |
#build_string_type(ast_data) ⇒ Object
String type
25 26 27 28 29 30 31 |
# File 'lib/expressir/express/builders/type_builder.rb', line 25 def build_string_type(ast_data) width = Builder.build_optional(ast_data[:width_spec]&.dig(:width)) fixed = !ast_data[:width_spec]&.dig(:t_fixed).nil? kwargs = { width: width } kwargs[:fixed] = fixed if fixed Expressir::Model::DataTypes::String.new(**kwargs) end |
#build_type_wrapper(ast_data) ⇒ Object
Type wrappers - choice types that dispatch to inner type
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/expressir/express/builders/type_builder.rb', line 208 def build_type_wrapper(ast_data) return nil unless ast_data.is_a?(Hash) ast_data.each do |k, v| next unless v result = Builder.build({ k => v }) return result if result end nil end |