| 1 | package GraphQL::Houtou::Internal::TypeSupport; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 62 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 62 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 62 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 62 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 62 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 62 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 11 | named_from_ast | ||||||||||||||||||||||||||||
| 12 | description_doc_lines | ||||||||||||||||||||||||||||
| 13 | apply_fields_deprecation | ||||||||||||||||||||||||||||
| 14 | from_ast_field_deprecate | ||||||||||||||||||||||||||||
| 15 | to_doc_field_deprecate | ||||||||||||||||||||||||||||
| 16 | make_field_def | ||||||||||||||||||||||||||||
| 17 | from_ast_fields | ||||||||||||||||||||||||||||
| 18 | from_ast_maptype | ||||||||||||||||||||||||||||
| 19 | make_fieldtuples | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 62 | my $JSON_noutf8 = JSON::MaybeXS->new->utf8(0)->allow_nonref; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 360 | ▶ | sub named_from_ast { | ||||||||||||||||||||||||||
|
Subroutine
named_from_ast: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 360 | my ($ast_node) = @_; | |||||||||||||||||||||||||||
| 26 | return ( | ||||||||||||||||||||||||||||
| 27 | name => $ast_node->{name}, | ||||||||||||||||||||||||||||
| 28 | 360 | ▶ | ($ast_node->{description} ? (description => $ast_node->{description}) : ()), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 898 | ▶ | sub description_doc_lines { | ||||||||||||||||||||||||||
|
Subroutine
description_doc_lines: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 898 | my ($description) = @_; | |||||||||||||||||||||||||||
| 34 | 898 | ▶ | return if !$description; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 547 | my @lines = split /\n/, $description; | |||||||||||||||||||||||||||
| 37 | 547 | ▶ | return if !@lines; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 547 | ▶ | if (@lines == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 419 | return '"' . ($lines[0] =~ s#"#\\"#gr) . '"'; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | return ( | ||||||||||||||||||||||||||||
| 43 | '"""', | ||||||||||||||||||||||||||||
| 44 | 128 | (map { s#"""#\\"""#gr } @lines), | |||||||||||||||||||||||||||
| 45 | '"""', | ||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2116 | ▶ | sub apply_fields_deprecation { | ||||||||||||||||||||||||||
|
Subroutine
apply_fields_deprecation: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 2116 | my ($values) = @_; | |||||||||||||||||||||||||||
| 51 | 2116 | ▶ | my $copy = { %{$values || {}} }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$values || {}
MC/DC0%0/1
$values || {}
$values
Truth table50%1/2
A
A= $values
| |||||||||||||||||||||||||||||
| 52 | 2116 | for my $name (keys %$copy) { | |||||||||||||||||||||||||||
| 53 | 6361 | my $field = $copy->{$name}; | |||||||||||||||||||||||||||
| 54 | 6361 | my %new = %$field; | |||||||||||||||||||||||||||
| 55 | 6361 | ▶ | $new{is_deprecated} = 1 if defined $field->{deprecation_reason}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 6361 | ▶ | $new{args} = apply_fields_deprecation($field->{args}) if $field->{args}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 6361 | $copy->{$name} = \%new; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 2116 | return $copy; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 693 | ▶ | sub from_ast_field_deprecate { | ||||||||||||||||||||||||||
|
Subroutine
from_ast_field_deprecate: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 693 | my ($key, $values) = @_; | |||||||||||||||||||||||||||
| 64 | 693 | my $value = +{ %{ $values->{$key} } }; | |||||||||||||||||||||||||||
| 65 | 693 | my $directives = delete $value->{directives}; | |||||||||||||||||||||||||||
| 66 | 693 | ▶ | return $values if !$directives || !@$directives; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$directives and @$directives
MC/DC50%1/2
$directives and @$directives
$directives
@$directives
Truth table66%2/3
A and B
A= $directivesB= @$directives
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 24 | my ($deprecated) = grep { $_->{name} eq 'deprecated' } @$directives; | |||||||||||||||||||||||||||
| 69 | 24 | ▶ | return $values if !$deprecated; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 24 | my @remaining = grep { $_->{name} ne 'deprecated' } @$directives; | |||||||||||||||||||||||||||
| 71 | 24 | ▶ | $value->{directives} = \@remaining if @remaining; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 24 | require GraphQL::Houtou::Directive; | |||||||||||||||||||||||||||
| 74 | my $reason = $deprecated->{arguments}{reason} | ||||||||||||||||||||||||||||
| 75 | 24 | ▶ | // $GraphQL::Houtou::Directive::DEPRECATED->args->{reason}{default_value}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$deprecated->{"arguments"}{"reason"} // $GraphQL::Houtou::Directive::DEPRECATED->args->{"reason"}{"default_value"}
MC/DC0%0/2
$deprecated->{"arguments"}{"reason"} // $GraphQL::Houtou::Directive::DEPRECATED->args->{"reason"}{"default_value"}
$deprecated->{"arguments"}{"reason"}
$GraphQL::Houtou::Directive::DEPRECATED->args->{"reason"}{"default_value"}
Truth table66%2/3
A // B
A= $deprecated->{"arguments"}{"reason"}B= $GraphQL::Houtou::Directive::DEPRECATED->args->{"reason"}{"default_value"}
| |||||||||||||||||||||||||||||
| 76 | return +{ | ||||||||||||||||||||||||||||
| 77 | 24 | %$values, | |||||||||||||||||||||||||||
| 78 | $key => { %$value, deprecation_reason => $reason }, | ||||||||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 756 | ▶ | sub to_doc_field_deprecate { | ||||||||||||||||||||||||||
|
Subroutine
to_doc_field_deprecate: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 756 | my ($line, $value) = @_; | |||||||||||||||||||||||||||
| 84 | 756 | ▶ | return $line if !$value->{is_deprecated}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 15 | require GraphQL::Houtou::Directive; | |||||||||||||||||||||||||||
| 87 | 15 | $line .= ' @deprecated'; | |||||||||||||||||||||||||||
| 88 | $line .= '(reason: ' . $JSON_noutf8->encode($value->{deprecation_reason}) . ')' | ||||||||||||||||||||||||||||
| 89 | if $value->{deprecation_reason} ne | ||||||||||||||||||||||||||||
| 90 | 15 | ▶ | $GraphQL::Houtou::Directive::DEPRECATED->args->{reason}{default_value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 15 | return $line; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 664 | ▶ | sub make_field_def { | ||||||||||||||||||||||||||
|
Subroutine
make_field_def: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 664 | my ($name2type, $field_name, $field_def) = @_; | |||||||||||||||||||||||||||
| 96 | 664 | require GraphQL::Houtou::Schema; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 664 | my %args; | |||||||||||||||||||||||||||
| 99 | 664 | ▶ | if ($field_def->{args}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 56 | my $arg_defs = $field_def->{args}; | |||||||||||||||||||||||||||
| 101 | 56 | $arg_defs = from_ast_field_deprecate($_, $arg_defs) for keys %$arg_defs; | |||||||||||||||||||||||||||
| 102 | %args = ( | ||||||||||||||||||||||||||||
| 103 | args => +{ | ||||||||||||||||||||||||||||
| 104 | 56 | map { make_field_def($name2type, $_, $arg_defs->{$_}) } | |||||||||||||||||||||||||||
| 105 | keys %$arg_defs | ||||||||||||||||||||||||||||
| 106 | }, | ||||||||||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | return ( | ||||||||||||||||||||||||||||
| 111 | 664 | $field_name => { | |||||||||||||||||||||||||||
| 112 | %$field_def, | ||||||||||||||||||||||||||||
| 113 | type => GraphQL::Houtou::Schema::lookup_type($field_def, $name2type), | ||||||||||||||||||||||||||||
| 114 | %args, | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 150 | ▶ | sub from_ast_maptype { | ||||||||||||||||||||||||||
|
Subroutine
from_ast_maptype: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | 150 | my ($name2type, $ast_node, $key) = @_; | |||||||||||||||||||||||||||
| 121 | 150 | ▶ | my $names = $ast_node->{$key} || []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$ast_node->{$key} || []
MC/DC100%1/1
$ast_node->{$key} || []
$ast_node->{$key}
Truth table100%2/2
A
A= $ast_node->{$key}
| |||||||||||||||||||||||||||||
| 122 | return ( | ||||||||||||||||||||||||||||
| 123 | 146 | ▶ | $key => sub { [ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 124 | 146 | ▶ | map { $name2type->{$_} // die "Unknown type '$_' in $key.\n" } @$names | ||||||||||||||||||||||||||
|
Condition50%1/2
$name2type->{$_} // die("Unknown type '${_}' in $key.\n")
MC/DC0%0/1
$name2type->{$_} // die("Unknown type '${_}' in $key.\n")
$name2type->{$_}
Truth table50%1/2
A
A= $name2type->{$_}
| |||||||||||||||||||||||||||||
| 125 | ] }, | ||||||||||||||||||||||||||||
| 126 | 150 | ); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 286 | ▶ | sub from_ast_fields { | ||||||||||||||||||||||||||
|
Subroutine
from_ast_fields: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 286 | my ($name2type, $ast_node, $key) = @_; | |||||||||||||||||||||||||||
| 131 | 286 | ▶ | my $fields = $ast_node->{$key} || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$ast_node->{$key} || {}
MC/DC100%1/1
$ast_node->{$key} || {}
$ast_node->{$key}
Truth table100%2/2
A
A= $ast_node->{$key}
| |||||||||||||||||||||||||||||
| 132 | 286 | $fields = from_ast_field_deprecate($_, $fields) for keys %$fields; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | return ( | ||||||||||||||||||||||||||||
| 135 | 283 | ▶ | $key => sub { +{ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 136 | map { | ||||||||||||||||||||||||||||
| 137 | 283 | my @pair = eval { | |||||||||||||||||||||||||||
| 138 | 533 | make_field_def($name2type, $_, $fields->{$_}) | |||||||||||||||||||||||||||
| 139 | }; | ||||||||||||||||||||||||||||
| 140 | 533 | ▶ | die "Error in field '$_': $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 532 | @pair; | |||||||||||||||||||||||||||
| 142 | } keys %$fields | ||||||||||||||||||||||||||||
| 143 | } }, | ||||||||||||||||||||||||||||
| 144 | 286 | ); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 880 | ▶ | sub make_fieldtuples { | ||||||||||||||||||||||||||
|
Subroutine
make_fieldtuples: called CC=7 SCAR=20.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 148 | 880 | my ($fields) = @_; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | return map { | ||||||||||||||||||||||||||||
| 151 | 880 | my $field = $fields->{$_}; | |||||||||||||||||||||||||||
| 152 | 750 | ▶ | my @argtuples = map { $_->[0] } make_fieldtuples($field->{args} || {}); | ||||||||||||||||||||||||||
|
Condition100%2/2
$field->{'args'} || {}
MC/DC100%1/1
$field->{'args'} || {}
$field->{'args'}
Truth table100%2/2
A
A= $field->{'args'}
| |||||||||||||||||||||||||||||
| 153 | 750 | my $type = $field->{type}; | |||||||||||||||||||||||||||
| 154 | 750 | my $line = $_; | |||||||||||||||||||||||||||
| 155 | 750 | ▶ | $line .= '(' . join(', ', @argtuples) . ')' if @argtuples; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 750 | $line .= ': ' . $type->to_string; | |||||||||||||||||||||||||||
| 157 | $line .= ' = ' . $JSON_noutf8->encode( | ||||||||||||||||||||||||||||
| 158 | $type->perl_to_graphql($field->{default_value}) | ||||||||||||||||||||||||||||
| 159 | 750 | ▶ | ) if exists $field->{default_value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | my @directives = map { | ||||||||||||||||||||||||||||
| 161 | 0 | my $args = $_->{arguments}; | |||||||||||||||||||||||||||
| 162 | 0 | my @pairs = map { "$_: " . $JSON_noutf8->encode($args->{$_}) } keys %$args; | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | '@' . $_->{name} . (@pairs ? '(' . join(', ', @pairs) . ')' : ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 750 | ▶ | } @{ $field->{directives} || [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$field->{'directives'} || []
MC/DC0%0/1
$field->{'directives'} || []
$field->{'directives'}
Truth table50%1/2
A
A= $field->{'directives'}
| |||||||||||||||||||||||||||||
| 165 | 750 | ▶ | $line .= join(' ', ('', @directives)) if @directives; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | [ | ||||||||||||||||||||||||||||
| 167 | to_doc_field_deprecate($line, $field), | ||||||||||||||||||||||||||||
| 168 | 750 | description_doc_lines($field->{description}), | |||||||||||||||||||||||||||
| 169 | ] | ||||||||||||||||||||||||||||
| 170 | } sort keys %$fields; | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 62 | 1; | |||||||||||||||||||||||||||