2023-10-31 14:39:35 +00:00
|
|
|
package Dancer2::Plugin::JsonApi::Registry;
|
|
|
|
|
|
|
|
use 5.32.0;
|
2023-11-13 18:38:56 +00:00
|
|
|
use Dancer2::Plugin::JsonApi::Schema;
|
2023-10-31 16:17:28 +00:00
|
|
|
|
|
|
|
use Carp;
|
2023-10-31 14:39:35 +00:00
|
|
|
|
|
|
|
use Moo;
|
|
|
|
|
|
|
|
use experimental qw/ signatures /;
|
|
|
|
|
2023-11-10 20:30:25 +00:00
|
|
|
sub serialize ( $self, $type, $data, $extra_data = {} ) {
|
|
|
|
return $self->type($type)->serialize( $data, $extra_data );
|
2023-10-31 14:39:35 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 18:25:43 +00:00
|
|
|
sub deserialize ( $self, $data, $included = [] ) {
|
|
|
|
|
|
|
|
my $type =
|
|
|
|
ref $data->{data} eq 'ARRAY'
|
|
|
|
? $data->{data}[0]->{type}
|
|
|
|
: $data->{data}{type};
|
|
|
|
|
|
|
|
return $self->type($type)->deserialize( $data, $included );
|
|
|
|
}
|
|
|
|
|
2023-10-31 16:17:28 +00:00
|
|
|
has types => (
|
2023-11-10 20:30:25 +00:00
|
|
|
is => 'ro',
|
2023-10-31 16:17:28 +00:00
|
|
|
default => sub { +{} },
|
|
|
|
);
|
|
|
|
|
2023-11-13 15:38:51 +00:00
|
|
|
has app => ( is => 'ro', );
|
|
|
|
|
2023-11-10 20:30:25 +00:00
|
|
|
sub add_type ( $self, $type, $definition = {} ) {
|
2023-11-13 18:38:56 +00:00
|
|
|
$self->{types}{$type} = Dancer2::Plugin::JsonApi::Schema->new(
|
2023-11-01 17:16:34 +00:00
|
|
|
registry => $self,
|
2023-11-10 20:30:25 +00:00
|
|
|
type => $type,
|
2023-10-31 16:17:28 +00:00
|
|
|
%$definition
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-10 20:30:25 +00:00
|
|
|
sub type ( $self, $type ) {
|
|
|
|
return $self->types->{$type} //=
|
2023-11-13 18:38:56 +00:00
|
|
|
Dancer2::Plugin::JsonApi::Schema->new( type => $type );
|
2023-10-31 16:17:28 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 14:39:35 +00:00
|
|
|
1;
|
|
|
|
|
|
|
|
__END__
|