Skip to content

Commit

Permalink
Fix PreserveTypes behavior on non–nested data types
Browse files Browse the repository at this point in the history
Restoring type shouldn't rely on stored argument (data). It should
solely rely on type that was persisted in metadata.

E.g. using OpenStruct for data failed before this change, see:

  #1334 (comment)

As a sideeffect, two obsolete conditionals could be removed.

Co-authored-by: Łukasz Reszke <[email protected]>
Co-authored-by: Paweł Pacana <[email protected]>
  • Loading branch information
3 people committed Jul 19, 2023
1 parent 8b85a04 commit 3c5d928
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def load(record)
end
data_types = types&.fetch(:data, nil)
metadata_types = types&.fetch(:metadata, nil)
data = data_types ? restore_type(record.data, data_types) : record.data
metadata = metadata_types ? restore_type(record.metadata, metadata_types) : record.metadata
data = restore_type(record.data, data_types)
metadata = restore_type(record.metadata, metadata_types)

Record.new(
event_id: record.event_id,
Expand Down Expand Up @@ -151,7 +151,7 @@ def restore_types(argument, types)
end

def restore_type(argument, type)
case argument
case type
when Hash
restore_types(argument, type)
when Array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,27 @@ module Transformation
ensure
Time.zone = current_tz
end

specify "preserves OpenStruct data type passed by stored type lambda" do
transformation = PreserveTypes.new.register(
OpenStruct,
serializer: ->(v) { v.to_h },
deserializer: ->(v) { OpenStruct.new(v) }
)
ostruct = OpenStruct.new(foo: "bar")
record =
Record.new(
event_id: uuid,
metadata: {},
data: ostruct,
event_type: "TestEvent",
timestamp: nil,
valid_at: nil
)

expect(transformation.dump(record).metadata[:types]).to eq({ data: "OpenStruct", metadata: {} })
expect(transformation.load(transformation.dump(record))).to eq(record)
end
end
end
end
Expand Down

0 comments on commit 3c5d928

Please sign in to comment.