]> Dogcows Code - chaz/graphql-client/blobdiff - lib/GraphQL/Client/CLI.pm
format deeply with cvs,tsv,table
[chaz/graphql-client] / lib / GraphQL / Client / CLI.pm
index 7c1c54545b484f02b93f43af1e943fbff77fefca..81fb47a67f8824ae3fe5f378e4bf8f3b8051af42 100644 (file)
@@ -7,7 +7,7 @@ use strict;
 use Text::ParseWords;
 use Getopt::Long 2.39 qw(GetOptionsFromArray);
 use GraphQL::Client;
-use JSON::MaybeXS;
+use JSON::MaybeXS qw(encode_json);
 use namespace::clean;
 
 our $VERSION = '999.999'; # VERSION
@@ -131,6 +131,17 @@ sub _get_options {
     return \%options;
 }
 
+sub _stringify {
+    my ($item) = @_;
+    if (ref($item) eq 'ARRAY') {
+        my $first = @$item && $item->[0];
+        return join(',', @$item) if !ref($first);
+        return join(',', map { encode_json($_) } @$item);
+    }
+    return encode_json($item) if ref($item) eq 'HASH';
+    return $item;
+}
+
 sub _print_data {
     my ($data, $format) = @_;
     $format = lc($format || 'json:pretty');
@@ -160,12 +171,12 @@ sub _print_data {
                 if ($first && ref $first eq 'HASH') {
                     @columns = sort keys %$first;
                     $rows = [
-                        map { [@{$_}{@columns}] } @$val
+                        map { [map { _stringify($_) } @{$_}{@columns}] } @$val
                     ];
                 }
                 elsif ($first) {
                     @columns = keys %$unpacked;
-                    $rows = [map { [$_] } @$val];
+                    $rows = [map { [map { _stringify($_) } $_] } @$val];
                 }
             }
         }
This page took 0.018015 seconds and 4 git commands to generate.