/
Format.pm
140 lines (93 loc) · 3.07 KB
/
Format.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package Perinci::Result::Format;
use 5.010;
use strict;
use warnings;
use Scalar::Util qw(reftype);
# VERSION
# text formats are special. since they are more oriented towards human instead
# of machine, we remove envelope when status is 200, so users only see content.
my $format_text = sub {
my ($format, $res) = @_;
if (!defined($res->[2])) {
return $res->[0] =~ /\A(?:200|304)\z/ ? "" :
"ERROR $res->[0]: $res->[1]" .
($res->[1] =~ /\n\z/ ? "" : "\n");
}
my ($r, $opts);
if ($res->[0] == 200) {
$r = $res->[2];
$opts = $res->[3]{result_format_options} // {};
} else {
$r = $res;
$opts = {};
}
if ($format eq 'text') {
return Data::Format::Pretty::format_pretty(
$r, {%$opts, module=>'Console'});
}
if ($format eq 'text-simple') {
return Data::Format::Pretty::format_pretty(
$r, {%$opts, module=>'SimpleText'});
}
if ($format eq 'text-pretty') {
return Data::Format::Pretty::format_pretty(
$r, {%$opts, module=>'Text'});
}
};
our %Formats = (
yaml => ['YAML', 'text/yaml'],
json => ['CompactJSON', 'application/json'],
'json-pretty' => ['JSON', 'application/json'],
text => [$format_text, 'text/plain'],
'text-simple' => [$format_text, 'text/plain'],
'text-pretty' => [$format_text, 'text/plain'],
);
sub format {
require Data::Format::Pretty;
my ($res, $format) = @_;
my $formatter = $Formats{$format} or return undef;
if ((reftype($formatter->[0]) // '') eq 'CODE') {
return $formatter->[0]->($format, $res);
} else {
return Data::Format::Pretty::format_pretty(
$res, {module=>$formatter->[0]});
}
}
1;
# ABSTRACT: Format envelope result
=head1 SYNOPSIS
=head1 DESCRIPTION
This module formats enveloped result to YAML, JSON, etc. It uses
L<Data::Format::Pretty> for the backend. It is used by other Perinci modules
like L<Perinci::CmdLine> and L<Perinci::Access::HTTP::Server>.
The default supported formats are:
=over 4
=item * json
Using Data::Format::Pretty::YAML.
=item * text-simple
Using Data::Format::Pretty::SimpleText.
=item * text-pretty
Using Data::Format::Pretty::Text.
=item * text
Using Data::Format::Pretty::Console.
=item * yaml
Using Data::Format::Pretty::YAML.
=back
=head1 VARIABLES
=head1 %Perinci::Result::Format::Formats
Contains a mapping between format names and Data::Format::Pretty::* module
names + MIME type.
=head1 FUNCTIONS
None is currently exported/exportable.
=head1 format($res, $format) => STR
=head1 FAQ
=head2 How to add support for new formats?
First make sure that Data::Format::Pretty::<FORMAT> module is available for your
format. Look on CPAN. If it's not, i't also not hard to create one.
Then, add your format to %Perinci::Result::Format::Formats hash:
use Perinci::Result::Format;
# this means format named 'xml' will be handled by Data::Format::Pretty::XML
$Perinci::Result::Format::Formats{xml} = ['XML', 'text/xml'];
=head1 SEE ALSO
L<Data::Format::Pretty>
=cut