formatting

releases
Yanick Champoux 2023-11-13 09:49:27 -05:00
parent 83e2f01add
commit 250bd940da
1 changed files with 41 additions and 39 deletions

View File

@ -4,7 +4,7 @@ package Dancer2::Plugin::JsonApi::Registry::Schema;
use Moo;
use experimental qw/ signatures /;
use experimental qw/ signatures /;
use List::AllUtils qw/ pairmap pairgrep /;
use Set::Object qw/set/;
@ -34,13 +34,13 @@ has id => (
default => 'id'
);
has links => (is => 'ro');
has top_level_links => (is => 'ro');
has top_level_meta => (is => 'ro');
has relationships => (is => 'ro', default => sub { +{} });
has links => ( is => 'ro' );
has top_level_links => ( is => 'ro' );
has top_level_meta => ( is => 'ro' );
has relationships => ( is => 'ro', default => sub { +{} } );
has registry => ( is => 'ro' );
has allowed_attributes => ( is => 'ro');
has registry => ( is => 'ro' );
has allowed_attributes => ( is => 'ro' );
=head1 METHODS
@ -59,23 +59,21 @@ sub serialize ( $self, $data, $extra_data = {} ) {
my @included;
$serial->{data} = $self->serialize_data($data,$extra_data,\@included);
$serial->{data} = $self->serialize_data( $data, $extra_data, \@included );
$serial->{links} = gen_links($self->top_level_links,$data,$extra_data)
if $self->top_level_links;
$serial->{meta} = gen_links($self->top_level_meta,$data,$extra_data)
if $self->top_level_meta;
$serial->{links} = gen_links( $self->top_level_links, $data, $extra_data )
if $self->top_level_links;
$serial->{meta} = gen_links( $self->top_level_meta, $data, $extra_data )
if $self->top_level_meta;
$serial->{included} = [ dedupe_included( @included )] if @included;
$serial->{included} = [ dedupe_included(@included) ] if @included;
return $serial;
}
sub dedupe_included {
my %seen;
return grep {
not $seen{$_->{type}}{$_->{id}}++
} @_;
return grep { not $seen{ $_->{type} }{ $_->{id} }++ } @_;
}
=head2 serialize_data($data,$extra_data)
@ -86,17 +84,20 @@ Serializes the inner C<$data>.
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
return { id => $data, type => $self->type } unless ref $data;
my $s = {
type => $self->type,
id => $self->gen_id($data) };
id => $self->gen_id($data)
};
if($self->links) {
$s->{links} = gen_links($self->links,$data,$extra_data);
if ( $self->links ) {
$s->{links} = gen_links( $self->links, $data, $extra_data );
}
$s->{attributes} = +{ pairgrep { $a ne $self->id } %$data };
@ -108,14 +109,15 @@ sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) {
my @inc;
my $t = $self->registry->serialize(
$relationships{$key}{type}, $attr, \@inc
);
my $t = $self->registry->serialize( $relationships{$key}{type},
$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} ) {
$s->{relationships}{$key}{links} = gen_links($links,$s->{relationships}{ $key }{data}, $extra_data );
if ( my $links = $relationships{$key}{links} ) {
$s->{relationships}{$key}{links} =
gen_links( $links, $s->{relationships}{$key}{data},
$extra_data );
}
push @$included, @inc if $included;
@ -123,17 +125,19 @@ sub serialize_data ( $self, $data, $extra_data = {}, $included = undef ) {
delete $s->{attributes} unless $s->{attributes}->%*;
if( $self->allowed_attributes ) {
delete $s->{attributes}{$_} for (
set( keys $s->{attributes}->%* ) - set($self->allowed_attributes->@* ) )->@*;
if ( $self->allowed_attributes ) {
delete $s->{attributes}{$_}
for ( set( keys $s->{attributes}->%* ) -
set( $self->allowed_attributes->@* ) )->@*;
}
return $s;
}
sub obj_ref($data, $included) {
return [ map { obj_ref($_,$included) } @$data ] if ref $data eq 'ARRAY';
sub obj_ref ( $data, $included ) {
return [ map { obj_ref( $_, $included ) } @$data ]
if ref $data eq 'ARRAY';
return $data if keys %$data == 2;
@ -142,25 +146,23 @@ sub obj_ref($data, $included) {
return +{ $data->%{qw/ id type/} };
}
sub gen_id($self,$data) {
sub gen_id ( $self, $data ) {
my $id = $self->id;
return ref $id ? $id->($data) : $data->{$id};
}
sub gen_links($links,$data,$extra_data={}) {
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 {
pairmap { $a => gen_item($b, $data,$extra_data) } %$links
};
return { pairmap { $a => gen_item( $b, $data, $extra_data ) } %$links };
}
sub gen_item($item,$data,$extra_data) {
sub gen_item ( $item, $data, $extra_data ) {
return $item unless ref $item;
return $item->($data,$extra_data);
return $item->( $data, $extra_data );
}
1;