module Rookout::Processor::Namespaces::RubyObjectSerializer
Constants
- INT32_MAX
- INT32_MIN
Based off protobuf for Python
- INT64_MAX
- INT64_MIN
Public Instance Methods
create_base_variant(obj, current_depth, config, log_object_errors)
click to toggle source
rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity
# File lib/rookout/processor/namespaces/ruby_object_serializer.rb, line 56 def create_base_variant obj, current_depth, config, log_object_errors variant = Com::Rookout::Variant.new original_type: obj.class.to_s object_weight = obj.instance_variables.length > config.max_width ? 2 : 1 next_depth = current_depth + object_weight if next_depth >= config.max_depth variant.max_depth = true return variant end obj.instance_variables.each do |name| raw_value = obj.instance_variable_get name variant_value = dump_raw_object raw_value, current_depth + 1, config, log_object_errors variant.attributes << Com::Rookout::Variant::NamedValue.new(name: name, value: variant_value) end variant end
dump_array(obj, variant, current_depth, config, log_object_errors)
click to toggle source
# File lib/rookout/processor/namespaces/ruby_object_serializer.rb, line 143 def dump_array obj, variant, current_depth, config, log_object_errors variant.variant_type = :VARIANT_LIST variant.list_value = Com::Rookout::Variant::List.new type: "array", original_size: obj.length return unless current_depth < config.max_collection_depth obj.each_with_index do |value, index| break if index >= config.max_width variant.list_value.values << dump_raw_object(value, current_depth + 1, config, log_object_errors) end end
dump_code_object(obj, variant)
click to toggle source
# File lib/rookout/processor/namespaces/ruby_object_serializer.rb, line 183 def dump_code_object obj, variant variant.variant_type = :VARIANT_CODE_OBJECT if obj.is_a?(Proc) || obj.is_a?(Method) source_location = obj.source_location else source_location = [nil, nil] end if obj.is_a? Proc name = "" else name = obj.name end variant.code_value = Com::Rookout::Variant::CodeObject.new name: name, filename: source_location[0], lineno: source_location[1] end
dump_exception(obj, variant, current_depth, config, log_object_errors)
click to toggle source
# File lib/rookout/processor/namespaces/ruby_object_serializer.rb, line 170 def dump_exception obj, variant, current_depth, config, log_object_errors variant.variant_type = :VARIANT_OBJECT message = dump_raw_object obj.message, current_depth + 1, config, log_object_errors variant.attributes << Com::Rookout::Variant::NamedValue.new(name: "message", value: message) cause = dump_raw_object obj.cause, current_depth + 1, config, log_object_errors variant.attributes << Com::Rookout::Variant::NamedValue.new(name: "cause", value: cause) backtrace = dump_raw_object obj.backtrace, current_depth + 1, config, log_object_errors variant.attributes << Com::Rookout::Variant::NamedValue.new(name: "backtrace", value: backtrace) end
dump_hash(obj, variant, current_depth, config, log_object_errors)
click to toggle source
# File lib/rookout/processor/namespaces/ruby_object_serializer.rb, line 155 def dump_hash obj, variant, current_depth, config, log_object_errors variant.variant_type = :VARIANT_MAP variant.map_value = Com::Rookout::Variant::Map.new original_size: obj.length return unless current_depth < config.max_collection_depth obj.each_with_index do |(key, value), index| break if index >= config.max_width key_variant = dump_raw_object key, current_depth + 1, config, log_object_errors value_variant = dump_raw_object value, current_depth + 1, config, log_object_errors pair = Com::Rookout::Variant::Pair.new first: key_variant, second: value_variant variant.map_value.pairs << pair end end
dump_integer(obj, variant)
click to toggle source
# File lib/rookout/processor/namespaces/ruby_object_serializer.rb, line 112 def dump_integer obj, variant if obj > INT32_MIN && obj < INT32_MAX variant.variant_type = :VARIANT_INT variant.int_value = obj elsif obj > INT64_MIN && obj < INT64_MAX variant.variant_type = :VARIANT_LONG variant.long_value = obj else variant.variant_type = :VARIANT_LARGE_INT variant.large_int_value = Com::Rookout::Variant::LargeInt.new value: obj.to_s end end
dump_nil(variant)
click to toggle source
# File lib/rookout/processor/namespaces/ruby_object_serializer.rb, line 75 def dump_nil variant variant.variant_type = :VARIANT_NONE end
dump_numeric(obj, variant)
click to toggle source
# File lib/rookout/processor/namespaces/ruby_object_serializer.rb, line 85 def dump_numeric obj, variant if obj == true variant.variant_type = :VARIANT_INT variant.int_value = 1 elsif obj == false variant.variant_type = :VARIANT_INT variant.int_value = 0 elsif obj.is_a? Integer dump_integer obj, variant elsif obj.is_a? Float variant.variant_type = :VARIANT_DOUBLE variant.double_value = obj.to_f elsif obj.is_a? BigDecimal serialized_decimal = obj.to_s variant.variant_type = :VARIANT_STRING variant.string_value = Com::Rookout::Variant::String.new value: serialized_decimal, original_size: serialized_decimal.length elsif obj.is_a? Complex variant.variant_type = :VARIANT_COMPLEX variant.complex_value = Com::Rookout::Variant::Complex.new real: obj.real.to_f, imaginary: obj.imaginary.to_f else raise Exceptions::RookClassCannotBeSerialized.new(obj.class, "Unknown Numeric Type") end # TODO: ADD SUPPORT FOR RATIONALS end
dump_raw_object(obj, current_depth, config, log_object_errors)
click to toggle source
# File lib/rookout/processor/namespaces/ruby_object_serializer.rb, line 10 def dump_raw_object obj, current_depth, config, log_object_errors unsafe_dump_object obj, current_depth, config, log_object_errors rescue StandardError => e message = "Failed to serialize object" variant = Com::Rookout::Variant.new variant_type: :VARIANT_ERROR if log_object_errors Logger.instance.exception message, e error = RookError.new e, message variant.error_value = error.dumps end variant end
dump_string(obj, variant, config)
click to toggle source
# File lib/rookout/processor/namespaces/ruby_object_serializer.rb, line 125 def dump_string obj, variant, config obj = obj.to_s if obj.length > config.max_string final_obj = obj[0...config.max_string] else final_obj = obj end variant.variant_type = :VARIANT_STRING variant.string_value = Com::Rookout::Variant::String.new value: final_obj, original_size: obj.length end
dump_time(obj, variant)
click to toggle source
# File lib/rookout/processor/namespaces/ruby_object_serializer.rb, line 137 def dump_time obj, variant variant.variant_type = :VARIANT_TIME variant.time_value = Google::Protobuf::Timestamp.new variant.time_value.from_time obj end
dump_user_class(variant)
click to toggle source
# File lib/rookout/processor/namespaces/ruby_object_serializer.rb, line 203 def dump_user_class variant variant.variant_type = :VARIANT_OBJECT end
unsafe_dump_object(obj, current_depth, config, log_object_errors)
click to toggle source
rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/CyclomaticComplexity
# File lib/rookout/processor/namespaces/ruby_object_serializer.rb, line 27 def unsafe_dump_object obj, current_depth, config, log_object_errors variant = create_base_variant obj, current_depth, config, log_object_errors if obj.nil? dump_nil variant elsif obj.is_a?(Numeric) || obj.is_a?(TrueClass) || obj.is_a?(FalseClass) dump_numeric obj, variant elsif obj.is_a?(String) || obj.is_a?(Symbol) dump_string obj, variant, config elsif obj.is_a? Time dump_time obj, variant elsif obj.class == Array dump_array obj, variant, current_depth, config, log_object_errors elsif obj.class == Hash dump_hash obj, variant, current_depth, config, log_object_errors elsif obj.is_a? Exception dump_exception obj, variant, current_depth, config, log_object_errors elsif obj.is_a?(Method) || obj.is_a?(Proc) || obj.is_a?(Class) || obj.is_a?(Module) dump_code_object obj, variant else dump_user_class variant end variant end