Module: T::Props::Private::DeserializerGenerator
- Extended by:
- Sig
- Defined in:
- lib/types/props/private/deserializer_generator.rb
Overview
Generates a specialized deserialize implementation for a subclass of
T::Props::Serializable.
The basic idea is that we analyze the props and for each prop, generate the simplest possible logic as a block of Ruby source, so that we don't pay the cost of supporting types like T:::Hash[CustomType, SubstructType] when deserializing a simple Integer. Then we join those together, with a little shared logic to be able to detect when we get input keys that don't match any prop.
Constant Summary collapse
Class Method Summary collapse
Methods included from Sig
Methods included from T::Private::Methods::SingletonMethodHooks
Methods included from T::Private::Methods::MethodHooks
Class Method Details
.generate(props, defaults) ⇒ Object
33 34 35 36 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/types/props/private/deserializer_generator.rb', line 33 def self.generate(props, defaults) parts = props.filter_map do |prop, rules| next if rules[:dont_store] # All of these strings should already be validated (directly or # indirectly) in `validate_prop_name`, so we don't bother with a nice # error message, but we double check here to prevent a refactoring # from introducing a security vulnerability. raise unless T::Props::Decorator::SAFE_NAME.match?(CAN_USE_SYMBOL_NAME ? prop.name : prop.to_s) hash_key = rules.fetch(:serialized_form) raise unless T::Props::Decorator::SAFE_NAME.match?(hash_key) key = rules.fetch(:accessor_key) ivar_name = CAN_USE_SYMBOL_NAME ? key.name : key.to_s raise unless ivar_name.start_with?('@') && T::Props::Decorator::SAFE_ACCESSOR_KEY_NAME.match?(ivar_name) transformation = SerdeTransform.generate( T::Utils::Nilable.(rules.fetch(:type_object)), SerdeTransform::Mode::DESERIALIZE, 'val' ) transformed_val = transformation || 'val' nil_handler = generate_nil_handler( prop: prop, serialized_form: hash_key, default: defaults[prop], nilable_type: T::Props::Utils.optional_prop?(rules), raise_on_nil_write: !!rules[:raise_on_nil_write], ) # The `.freeze` on the `hash.key?` argument matters: when this source # is eval'd lazily (without a frozen-string-literal prefix, unlike # `eagerly_define_lazy_methods!`), a bare literal here would allocate # a new String on every call for each nil/missing prop. `"str".freeze` # compiles to a no-allocation VM instruction (opt_str_freeze). The # `hash[...]` read doesn't need it because of opt_aref_with. <<~RUBY val = hash[#{hash_key.inspect}] #{ivar_name} = if val.nil? found -= 1 unless hash.key?(#{hash_key.inspect}.freeze) #{nil_handler} else #{transformed_val} end RUBY end <<~RUBY def __t_props_generated_deserialize(hash) found = #{parts.size} #{parts.join("\n\n")} found end RUBY end |