| 1 | package Dancer2::Serializer::Mutable; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Serialize and deserialize content based on HTTP header | ||||||||||||||||||||||||||||
| 3 | 4 | $Dancer2::Serializer::Mutable::VERSION = '2.3.0'; | |||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Module::Runtime 'require_module'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | with 'Dancer2::Core::Role::Serializer'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use constant DEFAULT_CONTENT_TYPE => 'application/json'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | has '+content_type' => ( default => DEFAULT_CONTENT_TYPE() ); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | my $serializer = { | ||||||||||||||||||||||||||||
| 15 | 'YAML' => { | ||||||||||||||||||||||||||||
| 16 | 17 | ▶ | to => sub { Dancer2::Core::DSL::to_yaml(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 8 | ▶ | from => sub { Dancer2::Core::DSL::from_yaml(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | }, | ||||||||||||||||||||||||||||
| 19 | 'JSON' => { | ||||||||||||||||||||||||||||
| 20 | 33 | ▶ | to => sub { Dancer2::Core::DSL::to_json(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 10 | ▶ | from => sub { Dancer2::Core::DSL::from_json(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | }, | ||||||||||||||||||||||||||||
| 23 | 4 | }; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | has enable_dumper => ( | ||||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||
| 27 | lazy => 1, | ||||||||||||||||||||||||||||
| 28 | 3 | ▶ | default => sub { $_[0]->config->{enable_dumper} || 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 4 | ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | has mapping => ( | ||||||||||||||||||||||||||||
| 32 | is => 'ro', | ||||||||||||||||||||||||||||
| 33 | lazy => 1, | ||||||||||||||||||||||||||||
| 34 | 4 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.8
| |||||||||||||||||||||||||||||
| 35 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 4 | ▶ | if ( my $mapping = $self->config->{mapping} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # initialize non-default serializers | ||||||||||||||||||||||||||||
| 40 | 2 | for my $s ( values %$mapping ) { | |||||||||||||||||||||||||||
| 41 | # TODO allow for arguments via the config | ||||||||||||||||||||||||||||
| 42 | 5 | ▶ | next if $serializer->{$s}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 2 | ▶ | if ( $s eq 'Dumper' && !$self->enable_dumper ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$s eq "Dumper" and not $self->enable_dumper
MC/DC50%1/2
$s eq "Dumper" and not $self->enable_dumper
$s eq "Dumper"
not $self->enable_dumper
Truth table66%2/3
A and not(B)
A= $s eq "Dumper"B= not $self->enable_dumper
| |||||||||||||||||||||||||||||
| 45 | 1 | croak | |||||||||||||||||||||||||||
| 46 | "The Dumper serializer is disabled. Set " | ||||||||||||||||||||||||||||
| 47 | . "engines.serializer.Mutable.enable_dumper to a " | ||||||||||||||||||||||||||||
| 48 | . "true value to use it. This requires the " | ||||||||||||||||||||||||||||
| 49 | . "Dancer2::Serializer::Dumper distribution to be " | ||||||||||||||||||||||||||||
| 50 | . "installed."; | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | $self->_load_serializer($s); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1 | return $mapping; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | my %default_mapping = ( | |||||||||||||||||||||||||||
| 60 | 'text/x-yaml' => 'YAML', | ||||||||||||||||||||||||||||
| 61 | 'text/html' => 'YAML', | ||||||||||||||||||||||||||||
| 62 | 'text/x-json' => 'JSON', | ||||||||||||||||||||||||||||
| 63 | 'application/json' => 'JSON', | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | ▶ | if ( $self->enable_dumper ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | $default_mapping{'text/x-data-dumper'} = 'Dumper'; | |||||||||||||||||||||||||||
| 68 | 0 | $self->_load_serializer('Dumper'); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | return \%default_mapping; | |||||||||||||||||||||||||||
| 72 | }, | ||||||||||||||||||||||||||||
| 73 | 4 | ); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | sub _load_serializer { | ||||||||||||||||||||||||||
|
Subroutine
_load_serializer: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 76 | 1 | my ( $self, $name ) = @_; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | return $serializer->{$name} if $serializer->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | my $serializer_class = "Dancer2::Serializer::$name"; | |||||||||||||||||||||||||||
| 81 | 1 | require_module($serializer_class); | |||||||||||||||||||||||||||
| 82 | 1 | my $serializer_object = $serializer_class->new; | |||||||||||||||||||||||||||
| 83 | return $serializer->{$name} = { | ||||||||||||||||||||||||||||
| 84 | 1 | ▶ | from => sub { shift; $serializer_object->deserialize(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | 3 | ▶ | to => sub { shift; $serializer_object->serialize(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 86 | 1 | }; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 53 | ▶ | sub serialize { | ||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 53 | my ( $self, $entity ) = @_; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Look for valid format in the headers | ||||||||||||||||||||||||||||
| 93 | 53 | my $format = $self->_get_content_type('accept'); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Match format with a serializer and return | ||||||||||||||||||||||||||||
| 96 | 53 | ▶ | $format and return $serializer->{$format}{'to'}->( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | $self, $entity | ||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # If none is found then just return the entity without change | ||||||||||||||||||||||||||||
| 101 | 0 | return $entity; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 19 | ▶ | sub deserialize { | ||||||||||||||||||||||||||
|
Subroutine
deserialize: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 19 | my ( $self, $content ) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 19 | my $format = $self->_get_content_type('content_type'); | |||||||||||||||||||||||||||
| 108 | 19 | ▶ | $format and return $serializer->{$format}{'from'}->($self, $content); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | return $content; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 72 | ▶ | sub _get_content_type { | ||||||||||||||||||||||||||
|
Subroutine
_get_content_type: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 114 | 72 | my ($self, $header) = @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 72 | ▶ | if ( $self->has_request ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | # Search for the first HTTP header variable which specifies | ||||||||||||||||||||||||||||
| 118 | # supported content. Both content_type and accept are checked | ||||||||||||||||||||||||||||
| 119 | # for backwards compatibility. | ||||||||||||||||||||||||||||
| 120 | 72 | foreach my $method ( $header, qw<content_type accept> ) { | |||||||||||||||||||||||||||
| 121 | 130 | ▶ | if ( my $value = $self->request->header($method) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | # A header value may carry parameters after the content | ||||||||||||||||||||||||||||
| 123 | # type itself (e.g. 'text/x-yaml; charset=utf-8'). Strip | ||||||||||||||||||||||||||||
| 124 | # anything from the first ';' onward, trim surrounding | ||||||||||||||||||||||||||||
| 125 | # whitespace and lowercase what's left, so the lookup | ||||||||||||||||||||||||||||
| 126 | # matches on the content type alone - the mapping's keys | ||||||||||||||||||||||||||||
| 127 | # are bare lowercase content types. (An Accept header | ||||||||||||||||||||||||||||
| 128 | # listing several comma-separated types is not split | ||||||||||||||||||||||||||||
| 129 | # further here: the whole remainder is matched as one | ||||||||||||||||||||||||||||
| 130 | # string, so only a single-type Accept is recognised.) | ||||||||||||||||||||||||||||
| 131 | 76 | ( my $type = $value ) =~ s/;.*$//; | |||||||||||||||||||||||||||
| 132 | 76 | $type =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 133 | 76 | $type = lc $type; | |||||||||||||||||||||||||||
| 134 | 76 | ▶ | if ( my $serializer = $self->mapping->{$type} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 54 | $self->set_content_type($type); | |||||||||||||||||||||||||||
| 136 | 54 | return $serializer; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # If none if found, return the default, 'JSON'. | ||||||||||||||||||||||||||||
| 143 | 18 | $self->set_content_type( DEFAULT_CONTENT_TYPE() ); | |||||||||||||||||||||||||||
| 144 | 18 | return 'JSON'; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4 | 1; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||