From 4d4c4ee83817a733232fbc730007421ddbff6e11 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Wed, 1 Nov 2023 11:03:11 -0400 Subject: [PATCH] rename serialize and top_level_serialize --- lib/Dancer2/Plugin/JsonApi/Registry.pm | 2 +- lib/Dancer2/Plugin/JsonApi/Registry/Schema.pm | 12 ++++++------ t/registry-schema.t | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/Dancer2/Plugin/JsonApi/Registry.pm b/lib/Dancer2/Plugin/JsonApi/Registry.pm index c50a136..3eed412 100644 --- a/lib/Dancer2/Plugin/JsonApi/Registry.pm +++ b/lib/Dancer2/Plugin/JsonApi/Registry.pm @@ -22,7 +22,7 @@ Returns the serialized form of C<$data>. =cut sub serialize($self,$type,$data,$extra_data={}) { - return $self->type($type)->top_level_serialize($data,$extra_data); + return $self->type($type)->serialize($data,$extra_data); } has types => ( diff --git a/lib/Dancer2/Plugin/JsonApi/Registry/Schema.pm b/lib/Dancer2/Plugin/JsonApi/Registry/Schema.pm index 047fefd..3261023 100644 --- a/lib/Dancer2/Plugin/JsonApi/Registry/Schema.pm +++ b/lib/Dancer2/Plugin/JsonApi/Registry/Schema.pm @@ -45,13 +45,13 @@ JSON:API object. =cut -sub top_level_serialize ( $self, $data, $extra_data = {} ) { +sub serialize ( $self, $data, $extra_data = {} ) { my $serial = {}; $serial->{jsonapi} = { version => '1.0' }; - $serial->{data} = $self->serialize($data,$extra_data); + $serial->{data} = $self->serialize_data($data,$extra_data); $serial->{links} = gen_links($self->top_level_links,$data,$extra_data) if $self->top_level_links; @@ -62,15 +62,15 @@ sub top_level_serialize ( $self, $data, $extra_data = {} ) { } -=head2 serialize($data,$extra_data) +=head2 serialize_data($data,$extra_data) -Serializes C<$data> as a JSON:API object. +Serializes the inner C<$data>. =cut -sub serialize ( $self, $data, $extra_data = {} ) { +sub serialize_data ( $self, $data, $extra_data = {} ) { - return [ map { $self->serialize($_,$extra_data) } @$data ] if ref $data eq 'ARRAY'; + return [ map { $self->serialize_data($_,$extra_data) } @$data ] if ref $data eq 'ARRAY'; my $s = { type => $self->type, diff --git a/t/registry-schema.t b/t/registry-schema.t index 00ade44..d0aa8cb 100644 --- a/t/registry-schema.t +++ b/t/registry-schema.t @@ -7,7 +7,7 @@ use experimental qw/ signatures /; my $type = Dancer2::Plugin::JsonApi::Registry::Schema->new( 'type' => 'thing' ); -like $type->top_level_serialize( { attr1 => 'a', id => '123' }, +like $type->serialize( { attr1 => 'a', id => '123' }, { foo => 1 } ) => { jsonapi => { version => '1.0' }, data => { type => 'thing', id => '123' } }; @@ -15,7 +15,7 @@ like $type->top_level_serialize( { attr1 => 'a', id => '123' }, is( Dancer2::Plugin::JsonApi::Registry::Schema->new( 'type' => 'thing', id => 'foo' - )->serialize( { foo => '123' } )->{id} => '123', + )->serialize( { foo => '123' } )->{data}{id} => '123', 'custom id' ); @@ -26,11 +26,11 @@ my $serialized = schema_serialize( }, { x => '1', y => '2' } ); -is( $serialized->{id} => '12', +is( $serialized->{data}{id} => '12', 'custom id, function' ); -like $serialized, { links => { self => '/some/url' } }, "links"; +like $serialized->{data}, { links => { self => '/some/url' } }, "links"; sub schema_serialize ( $schema, $data ) { return Dancer2::Plugin::JsonApi::Registry::Schema->new(%$schema) @@ -46,7 +46,7 @@ like( $xtra->{bar}; } } - )->top_level_serialize( {}, { bar => 'yup' } ), + )->serialize( {}, { bar => 'yup' } ), { meta => { foo => 1, bar => 'yup' } } ); @@ -56,7 +56,7 @@ subtest 'attributes' => sub { type => 'thing', )->serialize( { id => 1, foo => 'bar'}); - is $serialized => { + is $serialized->{data} => { type => 'thing', id => 1, attributes => {