Class: Foobara::RemoteGenerator::Generators::TypescriptFromManifestBaseGenerator
- Inherits:
-
FilesGenerator
- Object
- FilesGenerator
- Foobara::RemoteGenerator::Generators::TypescriptFromManifestBaseGenerator
- Defined in:
- foobara-typescript-remote-command-generator-1.3.1/src/generators/typescript_from_manifest_base_generator.rb
Direct Known Subclasses
Auth::AccessTokensGenerator, Auth::LoginCommandGenerator, Auth::LogoutCommandGenerator, Auth::RequiresAuthCommandGenerator, CommandGenerator, CommandInputsGenerator, DomainGenerator, ErrorGenerator, ManifestGenerator, OrganizationGenerator, ProcessorClassGenerator, RemoteCommandGenerator, TypeGenerator
Constant Summary
Constants included from TruncatedInspect
Instance Attribute Summary
Attributes inherited from FilesGenerator
#belongs_to_dependency_group, #relevant_manifest
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
Files generator checks that the relevant_manifest is the same but this is faster.
- #attributes_to_ts_type(attributes, dependency_group:, association_depth: AssociationDepth::AMBIGUOUS, model_and_entity_free: false, is_output: false, parent: nil) ⇒ Object
- #auto_dirty_queries? ⇒ Boolean
- #collision_winners ⇒ Object
- #custom_type_to_ts_type_name(type) ⇒ Object
- #dependency_group ⇒ Object
- #dependency_roots ⇒ Object
- #domain_name ⇒ Object
-
#foobara_type_to_ts_type(type_declaration, dependency_group: self.dependency_group, name: nil, association_depth: AssociationDepth::AMBIGUOUS, initial: true, model_and_entity_free: false, is_output: false, parent: nil) ⇒ Object
is_output means the value came from elsewhere and is fully formed.
- #hash ⇒ Object
- #import_destructure ⇒ Object
- #import_path ⇒ Object
- #import_path_array ⇒ Object
-
#initialize(relevant_manifest = (none_given = true)) ⇒ TypescriptFromManifestBaseGenerator
constructor
A new instance of TypescriptFromManifestBaseGenerator.
- #model_to_ts_model_name(model, association_depth: AssociationDepth::AMBIGUOUS, initial: true) ⇒ Object
- #organization_name ⇒ Object
- #parent ⇒ Object
- #path_to_root ⇒ Object
- #templates_dir ⇒ Object
- #ts_instance_full_name ⇒ Object
- #ts_instance_full_path ⇒ Object
- #ts_instance_path ⇒ Object
- #value_to_ts_value(value) ⇒ Object
- #will_define ⇒ Object
Methods inherited from FilesGenerator
#absolute_template_path, #applicable?, #dependencies, #dependencies_to_generate, #eql?, #erb_template, #generate, generator_for, #generator_for, generators_for, #generators_for, #method_missing, #respond_to_missing?, #target_dir, #target_path, #template_path, #template_string
Methods included from TruncatedInspect
Constructor Details
#initialize(relevant_manifest = (none_given = true)) ⇒ TypescriptFromManifestBaseGenerator
Returns a new instance of TypescriptFromManifestBaseGenerator.
97 98 99 100 101 102 103 104 105 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 97 def initialize(relevant_manifest = (none_given = true)) if none_given || relevant_manifest.is_a?(Manifest::BaseManifest) super else # :nocov: raise ArgumentError, "Expected a Foobara::Manifest, got #{relevant_manifest.class}" # :nocov: end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Foobara::FilesGenerator
Class Method Details
.manifest_to_generator_classes(manifest) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 28 def manifest_to_generator_classes(manifest) case manifest when Manifest::Command generator_classes = case manifest.full_command_name when "Foobara::Auth::RefreshLogin" Auth::RefreshLoginGenerator when "Foobara::Auth::Login" Auth::LoginGenerator when "Foobara::Auth::Logout" Auth::LogoutGenerator when /\bGetCurrentUser$/ Auth::RequiresAuthGenerator else if manifest.requires_authentication? Auth::RequiresAuthGenerator else CommandGenerator end end [ *generator_classes, CommandInputsGenerator, CommandResultGenerator, CommandCastResultGenerator, CommandErrorsGenerator, CommandErrorsIndexGenerator, CommandManifestGenerator ] when Manifest::Domain [ DomainGenerator, DomainConfigGenerator, DomainManifestGenerator ] when Manifest::Organization [ OrganizationGenerator, OrganizationConfigGenerator, OrganizationManifestGenerator ] when Manifest::Entity, Manifest::DetachedEntity [ EntityGenerator, EntityVariantsGenerator, EntityManifestGenerator ] when Manifest::Model [ ModelGenerator, ModelVariantsGenerator, ModelManifestGenerator ] when Manifest::Error ErrorGenerator when Manifest::ProcessorClass ProcessorClassGenerator when Manifest::RootManifest RootManifestGenerator when Manifest::Type TypeGenerator else # :nocov: raise "Not sure how build a generator for a #{manifest}" # :nocov: end end |
Instance Method Details
#==(other) ⇒ Object
Files generator checks that the relevant_manifest is the same but this is faster
420 421 422 423 424 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 420 def ==(other) self.class == other.class && manifest_path == other.manifest_path && root_manifest == other.root_manifest end |
#attributes_to_ts_type(attributes, dependency_group:, association_depth: AssociationDepth::AMBIGUOUS, model_and_entity_free: false, is_output: false, parent: nil) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 337 def attributes_to_ts_type( attributes, dependency_group:, association_depth: AssociationDepth::AMBIGUOUS, model_and_entity_free: false, is_output: false, parent: nil ) # TODO: if we don't actually have attribute_declarations because we # are trying to express attributes of any type, then we want Record<string, any> # or something. if attributes.has_attribute_declarations? guts = attributes.attribute_declarations.map do |attribute_name, attribute_declaration| exists = attributes.required?(attribute_name) if !exists && parent&.model? exists = parent.guaranteed_to_exist?(attribute_name) end if !exists && is_output default = attributes.default_for(attribute_name) if default || default == false || (parent&.detached_entity? && attribute_name == parent.primary_key_name.to_sym) exists = true end end " #{attribute_name}#{"?" unless exists}: #{ ( attribute_declaration, dependency_group:, association_depth:, initial: false, model_and_entity_free:, is_output: ) }" end.join("\n") "{\n#{guts}\n}" else # TODO: test this path with an :attributes type (not extended from :attributes but # the built-in type itself directly) # :nocov: "Record<string, any>" # :nocov: end end |
#auto_dirty_queries? ⇒ Boolean
435 436 437 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 435 def auto_dirty_queries? RemoteGenerator.auto_dirty_queries? end |
#collision_winners ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 148 def collision_winners # TODO: odd that root_manifest exists but isn't decorated. We need a decorated and undecorated method root_manifest = Manifest::RootManifest.new(self.root_manifest) [*dependencies].select do |dependency| root_manifest.contains?(dependency.domain_reference, :domain) && dependency.domain == domain end end |
#custom_type_to_ts_type_name(type) ⇒ Object
412 413 414 415 416 417 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 412 def custom_type_to_ts_type_name(type) type = type.to_type if type.is_a?(Manifest::TypeDeclaration) generator = TypeGenerator.new(type) dependency_group.non_colliding_type(generator) end |
#dependency_group ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 117 def dependency_group @dependency_group ||= begin generators = dependencies.map do |dependency| generator_for(dependency) end DependencyGroup.new(generators, deps_are_for: self, name: scoped_full_path.join("."), will_define:, winners: collision_winners) end end |
#dependency_roots ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 135 def dependency_roots return @dependency_roots if defined?(@dependency_roots) unless dependency_group # :nocov: raise "This generator was created without a " \ "dependency_group and therefore cannot call #{__method__}" # :nocov: end @dependency_roots = dependency_group.non_colliding_dependency_roots.sort_by(&:scoped_full_name) end |
#domain_name ⇒ Object
171 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 171 def domain_name = relevant_manifest.domain_name |
#foobara_type_to_ts_type(type_declaration, dependency_group: self.dependency_group, name: nil, association_depth: AssociationDepth::AMBIGUOUS, initial: true, model_and_entity_free: false, is_output: false, parent: nil) ⇒ Object
is_output means the value came from elsewhere and is fully formed. This is helpful for specifying what is expected to be present. If this is provided, then things like attribute properties that have defaults will be considered required and present.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 214 def ( type_declaration, dependency_group: self.dependency_group, name: nil, association_depth: AssociationDepth::AMBIGUOUS, initial: true, model_and_entity_free: false, is_output: false, parent: nil ) if type_declaration.is_a?(Manifest::Error) error_generator = generator_for(type_declaration) return dependency_group.non_colliding_type(error_generator) end type_string = if type_declaration.is_a?(Manifest::Attributes) ts_type = attributes_to_ts_type( type_declaration, association_depth:, dependency_group:, model_and_entity_free:, is_output:, parent: ) if type_declaration.has_attribute_declarations? is_empty = type_declaration.attribute_declarations.empty? if name # TODO: test this code path or delete it # :nocov: return is_empty ? "undefined" : "interface #{name} #{ts_type}" # :nocov: else is_empty ? "undefined" : ts_type end else # TODO: test this path with an :attributes type (not extended from :attributes but # the built-in type itself directly) # :nocov: ts_type # :nocov: end elsif type_declaration.is_a?(Manifest::Array) # TODO: which association_depth do we pass here? ts_type = ( type_declaration.element_type, association_depth:, dependency_group:, initial: false, model_and_entity_free:, is_output: ) "#{ts_type}[]" else type_symbol = type_declaration.type case type_symbol when "string", "boolean" type_symbol when "number", "integer", "float" "number" # TODO: should apply relevant processors to make email a real email type instead of "string" when "symbol", "email" "string" when "duck" "any" when "datetime", "date" "Date" else if type_declaration.model? if model_and_entity_free model_type = type_declaration.to_type if type_declaration.detached_entity? ( model_type.primary_key_type, association_depth:, dependency_group:, initial:, model_and_entity_free:, is_output:, parent: model_type ) else attributes_to_ts_type( model_type.attributes_type, association_depth:, dependency_group:, model_and_entity_free:, is_output:, parent: model_type ) end else model_to_ts_model_name(type_declaration, association_depth:, initial:) end elsif type_declaration.custom? custom_type_to_ts_type_name(type_declaration) end end end if type_string unless type_declaration.reference? if type_declaration.one_of type_string = type_declaration.one_of.map(&:inspect).join(" | ") end if type_declaration.allows_nil? type_string = "#{type_string} | null" end end # TODO: Add description as a comment? name ? "type #{name} = #{type_string}" : type_string else # :nocov: raise "Not sure how to convert #{type_declaration} to a TS type" # :nocov: end end |
#hash ⇒ Object
426 427 428 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 426 def hash manifest_path.hash end |
#import_destructure ⇒ Object
181 182 183 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 181 def import_destructure "{ #{scoped_short_name} }" end |
#import_path ⇒ Object
173 174 175 176 177 178 179 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 173 def import_path if import_path_array.size == 1 "./#{import_path_array.first}" else import_path_array.join("/") end end |
#import_path_array ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 185 def import_path_array path = if target_path.last == "index.ts" target_path[0..-2] else target_path end path[-1] = path.last.gsub(/\.ts$/, "") path end |
#model_to_ts_model_name(model, association_depth: AssociationDepth::AMBIGUOUS, initial: true) ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 387 def model_to_ts_model_name(model, association_depth: AssociationDepth::AMBIGUOUS, initial: true) model = model.to_type if model.is_a?(Manifest::TypeDeclaration) generator_class = case association_depth when AssociationDepth::AMBIGUOUS ModelGenerator when AssociationDepth::ATOM if !initial && model.detached_entity? UnloadedEntityGenerator else AtomModelGenerator end when AssociationDepth::AGGREGATE AggregateModelGenerator else # :nocov: raise "Bad association_depth: #{association_depth}" # :nocov: end generator = generator_class.new(model) dependency_group.non_colliding_type(generator) end |
#organization_name ⇒ Object
170 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 170 def organization_name = relevant_manifest.organization_name |
#parent ⇒ Object
111 112 113 114 115 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 111 def parent if relevant_manifest.parent generator_for(relevant_manifest.parent) end end |
#path_to_root ⇒ Object
430 431 432 433 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 430 def path_to_root path = super path.empty? ? "./" : path end |
#templates_dir ⇒ Object
107 108 109 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 107 def templates_dir "#{__dir__}/../../templates" end |
#ts_instance_full_name ⇒ Object
162 163 164 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 162 def ts_instance_full_name ts_instance_full_path.join(".") end |
#ts_instance_full_path ⇒ Object
166 167 168 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 166 def ts_instance_full_path [*parent&.scoped_full_path, *ts_instance_path] end |
#ts_instance_path ⇒ Object
158 159 160 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 158 def ts_instance_path scoped_path end |
#value_to_ts_value(value) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 197 def value_to_ts_value(value) case value when ::String, Numeric value.inspect when ::Symbol value.to_s.inspect else # :nocov: raise "Not sure how to represent #{value} in typescript. Maybe implement it." # :nocov: end end |
#will_define ⇒ Object
131 132 133 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 131 def will_define nil end |