Class: RubyReactor::ContextSerializer
- Inherits:
-
Object
- Object
- RubyReactor::ContextSerializer
- Defined in:
- lib/ruby_reactor/context_serializer.rb
Overview
Utility class for handling context serialization and deserialization
Constant Summary collapse
- MAX_CONTEXT_SIZE =
512MB Redis limit
512 * 1024 * 1024
- SCHEMA_VERSION =
"1.0"
Class Method Summary collapse
- .deserialize(serialized_data) ⇒ Object
- .deserialize_value(value) ⇒ Object
- .serialize(context, job_id: nil, started_at: nil) ⇒ Object
-
.serialize_value(value) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength.
-
.simplify_for_api(value) ⇒ Object
Simplifies data for public API usage (removes wrappers, flattens types).
Class Method Details
.deserialize(serialized_data) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ruby_reactor/context_serializer.rb', line 20 def deserialize(serialized_data) decompressed = decompress_if_needed(serialized_data) data = JSON.parse(decompressed, symbolize_names: false) validate_schema_version(data) Context.deserialize_from_retry(data) rescue JSON::ParserError => e raise RubyReactor::Error::DeserializationError, "Failed to parse serialized context: #{e.}" end |
.deserialize_value(value) ⇒ Object
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 |
# File 'lib/ruby_reactor/context_serializer.rb', line 104 def deserialize_value(value) case value when Hash if value.key?("_type") # Special serialized types (Time, BigDecimal, etc.) case value["_type"] when "Success" RubyReactor::Success(deserialize_value(value["value"])) when "Failure" RubyReactor::Failure.new( deserialize_value(value["error"]), retryable: value["retryable"], step_name: value["step_name"], inputs: deserialize_value(value["inputs"]), backtrace: value["backtrace"], reactor_name: value["reactor_name"], step_arguments: deserialize_value(value["step_arguments"]), exception_class: value["exception_class"], file_path: value["file_path"], line_number: value["line_number"], code_snippet: deserialize_value(value["code_snippet"]), validation_errors: deserialize_value(value["validation_errors"]) ) when "Context" Context.deserialize_from_retry(value["value"]) when "Symbol" value["value"].to_sym when "Time" Time.iso8601(value["value"]) when "BigDecimal" BigDecimal(value["value"]) when "Rational" Rational(value["numerator"], value["denominator"]) when "Date" Date.iso8601(value["value"]) when "DateTime" DateTime.iso8601(value["value"]) when "Complex" Complex(value["real"], value["imag"]) when "Range" Range.new(deserialize_value(value["begin"]), deserialize_value(value["end"]), value["exclude_end"]) when "Regexp" Regexp.new(value["source"], value["options"]) when "GlobalID" GlobalID::Locator.locate(value["gid"]) when "Template::Element" RubyReactor::Template::Element.new(value["map_name"], value["path"]) when "Template::Input" RubyReactor::Template::Input.new(value["name"], value["path"]) when "Template::Value" RubyReactor::Template::Value.new(deserialize_value(value["value"])) when "Template::Result" RubyReactor::Template::Result.new(value["step_name"], value["path"]) when "Map::ResultEnumerator" RubyReactor::Map::ResultEnumerator.new( value["map_id"], value["reactor_class_name"], strict_ordering: value["strict_ordering"], batch_size: value["batch_size"] ) else # Unknown type wrapper, return as is (but deserialize values) value.transform_values { |v| deserialize_value(v) } end else # Regular Hash value.transform_keys(&:to_sym).transform_values { |v| deserialize_value(v) } end when Array value.map { |v| deserialize_value(v) } else value end end |
.serialize(context, job_id: nil, started_at: nil) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/ruby_reactor/context_serializer.rb', line 10 def serialize(context, job_id: nil, started_at: nil) data = context.serialize_for_retry(job_id: job_id, started_at: started_at) data[:schema_version] = SCHEMA_VERSION serialized = JSON.generate(data) validate_size(serialized) compress_if_needed(serialized) end |
.serialize_value(value) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
32 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 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ruby_reactor/context_serializer.rb', line 32 def serialize_value(value) case value when RubyReactor::Success { "_type" => "Success", "value" => serialize_value(value.value) } when RubyReactor::Failure { "_type" => "Failure", "error" => serialize_value(value.error), "retryable" => value.retryable, "step_name" => value.step_name, "inputs" => serialize_value(value.inputs), "backtrace" => value.backtrace, "reactor_name" => value.reactor_name, "step_arguments" => serialize_value(value.step_arguments), "exception_class" => value.exception_class, "file_path" => value.file_path, "line_number" => value.line_number, "code_snippet" => serialize_value(value.code_snippet), "validation_errors" => serialize_value(value.validation_errors) } when RubyReactor::Context { "_type" => "Context", "value" => value.serialize_for_retry } when Symbol { "_type" => "Symbol", "value" => value.to_s } when Time { "_type" => "Time", "value" => value.iso8601 } when BigDecimal { "_type" => "BigDecimal", "value" => value.to_s("F") } when Rational { "_type" => "Rational", "numerator" => value.numerator, "denominator" => value.denominator } when Date { "_type" => "Date", "value" => value.iso8601 } when DateTime { "_type" => "DateTime", "value" => value.iso8601 } when Complex { "_type" => "Complex", "real" => value.real, "imag" => value.imag } when Range { "_type" => "Range", "begin" => serialize_value(value.begin), "end" => serialize_value(value.end), "exclude_end" => value.exclude_end? } when Regexp { "_type" => "Regexp", "source" => value.source, "options" => value. } when ->(v) { v.respond_to?(:to_global_id) } { "_type" => "GlobalID", "gid" => value.to_global_id.to_s } when RubyReactor::Template::Element { "_type" => "Template::Element", "map_name" => value.map_name.to_s, "path" => value.path } when RubyReactor::Template::Input { "_type" => "Template::Input", "name" => value.name.to_s, "path" => value.path } when RubyReactor::Template::Value # Actually Template::Value holds a raw value. We should probably just serialize the raw value if possible, # or keep it as a template if we need to distinguish. # But wait, Template::Value is used to wrap raw values in arguments. # If we serialize it, we should probably deserialize it back to Template::Value. { "_type" => "Template::Value", "value" => serialize_value(value.instance_variable_get(:@value)) } when RubyReactor::Template::Result { "_type" => "Template::Result", "step_name" => value.step_name.to_s, "path" => value.path } when RubyReactor::Map::ResultEnumerator { "_type" => "Map::ResultEnumerator", "map_id" => value.map_id, "reactor_class_name" => value.reactor_class_name, "strict_ordering" => value.strict_ordering, "batch_size" => value.batch_size } when Hash value.transform_keys(&:to_s).transform_values { |v| serialize_value(v) } when Array value.map { |v| serialize_value(v) } else value end end |
.simplify_for_api(value) ⇒ Object
Simplifies data for public API usage (removes wrappers, flattens types)
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ruby_reactor/context_serializer.rb', line 181 def simplify_for_api(value) case value when Hash value.each_with_object({}) do |(k, v), hash| hash[k.to_s] = simplify_for_api(v) end when Array value.map { |v| simplify_for_api(v) } when Success, Failure, Context simplify_for_api(value.to_h) when Symbol value.to_s else value end end |