Class: TypeSpecFromSerializers::Property

Inherits:
Struct
  • Object
show all
Defined in:
lib/typespec_from_serializers/generator.rb

Overview

Internal: The type metadata for a serializer attribute.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#column_nameObject

Returns the value of attribute column_name

Returns:

  • (Object)

    the current value of column_name



180
181
182
# File 'lib/typespec_from_serializers/generator.rb', line 180

def column_name
  @column_name
end

#multiObject

Returns the value of attribute multi

Returns:

  • (Object)

    the current value of multi



180
181
182
# File 'lib/typespec_from_serializers/generator.rb', line 180

def multi
  @multi
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



180
181
182
# File 'lib/typespec_from_serializers/generator.rb', line 180

def name
  @name
end

#optionalObject

Returns the value of attribute optional

Returns:

  • (Object)

    the current value of optional



180
181
182
# File 'lib/typespec_from_serializers/generator.rb', line 180

def optional
  @optional
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



180
181
182
# File 'lib/typespec_from_serializers/generator.rb', line 180

def type
  @type
end

Instance Method Details

#as_typespecObject



210
211
212
213
214
215
216
217
218
# File 'lib/typespec_from_serializers/generator.rb', line 210

def as_typespec
  type_str = if type.respond_to?(:tsp_name)
    type.tsp_name
  else
    type || TypeSpecFromSerializers.config.unknown_type
  end

  "#{name}#{"?" if optional}: #{type_str}#{"[]" if multi};"
end

#infer_typespec_from(columns_hash, defined_enums, tsp_model) ⇒ Object

Internal: Infers the property’s type by checking a corresponding SQL column, or falling back to a TypeSpec model if provided.



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/typespec_from_serializers/generator.rb', line 196

def infer_typespec_from(columns_hash, defined_enums, tsp_model)
  if type
    type
  elsif (enum = defined_enums[column_name.to_s])
    self.type = enum.keys.map(&:inspect).join(" | ")
  elsif (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]
  elsif tsp_model
    self.type = "#{tsp_model}.#{name}::type"
  end
end

#inspectObject



190
191
192
# File 'lib/typespec_from_serializers/generator.rb', line 190

def inspect
  to_h.inspect
end