Class: SorbetBaml::TypeMapper

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/sorbet_baml/type_mapper.rb

Overview

Maps Sorbet type objects to BAML type strings

Class Method Summary collapse

Class Method Details

.map_array_type(type_object) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/sorbet_baml/type_mapper.rb', line 107

def self.map_array_type(type_object)
  element_type = map_type(type_object.type)

  # If element type contains union (|), wrap in parentheses for correct precedence
  if element_type.include?('|')
    "(#{element_type})[]"
  else
    "#{element_type}[]"
  end
end

.map_hash_type(type_object) ⇒ Object



119
120
121
122
123
124
# File 'lib/sorbet_baml/type_mapper.rb', line 119

def self.map_hash_type(type_object)
  # T::Types::TypedHash has keys and values methods
  key_type = map_type(type_object.keys)
  value_type = map_type(type_object.values)
  "map<#{key_type}, #{value_type}>"
end

.map_simple_type(raw_type) ⇒ Object



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
# File 'lib/sorbet_baml/type_mapper.rb', line 37

def self.map_simple_type(raw_type)
  case raw_type.name
  when 'String'
    'string'
  when 'Integer'
    'int'
  when 'Float'
    'float'
  when 'TrueClass', 'FalseClass'
    'bool'
  when 'NilClass'
    'null'
  when 'Symbol'
    'string'
  when 'Date', 'DateTime', 'Time'
    'string'
  else
    # Check if it's a T::Struct or T::Enum
    if raw_type < T::Struct || raw_type < T::Enum
      type_name = raw_type.name || raw_type.to_s
      type_name.split('::').last || 'unknown'
    else
      'unknown'
    end
  end
end

.map_type(type_object) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sorbet_baml/type_mapper.rb', line 12

def self.map_type(type_object)
  return 'string' if type_object.nil?

  case type_object
  when T::Types::Simple
    map_simple_type(type_object.raw_type)
  when T::Types::TypedArray
    map_array_type(type_object)
  when T::Types::TypedHash
    map_hash_type(type_object)
  when T::Types::Untyped
    # T.untyped maps to BAML's json type for dynamic values
    'json'
  else
    # Check if it's a union type (T.nilable or T.any)
    if type_object.respond_to?(:types)
      map_union_type(type_object)
    else
      # Fallback for unknown types
      'unknown'
    end
  end
end

.map_union_type(type_object) ⇒ Object



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
95
96
97
# File 'lib/sorbet_baml/type_mapper.rb', line 65

def self.map_union_type(type_object)
  types = type_object.types

  # Only T::Types::Simple has raw_type - check type class before accessing
  nil_type = types.find { |t| nil_type?(t) }
  non_nil_types = types.reject { |t| nil_type?(t) }

  return 'null' if non_nil_types.empty?

  if non_nil_types.size == 1
    # This is T.nilable(T) - single type with nil
    base_type = map_type(non_nil_types.first)
    return nil_type ? "#{base_type}?" : base_type
  end

  # This is T.any with multiple types
  mapped_types = non_nil_types.map { |t| map_type(t) }

  # Special case: TrueClass + FalseClass = bool
  union_string = if mapped_types.sort == %w[bool bool]
                   'bool'
                 else
                   # Remove duplicates and join
                   mapped_types.uniq.join(' | ')
                 end

  # If nil is present, wrap in parentheses and add ?
  if nil_type
    "(#{union_string})?"
  else
    union_string
  end
end

.nil_type?(type_obj) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/sorbet_baml/type_mapper.rb', line 102

def self.nil_type?(type_obj)
  type_obj.is_a?(T::Types::Simple) && type_obj.raw_type == NilClass || false
end