| 1 | package ICANN::RST::Input; | ||
| 2 | # ABSTRACT: an object representing an RST input parameter. | ||
| 3 | 1 | ▶ | use base qw(ICANN::RST::Base); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use JSON::Schema; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use strict; | ||
| 6 | |||
| 7 | sub description { ICANN::RST::Text->new($_[0]->{'Description'}) } | ||
| 8 | sub type { $_[0]->{'Type'} } | ||
| 9 | sub example { numify($_[0]->{'Example'}) } | ||
| 10 | sub schema { $_[0]->{'Schema'} ? JSON::Schema->new(numify($_[0]->{'Schema'})) : undef } | ||
| 11 | sub required { exists($_[0]->{'Required'}) && $_[0]->{'Required'} } | ||
| 12 | |||
| 13 | sub jsonExample { | ||
| 14 | my $self = shift; | ||
| 15 | |||
| 16 | if ('boolean' eq $self->{'Schema'}->{'type'}) { | ||
| 17 | return ($self->example ? \1 : \0); | ||
| 18 | |||
| 19 | } else { | ||
| 20 | return numify($self->example); | ||
| 21 | |||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | sub cases { | ||
| 26 | my $self = shift; | ||
| 27 | |||
| 28 | my %cases; | ||
| 29 | |||
| 30 | foreach my $case ($self->spec->cases) { | ||
| 31 | foreach my $input ($case->inputs) { | ||
| 32 | $cases{$case->id} = $case if ($input->id eq $self->id && !defined($cases{$case->id})); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | return sort { $a->id cmp $b->id } values(%cases); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub suites { | ||
| 40 | my $self = shift; | ||
| 41 | |||
| 42 | my %suites; | ||
| 43 | |||
| 44 | foreach my $suite ($self->spec->suites) { | ||
| 45 | foreach my $id (@{$suite->{'Input-Parameters'}}) { | ||
| 46 | $suites{$suite->id} = $suite if ($id eq $self->id && !defined($suites{$suite->id})); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | return sort { $a->order cmp $b->order } values(%suites); | ||
| 51 | } | ||
| 52 | |||
| 53 | # | ||
| 54 | # iteratively convert scalars into numeric values where appropriate | ||
| 55 | # | ||
| 56 | sub numify { | ||
| 57 | my $value = shift; | ||
| 58 | |||
| 59 | if ('HASH' eq ref($value)) { | ||
| 60 | return numify_hash($value); | ||
| 61 | |||
| 62 | } elsif ('ARRAY' eq ref($value)) { | ||
| 63 | return numify_array($value); | ||
| 64 | |||
| 65 | } else { | ||
| 66 | return numify_scalar($value); | ||
| 67 | |||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | sub numify_hash { | ||
| 72 | my $ref = shift; | ||
| 73 | |||
| 74 | my $ref2 = {}; | ||
| 75 | foreach my $key (keys(%{$ref})) { | ||
| 76 | $ref2->{$key} = numify($ref->{$key}); | ||
| 77 | } | ||
| 78 | |||
| 79 | return $ref2; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub numify_array { | ||
| 83 | my $ref = shift; | ||
| 84 | |||
| 85 | my @array; | ||
| 86 | foreach my $value (@{$ref}) { | ||
| 87 | push (@array, numify($value)); | ||
| 88 | } | ||
| 89 | |||
| 90 | return \@array; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub numify_scalar { | ||
| 94 | my $value = shift; | ||
| 95 | |||
| 96 | if (q{JSON::PP::Boolean} ne ref($value) && $value =~ /^\d+\.?\d*$/) { | ||
| 97 | $value += 0; | ||
| 98 | } | ||
| 99 | |||
| 100 | return $value; | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; | ||
| 104 | |||
| 105 | __END__ | ||
| 106 | |||
| 107 | =pod | ||
| 108 | |||
| 109 | =encoding UTF-8 | ||
| 110 | |||
| 111 | =head1 NAME | ||
| 112 | |||
| 113 | ICANN::RST::Input - an object representing an RST input parameter. | ||
| 114 | |||
| 115 | =head1 VERSION | ||
| 116 | |||
| 117 | version 0.03 | ||
| 118 | |||
| 119 | =head1 DESCRIPTION | ||
| 120 | |||
| 121 | This class inherits from L<ICANN::RST::Base> (so it has the C<id()>, | ||
| 122 | C<order()> and C<spec()> methods). | ||
| 123 | |||
| 124 | =head1 METHODS | ||
| 125 | |||
| 126 | =head2 description() | ||
| 127 | |||
| 128 | A L<ICANN::RST::Text> object containing the long textual description of the test | ||
| 129 | case. | ||
| 130 | |||
| 131 | =head2 type() | ||
| 132 | |||
| 133 | A string containing the input type. | ||
| 134 | |||
| 135 | =head2 example() | ||
| 136 | |||
| 137 | A string containing an example value. | ||
| 138 | |||
| 139 | =head2 jsonExample() | ||
| 140 | |||
| 141 | A scalar containing the example value coerced into a type that L<JSON::XS> | ||
| 142 | understands. This ensures that numbers and booleans are properly represented | ||
| 143 | when encoded into JSON. | ||
| 144 | |||
| 145 | =head2 schema() | ||
| 146 | |||
| 147 | A hashref containing a L<JSON::Schema> object that can be used to validate this | ||
| 148 | parameter. | ||
| 149 | |||
| 150 | =head2 cases() | ||
| 151 | |||
| 152 | A list of all C<ICANN::RST::Case> objects that use this input parameter. | ||
| 153 | |||
| 154 | =head2 suites() | ||
| 155 | |||
| 156 | A list of all C<ICANN::RST::Suite> objects that use this input parameter. | ||
| 157 | |||
| 158 | =head2 required() | ||
| 159 | |||
| 160 | Whether or not this input parameter is required. | ||
| 161 | |||
| 162 | =head1 AUTHOR | ||
| 163 | |||
| 164 | Gavin Brown <gavin.brown@icann.org> | ||
| 165 | |||
| 166 | =head1 COPYRIGHT AND LICENSE | ||
| 167 | |||
| 168 | This software is copyright (c) 2025 by Internet Corporation for Assigned Names and Number (ICANN). | ||
| 169 | |||
| 170 | This is free software; you can redistribute it and/or modify it under | ||
| 171 | the same terms as the Perl 5 programming language system itself. | ||
| 172 | |||
| 173 | =cut | ||