Module: Crystalline::MetadataFields::ClassMethods

Defined in:
lib/crystalline/metadata_fields.rb

Instance Method Summary collapse

Instance Method Details

#field(field_name, type, metadata = {}) ⇒ Object



27
28
29
30
31
# File 'lib/crystalline/metadata_fields.rb', line 27

def field(field_name, type,  = {})
  attr_accessor field_name

  fields << Field.new(field_name, type, )
end

#field_augmented?Boolean

Returns:



33
34
35
# File 'lib/crystalline/metadata_fields.rb', line 33

def field_augmented?
  true
end

#fieldsObject



21
22
23
24
25
# File 'lib/crystalline/metadata_fields.rb', line 21

def fields
  @__fields__ = [] if @__fields__.nil?

  @__fields__
end

#from_dict(d) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/crystalline/metadata_fields.rb', line 76

def from_dict(d)
  to_build = {}

  # Collect lookup keys for regular fields so we can identify additional properties later
  known_keys = {}
  additional_props_fields = []
  fields.each do |field|
     = field..fetch(:format_json, {})
    if .fetch(:additional_properties, false)
      additional_props_fields << field
    else
      lookup = .fetch(:letter_case, nil).call
      known_keys[lookup] = true
    end
  end

  # Process regular fields
  (fields - additional_props_fields).each do |field|
    key = field.name
     = field..fetch(:format_json, {})
    field_type = field.type

    lookup = .fetch(:letter_case, nil).call
    value = d[lookup]
    if ::Crystalline::Utils.nilable? field_type
      if value == 'null'
        to_build[key] = nil
        next
      end
      field_type = ::Crystalline::Utils.nilable_of(field_type)
    end

    # If field is not nilable, and the value is not in the dict, raise a KeyError
    raise KeyError, "key #{lookup} not found in hash" if value.nil? && !::Crystalline::Utils.nilable?(field.type)
    # If field is nilable, and the value is not in the dict, just move to the next field
    next if value.nil?

    if field_type.is_a?(Crystalline::DiscriminatedUnion)
      to_build[key] = field_type.parse(value)
    elsif Crystalline::Utils.arr? field_type
      inner_type = Crystalline::Utils.arr_of(field_type)
      unmarshalled_array = value.map { |f| unmarshal_single(inner_type, f, ) }
      to_build[key] = unmarshalled_array
    elsif Crystalline::Utils.hash? field_type
      val_type = Crystalline::Utils.hash_of(field_type)

      # rubocop:disable Style/HashTransformValues
      unmarshalled_hash = value.map { |k, v| [k, unmarshal_single(val_type, v, )] }.to_h
      # rubocop:enable Style/HashTransformValues
      to_build[key] = unmarshalled_hash
    elsif Crystalline::Utils.union? field_type
      discriminator = field..fetch(:discriminator, nil)
      discriminator_mapping = field..fetch(:discriminator_mapping, nil)
      if !discriminator.nil?
        discriminator_value = value.fetch(discriminator)
        if !discriminator_mapping.nil?
          # Use explicit mapping from discriminator value to type
          type_to_deserialize = discriminator_mapping[discriminator_value]
        else
          # Fallback: try to match discriminator value against type name
          type_to_deserialize = Crystalline::Utils.get_union_types(field_type).find { |t| t.name.split('::').last == discriminator_value }
        end
        to_build[key] = Crystalline.unmarshal_json(value, type_to_deserialize)
      else
        union_types = Crystalline::Utils.get_union_types(field_type)
        union_types = union_types.sort_by { |klass| Crystalline.non_nilable_attr_count(klass) }

        if Crystalline.union_strategy == :populated_fields
          to_build[key] = Crystalline.unmarshal_union_populated_fields(value, union_types)
        else
          union_types.each do |union_type|
            begin
              to_build[key] = Crystalline.unmarshal_json(value, union_type)
            rescue TypeError
              next
            rescue NoMethodError
              next
            rescue KeyError
              next
            end
            break
          end
        end
      end
    elsif field_type.instance_of?(Class) && field_type.include?(::Crystalline::MetadataFields)
      to_build[key] = Crystalline.unmarshal_json(value, field_type)
    else
      to_build[key] = unmarshal_single(field_type, value, )
    end
  end

  # Process additional properties fields: collect remaining keys from the dict
  additional_props_fields.each do |field|
    key = field.name
     = field..fetch(:format_json, {})
    field_type = field.type

    remaining = d.reject { |k, _| known_keys.key?(k) }
    if remaining.empty?
      next if ::Crystalline::Utils.nilable?(field.type)

      raise KeyError, 'no additional properties found in hash'
    end

    inner_field_type = field_type
    inner_field_type = ::Crystalline::Utils.nilable_of(inner_field_type) if ::Crystalline::Utils.nilable?(inner_field_type)
    if Crystalline::Utils.hash?(inner_field_type)
      val_type = Crystalline::Utils.hash_of(inner_field_type)
      # rubocop:disable Style/HashTransformValues
      to_build[key] = remaining.map { |k, v| [k, unmarshal_single(val_type, v, )] }.to_h
      # rubocop:enable Style/HashTransformValues
    else
      to_build[key] = remaining
    end
  end

  new(**to_build)
end

#unmarshal_single(field_type, value, format_metadata = nil) ⇒ 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
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/crystalline/metadata_fields.rb', line 37

def unmarshal_single(field_type, value,  = nil)
  decoder = .fetch(:decoder, nil)

  # Delegate complex Crystalline types to unmarshal_json
  if field_type.is_a?(Crystalline::DiscriminatedUnion) ||
     Crystalline::Utils.arr?(field_type) ||
     Crystalline::Utils.hash?(field_type) ||
     Crystalline::Utils.union?(field_type)
    return Crystalline.unmarshal_json(value, field_type)
  elsif field_type.instance_of?(Class) && field_type.include?(::Crystalline::MetadataFields)
    return field_type.from_dict(value)
  elsif field_type.to_s == 'Date'
    return Date.parse(value)
  elsif field_type.to_s == 'DateTime'
    return DateTime.parse(value)
  elsif field_type.to_s == 'Object'
    if value.is_a?(::String)
      trimmed = value.lstrip
      if trimmed.start_with?('{') || trimmed.start_with?('[')
        # rubocop:disable Lint/SuppressedException
        begin
          value = JSON.parse(value)
        rescue TypeError, JSON::ParserError
        end
        # rubocop:enable Lint/SuppressedException
      end
    end
    return value
  elsif field_type.to_s == 'Float'
    return value.to_f

  end
  if decoder.nil?
    value
  else
    decoder.call(value)
  end
end