Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to dynamically skip a field by returning SKIP #92

Merged
merged 1 commit into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions ext/panko_serializer/panko_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ static ID to_a_id;
static ID object_id;
static ID serialization_context_id;

static VALUE SKIP = Qundef;

void write_value(VALUE str_writer, VALUE key, VALUE value, VALUE isJson) {
if(isJson == Qtrue) {
rb_funcall(str_writer, push_json_id, 2, value, key);
Expand Down Expand Up @@ -40,8 +42,9 @@ void serialize_method_fields(VALUE object, VALUE str_writer,
Attribute attribute = PANKO_ATTRIBUTE_READ(raw_attribute);

volatile VALUE result = rb_funcall(serializer, attribute->name_id, 0);

write_value(str_writer, attribute->name_str, result, Qfalse);
if (result != SKIP) {
write_value(str_writer, attribute->name_str, result, Qfalse);
}
}

rb_ivar_set(serializer, object_id, Qnil);
Expand Down Expand Up @@ -167,6 +170,10 @@ void Init_panko_serializer() {
rb_define_singleton_method(mPanko, "serialize_objects", serialize_objects_api,
3);

VALUE mPankoSerializer = rb_const_get(mPanko, rb_intern("Serializer"));
SKIP = rb_const_get(mPankoSerializer, rb_intern("SKIP"));
rb_global_variable(&SKIP);

panko_init_serialization_descriptor(mPanko);
init_attributes_writer(mPanko);
panko_init_type_cast(mPanko);
Expand Down
2 changes: 2 additions & 0 deletions lib/panko/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def context

module Panko
class Serializer
SKIP = Object.new.freeze

class << self
def inherited(base)
if _descriptor.nil?
Expand Down
14 changes: 14 additions & 0 deletions spec/panko/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@ class FooWithAliasesSerializer < Panko::Serializer
expect(Foo.create).to serialized_as(FooSerializer, "name" => nil, "address" => nil)
end

it "can skip fields" do
class FooSkipSerializer < FooSerializer
def address
object.address || SKIP
end
end

foo = Foo.create(name: Faker::Lorem.word, address: Faker::Lorem.word).reload
expect(foo).to serialized_as(FooSkipSerializer, "name" => foo.name, "address" => foo.address)

foo = Foo.create(name: Faker::Lorem.word, address: nil).reload
expect(foo).to serialized_as(FooSkipSerializer, "name" => foo.name)
end

it "preserves changed attributes" do
foo = Foo.create(name: Faker::Lorem.word, address: Faker::Lorem.word).reload

Expand Down