formatting

This commit is contained in:
Yanick Champoux 2023-11-13 09:49:27 -05:00
parent 83e2f01add
commit 250bd940da

View File

@ -73,9 +73,7 @@ sub serialize ( $self, $data, $extra_data = {} ) {
sub dedupe_included { sub dedupe_included {
my %seen; my %seen;
return grep { return grep { not $seen{ $_->{type} }{ $_->{id} }++ } @_;
not $seen{$_->{type}}{$_->{id}}++
} @_;
} }
=head2 serialize_data($data,$extra_data) =head2 serialize_data($data,$extra_data)
@ -86,14 +84,17 @@ Serializes the inner C<$data>.
sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) { sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) {
return [ map { $self->serialize_data($_,$extra_data, $included) } @$data ] if ref $data eq 'ARRAY'; return [ map { $self->serialize_data( $_, $extra_data, $included ) }
@$data ]
if ref $data eq 'ARRAY';
# it's a scalar? it's the id # it's a scalar? it's the id
return { id => $data, type => $self->type } unless ref $data; return { id => $data, type => $self->type } unless ref $data;
my $s = { my $s = {
type => $self->type, type => $self->type,
id => $self->gen_id($data) }; id => $self->gen_id($data)
};
if ( $self->links ) { if ( $self->links ) {
$s->{links} = gen_links( $self->links, $data, $extra_data ); $s->{links} = gen_links( $self->links, $data, $extra_data );
@ -108,14 +109,15 @@ sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) {
my @inc; my @inc;
my $t = $self->registry->serialize( my $t = $self->registry->serialize( $relationships{$key}{type},
$relationships{$key}{type}, $attr, \@inc $attr, \@inc );
);
$s->{relationships}{$key}{data} = obj_ref( $t->{data}, \@inc ); $s->{relationships}{$key}{data} = obj_ref( $t->{data}, \@inc );
if ( my $links = $relationships{$key}{links} ) { if ( my $links = $relationships{$key}{links} ) {
$s->{relationships}{$key}{links} = gen_links($links,$s->{relationships}{ $key }{data}, $extra_data ); $s->{relationships}{$key}{links} =
gen_links( $links, $s->{relationships}{$key}{data},
$extra_data );
} }
push @$included, @inc if $included; push @$included, @inc if $included;
@ -124,8 +126,9 @@ sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) {
delete $s->{attributes} unless $s->{attributes}->%*; delete $s->{attributes} unless $s->{attributes}->%*;
if ( $self->allowed_attributes ) { if ( $self->allowed_attributes ) {
delete $s->{attributes}{$_} for ( delete $s->{attributes}{$_}
set( keys $s->{attributes}->%* ) - set($self->allowed_attributes->@* ) )->@*; for ( set( keys $s->{attributes}->%* ) -
set( $self->allowed_attributes->@* ) )->@*;
} }
return $s; return $s;
@ -133,7 +136,8 @@ sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) {
} }
sub obj_ref ( $data, $included ) { sub obj_ref ( $data, $included ) {
return [ map { obj_ref($_,$included) } @$data ] if ref $data eq 'ARRAY'; return [ map { obj_ref( $_, $included ) } @$data ]
if ref $data eq 'ARRAY';
return $data if keys %$data == 2; return $data if keys %$data == 2;
@ -152,9 +156,7 @@ sub gen_links($links,$data,$extra_data={}) {
return $links->( $data, $extra_data ) if ref $links eq 'CODE'; return $links->( $data, $extra_data ) if ref $links eq 'CODE';
return { return { pairmap { $a => gen_item( $b, $data, $extra_data ) } %$links };
pairmap { $a => gen_item($b, $data,$extra_data) } %$links
};
} }
sub gen_item ( $item, $data, $extra_data ) { sub gen_item ( $item, $data, $extra_data ) {