formatting
This commit is contained in:
parent
83e2f01add
commit
250bd940da
@ -4,7 +4,7 @@ package Dancer2::Plugin::JsonApi::Registry::Schema;
|
|||||||
|
|
||||||
use Moo;
|
use Moo;
|
||||||
|
|
||||||
use experimental qw/ signatures /;
|
use experimental qw/ signatures /;
|
||||||
use List::AllUtils qw/ pairmap pairgrep /;
|
use List::AllUtils qw/ pairmap pairgrep /;
|
||||||
|
|
||||||
use Set::Object qw/set/;
|
use Set::Object qw/set/;
|
||||||
@ -34,13 +34,13 @@ has id => (
|
|||||||
default => 'id'
|
default => 'id'
|
||||||
);
|
);
|
||||||
|
|
||||||
has links => (is => 'ro');
|
has links => ( is => 'ro' );
|
||||||
has top_level_links => (is => 'ro');
|
has top_level_links => ( is => 'ro' );
|
||||||
has top_level_meta => (is => 'ro');
|
has top_level_meta => ( is => 'ro' );
|
||||||
has relationships => (is => 'ro', default => sub { +{} });
|
has relationships => ( is => 'ro', default => sub { +{} } );
|
||||||
|
|
||||||
has registry => ( is => 'ro' );
|
has registry => ( is => 'ro' );
|
||||||
has allowed_attributes => ( is => 'ro');
|
has allowed_attributes => ( is => 'ro' );
|
||||||
|
|
||||||
=head1 METHODS
|
=head1 METHODS
|
||||||
|
|
||||||
@ -59,23 +59,21 @@ sub serialize ( $self, $data, $extra_data = {} ) {
|
|||||||
|
|
||||||
my @included;
|
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)
|
$serial->{links} = gen_links( $self->top_level_links, $data, $extra_data )
|
||||||
if $self->top_level_links;
|
if $self->top_level_links;
|
||||||
$serial->{meta} = gen_links($self->top_level_meta,$data,$extra_data)
|
$serial->{meta} = gen_links( $self->top_level_meta, $data, $extra_data )
|
||||||
if $self->top_level_meta;
|
if $self->top_level_meta;
|
||||||
|
|
||||||
$serial->{included} = [ dedupe_included( @included )] if @included;
|
$serial->{included} = [ dedupe_included(@included) ] if @included;
|
||||||
|
|
||||||
return $serial;
|
return $serial;
|
||||||
}
|
}
|
||||||
|
|
||||||
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,17 +84,20 @@ 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 );
|
||||||
}
|
}
|
||||||
|
|
||||||
$s->{attributes} = +{ pairgrep { $a ne $self->id } %$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 @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;
|
||||||
@ -123,17 +125,19 @@ 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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
@ -142,25 +146,23 @@ sub obj_ref($data, $included) {
|
|||||||
return +{ $data->%{qw/ id type/} };
|
return +{ $data->%{qw/ id type/} };
|
||||||
}
|
}
|
||||||
|
|
||||||
sub gen_id($self,$data) {
|
sub gen_id ( $self, $data ) {
|
||||||
my $id = $self->id;
|
my $id = $self->id;
|
||||||
|
|
||||||
return ref $id ? $id->($data) : $data->{$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 {
|
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 ) {
|
||||||
return $item unless ref $item;
|
return $item unless ref $item;
|
||||||
|
|
||||||
return $item->($data,$extra_data);
|
return $item->( $data, $extra_data );
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
Loading…
Reference in New Issue
Block a user