line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package GraphQL::Client::CLI; |
2
|
|
|
|
|
|
|
# ABSTRACT: Implementation of the graphql CLI program |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
72233
|
use warnings; |
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
32
|
|
5
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
570
|
use Encode qw(decode); |
|
1
|
|
|
|
|
10284
|
|
|
1
|
|
|
|
|
74
|
|
8
|
1
|
|
|
1
|
|
713
|
use Getopt::Long 2.39 qw(GetOptionsFromArray); |
|
1
|
|
|
|
|
12788
|
|
|
1
|
|
|
|
|
29
|
|
9
|
1
|
|
|
1
|
|
633
|
use GraphQL::Client; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
32
|
|
10
|
1
|
|
|
1
|
|
473
|
use JSON::MaybeXS; |
|
1
|
|
|
|
|
5647
|
|
|
1
|
|
|
|
|
64
|
|
11
|
1
|
|
|
1
|
|
468
|
use Text::ParseWords; |
|
1
|
|
|
|
|
1311
|
|
|
1
|
|
|
|
|
63
|
|
12
|
1
|
|
|
1
|
|
9
|
use namespace::clean; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
5
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.604'; # VERSION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $JSON = JSON::MaybeXS->new(canonical => 1); |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
2
|
|
16
|
sub _croak { require Carp; goto &Carp::croak } |
|
2
|
|
|
|
|
27
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub new { |
21
|
0
|
|
|
0
|
1
|
0
|
my $class = shift; |
22
|
0
|
|
|
|
|
0
|
bless {}, $class; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub main { |
26
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
27
|
0
|
0
|
|
|
|
0
|
$self = $self->new if !ref $self; |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
0
|
my $options = eval { $self->_get_options(@_) }; |
|
0
|
|
|
|
|
0
|
|
30
|
0
|
0
|
|
|
|
0
|
if (my $err = $@) { |
31
|
0
|
|
|
|
|
0
|
print STDERR $err; |
32
|
0
|
|
|
|
|
0
|
_pod2usage(2); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
0
|
0
|
|
|
|
0
|
if ($options->{version}) { |
36
|
0
|
|
|
|
|
0
|
print "graphql $VERSION\n"; |
37
|
0
|
|
|
|
|
0
|
exit 0; |
38
|
|
|
|
|
|
|
} |
39
|
0
|
0
|
|
|
|
0
|
if ($options->{help}) { |
40
|
0
|
|
|
|
|
0
|
_pod2usage(-exitval => 0, -verbose => 99, -sections => [qw(NAME SYNOPSIS OPTIONS)]); |
41
|
|
|
|
|
|
|
} |
42
|
0
|
0
|
|
|
|
0
|
if ($options->{manual}) { |
43
|
0
|
|
|
|
|
0
|
_pod2usage(-exitval => 0, -verbose => 2); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
0
|
my $url = $options->{url}; |
47
|
0
|
0
|
|
|
|
0
|
if (!$url) { |
48
|
0
|
|
|
|
|
0
|
print STDERR "The or --url option argument is required.\n"; |
49
|
0
|
|
|
|
|
0
|
_pod2usage(2); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
0
|
my $variables = $options->{variables}; |
53
|
0
|
|
|
|
|
0
|
my $query = $options->{query}; |
54
|
0
|
|
|
|
|
0
|
my $operation_name = $options->{operation_name}; |
55
|
0
|
|
|
|
|
0
|
my $unpack = $options->{unpack}; |
56
|
0
|
|
|
|
|
0
|
my $outfile = $options->{outfile}; |
57
|
0
|
|
|
|
|
0
|
my $format = $options->{format}; |
58
|
0
|
|
|
|
|
0
|
my $transport = $options->{transport}; |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
0
|
my $client = GraphQL::Client->new(url => $url); |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
0
|
eval { $client->transport }; |
|
0
|
|
|
|
|
0
|
|
63
|
0
|
0
|
|
|
|
0
|
if (my $err = $@) { |
64
|
0
|
0
|
|
|
|
0
|
warn $err if $ENV{GRAPHQL_CLIENT_DEBUG}; |
65
|
0
|
|
|
|
|
0
|
print STDERR "Could not construct a transport for URL: $url\n"; |
66
|
0
|
|
|
|
|
0
|
print STDERR "Is this URL correct?\n"; |
67
|
0
|
|
|
|
|
0
|
_pod2usage(2); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
0
|
if ($query eq '-') { |
71
|
0
|
0
|
|
|
|
0
|
print STDERR "Interactive mode engaged! Waiting for a query on ...\n" |
72
|
|
|
|
|
|
|
if -t STDIN; ## no critic (InputOutput::ProhibitInteractiveTest) |
73
|
0
|
|
|
|
|
0
|
binmode(STDIN, 'encoding(UTF-8)'); |
74
|
0
|
|
|
|
|
0
|
$query = do { local $/; }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
0
|
my $resp = $client->execute($query, $variables, $operation_name, $transport); |
78
|
0
|
|
|
|
|
0
|
my $err = $resp->{errors}; |
79
|
0
|
0
|
|
|
|
0
|
$unpack = 0 if $err; |
80
|
0
|
0
|
|
|
|
0
|
my $data = $unpack ? $resp->{data} : $resp; |
81
|
|
|
|
|
|
|
|
82
|
0
|
0
|
|
|
|
0
|
if ($outfile) { |
83
|
0
|
0
|
|
|
|
0
|
open(my $out, '>', $outfile) or die "Open $outfile failed: $!"; |
84
|
0
|
|
|
|
|
0
|
*STDOUT = $out; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
0
|
0
|
|
|
|
0
|
if (my $filter = $options->{filter}) { |
88
|
0
|
0
|
|
|
|
0
|
eval { require JSON::Path::Evaluator } or die "Missing dependency: JSON::Path\n"; |
|
0
|
|
|
|
|
0
|
|
89
|
0
|
|
|
|
|
0
|
my @values = JSON::Path::Evaluator::evaluate_jsonpath($data, $filter); |
90
|
0
|
0
|
|
|
|
0
|
if (@values == 1) { |
91
|
0
|
|
|
|
|
0
|
$data = $values[0]; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
0
|
|
|
|
|
0
|
$data = \@values; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
0
|
binmode(STDOUT, 'encoding(UTF-8)'); |
99
|
0
|
|
|
|
|
0
|
_print_data($data, $format); |
100
|
|
|
|
|
|
|
|
101
|
0
|
0
|
0
|
|
|
0
|
exit($unpack && $err ? 1 : 0); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub _get_options { |
105
|
4
|
|
|
4
|
|
3365
|
my $self = shift; |
106
|
4
|
|
|
|
|
13
|
my @args = @_; |
107
|
|
|
|
|
|
|
|
108
|
4
|
|
100
|
|
|
26
|
unshift @args, shellwords($ENV{GRAPHQL_CLIENT_OPTIONS} || ''); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# assume UTF-8 args if non-ASCII |
111
|
1
|
50
|
|
1
|
|
1679
|
@args = map { decode('UTF-8', $_) } @args if grep { /\P{ASCII}/ } @args; |
|
1
|
|
|
|
|
14
|
|
|
1
|
|
|
|
|
16
|
|
|
4
|
|
|
|
|
336
|
|
|
0
|
|
|
|
|
0
|
|
|
16
|
|
|
|
|
44
|
|
112
|
|
|
|
|
|
|
|
113
|
4
|
|
|
|
|
15
|
my %options = ( |
114
|
|
|
|
|
|
|
format => 'json:pretty', |
115
|
|
|
|
|
|
|
unpack => 0, |
116
|
|
|
|
|
|
|
); |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
GetOptionsFromArray(\@args, |
119
|
|
|
|
|
|
|
'version' => \$options{version}, |
120
|
|
|
|
|
|
|
'help|h|?' => \$options{help}, |
121
|
|
|
|
|
|
|
'manual|man' => \$options{manual}, |
122
|
|
|
|
|
|
|
'url|u=s' => \$options{url}, |
123
|
|
|
|
|
|
|
'query|mutation=s' => \$options{query}, |
124
|
|
|
|
|
|
|
'variables|vars|V=s' => \$options{variables}, |
125
|
|
|
|
|
|
|
'variable|var|d=s%' => \$options{variables}, |
126
|
|
|
|
|
|
|
'operation-name|n=s' => \$options{operation_name}, |
127
|
|
|
|
|
|
|
'transport|t=s%' => \$options{transport}, |
128
|
|
|
|
|
|
|
'format|f=s' => \$options{format}, |
129
|
|
|
|
|
|
|
'filter|p=s' => \$options{filter}, |
130
|
|
|
|
|
|
|
'unpack!' => \$options{unpack}, |
131
|
|
|
|
|
|
|
'output|o=s' => \$options{outfile}, |
132
|
4
|
50
|
|
|
|
38
|
) or _pod2usage(2); |
133
|
|
|
|
|
|
|
|
134
|
4
|
100
|
|
|
|
4868
|
$options{url} = shift @args if !$options{url}; |
135
|
4
|
100
|
|
|
|
13
|
$options{query} = shift @args if !$options{query}; |
136
|
|
|
|
|
|
|
|
137
|
4
|
|
50
|
|
|
10
|
$options{query} ||= '-'; |
138
|
|
|
|
|
|
|
|
139
|
4
|
|
|
|
|
6
|
my $transport = eval { _expand_vars($options{transport}) }; |
|
4
|
|
|
|
|
10
|
|
140
|
4
|
50
|
|
|
|
10
|
die "Two or more --transport keys are incompatible.\n" if $@; |
141
|
|
|
|
|
|
|
|
142
|
4
|
50
|
|
|
|
16
|
if (ref $options{variables}) { |
|
|
50
|
|
|
|
|
|
143
|
0
|
|
|
|
|
0
|
$options{variables} = eval { _expand_vars($options{variables}) }; |
|
0
|
|
|
|
|
0
|
|
144
|
0
|
0
|
|
|
|
0
|
die "Two or more --variable keys are incompatible.\n" if $@; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
elsif ($options{variables}) { |
147
|
0
|
|
|
|
|
0
|
$options{variables} = eval { $JSON->decode($options{variables}) }; |
|
0
|
|
|
|
|
0
|
|
148
|
0
|
0
|
|
|
|
0
|
die "The --variables JSON does not parse.\n" if $@; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
4
|
|
|
|
|
18
|
return \%options; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub _stringify { |
155
|
0
|
|
|
0
|
|
0
|
my ($item) = @_; |
156
|
0
|
0
|
|
|
|
0
|
if (ref($item) eq 'ARRAY') { |
157
|
0
|
|
0
|
|
|
0
|
my $first = @$item && $item->[0]; |
158
|
0
|
0
|
|
|
|
0
|
return join(',', @$item) if !ref($first); |
159
|
0
|
|
|
|
|
0
|
return join(',', map { $JSON->encode($_) } @$item); |
|
0
|
|
|
|
|
0
|
|
160
|
|
|
|
|
|
|
} |
161
|
0
|
0
|
|
|
|
0
|
return $JSON->encode($item) if ref($item) eq 'HASH'; |
162
|
0
|
|
|
|
|
0
|
return $item; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub _print_data { |
166
|
0
|
|
|
0
|
|
0
|
my ($data, $format) = @_; |
167
|
0
|
|
0
|
|
|
0
|
$format = lc($format || 'json:pretty'); |
168
|
0
|
0
|
0
|
|
|
0
|
if ($format eq 'json' || $format eq 'json:pretty') { |
|
|
0
|
0
|
|
|
|
|
|
|
0
|
0
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
169
|
0
|
|
|
|
|
0
|
my %opts = (allow_nonref => 1, canonical => 1); |
170
|
0
|
0
|
|
|
|
0
|
$opts{pretty} = 1 if $format eq 'json:pretty'; |
171
|
0
|
|
|
|
|
0
|
print JSON::MaybeXS->new(%opts)->encode($data); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
elsif ($format eq 'yaml') { |
174
|
0
|
0
|
|
|
|
0
|
eval { require YAML } or die "Missing dependency: YAML\n"; |
|
0
|
|
|
|
|
0
|
|
175
|
0
|
|
|
|
|
0
|
print YAML::Dump($data); |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
elsif ($format eq 'csv' || $format eq 'tsv' || $format eq 'table') { |
178
|
0
|
0
|
|
|
|
0
|
my $sep = $format eq 'tsv' ? "\t" : ','; |
179
|
|
|
|
|
|
|
|
180
|
0
|
|
|
|
|
0
|
my $unpacked = $data; |
181
|
|
|
|
|
|
|
# $unpacked = $data->{data} if !$unpack && !$err; |
182
|
0
|
0
|
0
|
|
|
0
|
$unpacked = $data->{data} if ref $data eq 'HASH' && $data->{data}; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# check the response to see if it can be formatted |
185
|
0
|
|
|
|
|
0
|
my @columns; |
186
|
0
|
|
|
|
|
0
|
my $rows = []; |
187
|
0
|
0
|
|
|
|
0
|
if (ref $unpacked eq 'HASH') { |
|
|
0
|
|
|
|
|
|
188
|
0
|
0
|
|
|
|
0
|
if (keys %$unpacked == 1) { |
189
|
0
|
|
|
|
|
0
|
my ($val) = values %$unpacked; |
190
|
0
|
0
|
|
|
|
0
|
if (ref $val eq 'ARRAY') { |
191
|
0
|
|
|
|
|
0
|
my $first = $val->[0]; |
192
|
0
|
0
|
0
|
|
|
0
|
if ($first && ref $first eq 'HASH') { |
|
|
0
|
|
|
|
|
|
193
|
0
|
|
|
|
|
0
|
@columns = sort keys %$first; |
194
|
|
|
|
|
|
|
$rows = [ |
195
|
0
|
|
|
|
|
0
|
map { [map { _stringify($_) } @{$_}{@columns}] } @$val |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
196
|
|
|
|
|
|
|
]; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
elsif ($first) { |
199
|
0
|
|
|
|
|
0
|
@columns = keys %$unpacked; |
200
|
0
|
|
|
|
|
0
|
$rows = [map { [map { _stringify($_) } $_] } @$val]; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
elsif (ref $unpacked eq 'ARRAY') { |
206
|
0
|
|
|
|
|
0
|
my $first = $unpacked->[0]; |
207
|
0
|
0
|
0
|
|
|
0
|
if ($first && ref $first eq 'HASH') { |
|
|
0
|
|
|
|
|
|
208
|
0
|
|
|
|
|
0
|
@columns = sort keys %$first; |
209
|
|
|
|
|
|
|
$rows = [ |
210
|
0
|
|
|
|
|
0
|
map { [map { _stringify($_) } @{$_}{@columns}] } @$unpacked |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
211
|
|
|
|
|
|
|
]; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
elsif ($first) { |
214
|
0
|
|
|
|
|
0
|
@columns = qw(column); |
215
|
0
|
|
|
|
|
0
|
$rows = [map { [map { _stringify($_) } $_] } @$unpacked]; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
0
|
0
|
|
|
|
0
|
if (@columns) { |
220
|
0
|
0
|
|
|
|
0
|
if ($format eq 'table') { |
221
|
0
|
0
|
|
|
|
0
|
eval { require Text::Table::Any } or die "Missing dependency: Text::Table::Any\n"; |
|
0
|
|
|
|
|
0
|
|
222
|
|
|
|
|
|
|
my $table = Text::Table::Any::table( |
223
|
|
|
|
|
|
|
header_row => 1, |
224
|
|
|
|
|
|
|
rows => [[@columns], @$rows], |
225
|
|
|
|
|
|
|
backend => $ENV{PERL_TEXT_TABLE}, |
226
|
0
|
|
|
|
|
0
|
); |
227
|
0
|
|
|
|
|
0
|
print $table; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
else { |
230
|
0
|
0
|
|
|
|
0
|
eval { require Text::CSV } or die "Missing dependency: Text::CSV\n"; |
|
0
|
|
|
|
|
0
|
|
231
|
0
|
|
|
|
|
0
|
my $csv = Text::CSV->new({binary => 1, sep => $sep, eol => $/}); |
232
|
0
|
|
|
|
|
0
|
$csv->print(*STDOUT, [@columns]); |
233
|
0
|
|
|
|
|
0
|
for my $row (@$rows) { |
234
|
0
|
|
|
|
|
0
|
$csv->print(*STDOUT, $row); |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
else { |
239
|
0
|
|
|
|
|
0
|
_print_data($data); |
240
|
0
|
|
|
|
|
0
|
print STDERR sprintf("Error: Response could not be formatted as %s.\n", uc($format)); |
241
|
0
|
|
|
|
|
0
|
exit 3; |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
elsif ($format eq 'string') { |
245
|
0
|
0
|
|
|
|
0
|
if (!ref $data) { |
|
|
0
|
|
|
|
|
|
246
|
0
|
|
|
|
|
0
|
print $data, "\n"; |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
elsif (ref $data eq 'ARRAY') { |
249
|
0
|
|
|
|
|
0
|
print join("\n", @$data); |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
else { |
252
|
0
|
|
|
|
|
0
|
_print_data($data); |
253
|
0
|
|
|
|
|
0
|
print STDERR sprintf("Error: Response could not be formatted as %s.\n", $format); |
254
|
0
|
|
|
|
|
0
|
exit 3; |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
elsif ($format eq 'perl') { |
258
|
0
|
0
|
|
|
|
0
|
eval { require Data::Dumper } or die "Missing dependency: Data::Dumper\n"; |
|
0
|
|
|
|
|
0
|
|
259
|
0
|
|
|
|
|
0
|
print Data::Dumper::Dumper($data); |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
else { |
262
|
0
|
|
|
|
|
0
|
_print_data($data); |
263
|
0
|
|
|
|
|
0
|
print STDERR "Error: Format not supported: $format\n"; |
264
|
0
|
|
|
|
|
0
|
exit 3; |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
sub _parse_path { |
269
|
8
|
|
|
8
|
|
11
|
my $path = shift; |
270
|
|
|
|
|
|
|
|
271
|
8
|
|
|
|
|
12
|
my @path; |
272
|
|
|
|
|
|
|
|
273
|
8
|
|
|
|
|
34
|
my @segments = map { split(/\./, $_) } split(/(\[[^\.\]]+\])\.?/, $path); |
|
15
|
|
|
|
|
38
|
|
274
|
8
|
|
|
|
|
18
|
for my $segment (@segments) { |
275
|
19
|
100
|
|
|
|
46
|
if ($segment =~ /\[([^\.\]]+)\]/) { |
276
|
4
|
50
|
|
|
|
12
|
$path[-1]{type} = 'ARRAY' if @path; |
277
|
4
|
|
|
|
|
15
|
push @path, { |
278
|
|
|
|
|
|
|
name => $1, |
279
|
|
|
|
|
|
|
index => 1, |
280
|
|
|
|
|
|
|
}; |
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
else { |
283
|
15
|
100
|
|
|
|
29
|
$path[-1]{type} = 'HASH' if @path; |
284
|
15
|
|
|
|
|
41
|
push @path, { |
285
|
|
|
|
|
|
|
name => $segment, |
286
|
|
|
|
|
|
|
}; |
287
|
|
|
|
|
|
|
} |
288
|
|
|
|
|
|
|
} |
289
|
|
|
|
|
|
|
|
290
|
8
|
|
|
|
|
17
|
return \@path; |
291
|
|
|
|
|
|
|
} |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
sub _expand_vars { |
294
|
7
|
|
|
7
|
|
5564
|
my $vars = shift; |
295
|
|
|
|
|
|
|
|
296
|
7
|
|
|
|
|
13
|
my $root = {}; |
297
|
|
|
|
|
|
|
|
298
|
7
|
|
|
|
|
35
|
while (my ($key, $value) = each %$vars) { |
299
|
8
|
|
|
|
|
19
|
my $parsed_path = _parse_path($key); |
300
|
|
|
|
|
|
|
|
301
|
8
|
|
|
|
|
15
|
my $curr = $root; |
302
|
8
|
|
|
|
|
12
|
for my $segment (@$parsed_path) { |
303
|
17
|
|
|
|
|
26
|
my $name = $segment->{name}; |
304
|
17
|
|
100
|
|
|
42
|
my $type = $segment->{type} || ''; |
305
|
17
|
100
|
|
|
|
41
|
my $next = $type eq 'HASH' ? {} : $type eq 'ARRAY' ? [] : $value; |
|
|
100
|
|
|
|
|
|
306
|
17
|
100
|
|
|
|
34
|
if (ref $curr eq 'HASH') { |
|
|
50
|
|
|
|
|
|
307
|
14
|
50
|
|
|
|
27
|
_croak 'Conflicting keys' if $segment->{index}; |
308
|
14
|
100
|
|
|
|
26
|
if (defined $curr->{$name}) { |
309
|
3
|
100
|
|
|
|
14
|
_croak 'Conflicting keys' if $type ne ref $curr->{$name}; |
310
|
1
|
|
|
|
|
2
|
$next = $curr->{$name}; |
311
|
|
|
|
|
|
|
} |
312
|
|
|
|
|
|
|
else { |
313
|
11
|
|
|
|
|
23
|
$curr->{$name} = $next; |
314
|
|
|
|
|
|
|
} |
315
|
|
|
|
|
|
|
} |
316
|
|
|
|
|
|
|
elsif (ref $curr eq 'ARRAY') { |
317
|
3
|
50
|
|
|
|
7
|
_croak 'Conflicting keys' if !$segment->{index}; |
318
|
3
|
50
|
|
|
|
9
|
if (defined $curr->[$name]) { |
319
|
0
|
0
|
|
|
|
0
|
_croak 'Conflicting keys' if $type ne ref $curr->[$name]; |
320
|
0
|
|
|
|
|
0
|
$next = $curr->[$name]; |
321
|
|
|
|
|
|
|
} |
322
|
|
|
|
|
|
|
else { |
323
|
3
|
|
|
|
|
6
|
$curr->[$name] = $next; |
324
|
|
|
|
|
|
|
} |
325
|
|
|
|
|
|
|
} |
326
|
|
|
|
|
|
|
else { |
327
|
0
|
|
|
|
|
0
|
_croak 'Conflicting keys'; |
328
|
|
|
|
|
|
|
} |
329
|
15
|
|
|
|
|
48
|
$curr = $next; |
330
|
|
|
|
|
|
|
} |
331
|
|
|
|
|
|
|
} |
332
|
|
|
|
|
|
|
|
333
|
5
|
|
|
|
|
12
|
return $root; |
334
|
|
|
|
|
|
|
} |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
sub _pod2usage { |
337
|
0
|
|
|
0
|
|
|
eval { require Pod::Usage }; |
|
0
|
|
|
|
|
|
|
338
|
0
|
0
|
|
|
|
|
if ($@) { |
339
|
0
|
0
|
|
|
|
|
my $ref = $VERSION eq '999.999' ? 'master' : "v$VERSION"; |
340
|
|
|
|
|
|
|
my $exit = (@_ == 1 && $_[0] =~ /^\d+$/ && $_[0]) // |
341
|
0
|
|
0
|
|
|
|
(@_ % 2 == 0 && {@_}->{'-exitval'}) // 2; |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
342
|
0
|
|
|
|
|
|
print STDERR <
|
343
|
|
|
|
|
|
|
Online documentation is available at: |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
https://github.com/chazmcgarvey/graphql-client/blob/$ref/README.md |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
Tip: To enable inline documentation, install the Pod::Usage module. |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
END |
350
|
0
|
|
|
|
|
|
exit $exit; |
351
|
|
|
|
|
|
|
} |
352
|
|
|
|
|
|
|
else { |
353
|
0
|
|
|
|
|
|
goto &Pod::Usage::pod2usage; |
354
|
|
|
|
|
|
|
} |
355
|
|
|
|
|
|
|
} |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
1; |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
__END__ |