module T::Props::Serializable
def __t_props_generated_deserialize(hash)
def __t_props_generated_deserialize(hash) p; will be overridden if there are any props. ee the definition for class `Foo`, run `Foo.decorator.send(:generate_deserialize_source)`
def __t_props_generated_serialize(strict)
def __t_props_generated_serialize(strict) p; will be overridden if there are any props. ee the definition for class `Foo`, run `Foo.decorator.send(:generate_serialize_source)`
def deserialize(hash, strict=false)
-
(void)
-
Parameters:
-
strict
(T::Boolean
) -- (false) If true, raise an exception if -
hash
(Hash
) -- The hash to take property
def deserialize(hash, strict=false) begin hash_keys_matching_props = __t_props_generated_deserialize(hash) rescue => e msg = self.class.decorator.message_with_generated_source_context( e, :__t_props_generated_deserialize, :generate_deserialize_source ) if msg begin raise e.class.new(msg) rescue ArgumentError raise TypeError.new(msg) end else raise end end if hash.size > hash_keys_matching_props serialized_forms = self.class.decorator.prop_by_serialized_forms extra = hash.reject {|k, _| serialized_forms.key?(k)} # `extra` could still be empty here if the input matches a `dont_store` prop; # historically, we just ignore those if !extra.empty? if strict raise "Unknown properties for #{self.class.name}: #{extra.keys.inspect}" else @_extra_props = extra end end end end
def raise_deserialization_error(prop_name, value, orig_error)
def raise_deserialization_error(prop_name, value, orig_error) figuration.soft_assert_handler( erialization error (probably unexpected stored type)', ytime: { ass: self.class, op: prop_name, lue: value, ror: orig_error.message, tify: 'djudd'
def recursive_stringify_keys(obj)
def recursive_stringify_keys(obj) .is_a?(Hash) obj = obj.class.new each do |k, v| w_obj[k.to_s] = recursive_stringify_keys(v) obj.is_a?(Array) obj = obj.map {|v| recursive_stringify_keys(v)} obj = obj j
def required_prop_missing_from_deserialize(prop)
def required_prop_missing_from_deserialize(prop) ired_props_missing_from_deserialize ||= Set[] ired_props_missing_from_deserialize << prop
def required_prop_missing_from_serialize(prop)
def required_prop_missing_from_serialize(prop) ined?(@_required_props_missing_from_deserialize) && equired_props_missing_from_deserialize&.include?(prop) the prop was already missing during deserialization, that means the application de already had to deal with a nil value, which means we wouldn't be accomplishing ch by raising here (other than causing an unnecessary breakage). onfiguration.log_info_handler( halk-odm: missing required property in serialize", op: prop, class: self.class.name, id: self.class.decorator.get_id(self) e TypeError.new("#{self.class.name}.#{prop} not set for non-optional prop")
def serialize(strict=true)
-
(Hash)
- A serialization of this object.
Parameters:
-
strict
(T::Boolean
) -- (true) If false, do not raise an
def serialize(strict=true) begin h = __t_props_generated_serialize(strict) rescue => e msg = self.class.decorator.message_with_generated_source_context( e, :__t_props_generated_serialize, :generate_serialize_source ) if msg begin raise e.class.new(msg) rescue ArgumentError raise TypeError.new(msg) end else raise end end h.merge!(@_extra_props) if defined?(@_extra_props) h end
def with(changed_props)
def with(changed_props) with_existing_hash(changed_props, existing_hash: self.serialize) end
def with_existing_hash(changed_props, existing_hash:)
def with_existing_hash(changed_props, existing_hash:) ized = existing_hash l = self.class.from_hash(serialized.merge(recursive_stringify_keys(changed_props))) tra = self.instance_variable_get(:@_extra_props) if self.instance_variable_defined?(:@_extra_props) tra = new_val.instance_variable_get(:@_extra_props) if new_val.instance_variable_defined?(:@_extra_props) _extra != new_extra erence = old_extra new_extra.reject {|k, v| old_extra[k] == v} se new_extra d e ArgumentError.new("Unexpected arguments: input(#{changed_props}), unexpected(#{difference})") l