Class: TypeSpecFromSerializers::Property
- Inherits:
-
Struct
- Object
- Struct
- TypeSpecFromSerializers::Property
- Defined in:
- lib/typespec_from_serializers/generator.rb
Overview
Internal: The type metadata for a serializer attribute.
Instance Attribute Summary collapse
-
#column_name ⇒ Object
Returns the value of attribute column_name.
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#multi ⇒ Object
Returns the value of attribute multi.
-
#name ⇒ Object
Returns the value of attribute name.
-
#optional ⇒ Object
Returns the value of attribute optional.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #as_typespec ⇒ Object
-
#infer_typespec_from(columns_hash, defined_enums, tsp_model, serializer_class = nil, model_class = nil) ⇒ Object
Internal: Infers the property’s type by checking a corresponding SQL column, or falling back to a TypeSpec model if provided.
- #inspect ⇒ Object
Instance Attribute Details
#column_name ⇒ Object
Returns the value of attribute column_name
342 343 344 |
# File 'lib/typespec_from_serializers/generator.rb', line 342 def column_name @column_name end |
#doc ⇒ Object
Returns the value of attribute doc
342 343 344 |
# File 'lib/typespec_from_serializers/generator.rb', line 342 def doc @doc end |
#multi ⇒ Object
Returns the value of attribute multi
342 343 344 |
# File 'lib/typespec_from_serializers/generator.rb', line 342 def multi @multi end |
#name ⇒ Object
Returns the value of attribute name
342 343 344 |
# File 'lib/typespec_from_serializers/generator.rb', line 342 def name @name end |
#optional ⇒ Object
Returns the value of attribute optional
342 343 344 |
# File 'lib/typespec_from_serializers/generator.rb', line 342 def optional @optional end |
#type ⇒ Object
Returns the value of attribute type
342 343 344 |
# File 'lib/typespec_from_serializers/generator.rb', line 342 def type @type end |
Instance Method Details
#as_typespec ⇒ Object
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/typespec_from_serializers/generator.rb', line 409 def as_typespec type_str = if type.respond_to?(:tsp_name) type.tsp_name elsif type # Map common type symbols/strings through the Sorbet mapping mapped = TypeSpecFromSerializers.config.sorbet_to_typespec_type_mapping[type.to_s] mapped || type else TypeSpecFromSerializers.config.unknown_type end escaped_name = escape_field_name(name) field_line = "#{escaped_name}#{"?" if optional}: #{type_str}#{"[]" if multi};" doc ? "@doc(\"#{escape_doc(doc)}\")\n #{field_line}" : field_line end |
#infer_typespec_from(columns_hash, defined_enums, tsp_model, serializer_class = nil, model_class = nil) ⇒ Object
Internal: Infers the property’s type by checking a corresponding SQL column, or falling back to a TypeSpec model if provided.
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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/typespec_from_serializers/generator.rb', line 359 def infer_typespec_from(columns_hash, defined_enums, tsp_model, serializer_class = nil, model_class = nil) # Priority 1: Explicit type (already set via DSL) if type return type end # Priority 2: Sorbet method signature on serializer (if available) if serializer_class && Sorbet.available? sorbet_info = Sorbet.extract_type_for(serializer_class, column_name) if sorbet_info self.type = sorbet_info[:typespec_type] self.optional = true if sorbet_info[:nilable] self.multi = true if sorbet_info[:array] return type end end # Priority 2b: Sorbet method signature on model (if available) if model_class && Sorbet.available? sorbet_info = Sorbet.extract_type_for(model_class, column_name) if sorbet_info self.type = sorbet_info[:typespec_type] self.optional = true if sorbet_info[:nilable] self.multi = true if sorbet_info[:array] return type end end # Priority 3: ActiveRecord enums if (enum = defined_enums[column_name.to_s]) self.type = enum.keys.map(&:inspect).join(" | ") return type end # Priority 4: SQL schema columns if (column = columns_hash[column_name.to_s]) self.multi = true if column.try(:array) self.optional = true if column.null && !column.default self.type = TypeSpecFromSerializers.config.sql_to_typespec_type_mapping[column.type] return type end # Priority 5: TypeSpec model fallback if tsp_model self.type = "#{tsp_model}.#{name}::type" end type end |
#inspect ⇒ Object
353 354 355 |
# File 'lib/typespec_from_serializers/generator.rb', line 353 def inspect to_h.inspect end |