| 1 | package Thrift::Parser::Type::Enum; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Thrift::Parser::Type::Enum - Enum type | ||||||||||
| 6 | |||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||
| 8 | |||||||||||
| 9 | =cut | ||||||||||
| 10 | |||||||||||
| 11 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 6 | ▶ | use base qw(Thrift::Parser::Type); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 6 | ▶ | use Scalar::Util qw(blessed); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | =head1 METHODS | ||||||||||
| 17 | |||||||||||
| 18 | This class inherits from L<Thrift::Parser::Type>; see docs there for inherited methods. | ||||||||||
| 19 | |||||||||||
| 20 | =cut | ||||||||||
| 21 | |||||||||||
| 22 | 0 | ▶ | sub type_id { | ||||||||
|
Subroutine
type_id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 23 | 0 | return Thrift::Parser::Types->to_id('i32'); | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | =head2 compose | ||||||||||
| 27 | |||||||||||
| 28 | Pass either a named element of the enumeration or an index: | ||||||||||
| 29 | |||||||||||
| 30 | my $value = $enum->compose('PHP'); | ||||||||||
| 31 | my $value = $enum->compose('_4'); | ||||||||||
| 32 | |||||||||||
| 33 | As you can see, the index is '_' + digits. Throws L<Thrift::Parser::InvalidArgument> | ||||||||||
| 34 | |||||||||||
| 35 | =cut | ||||||||||
| 36 | |||||||||||
| 37 | 0 | ▶ | sub compose { | ||||||||
|
Subroutine
compose: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 38 | 0 | my ($class, $value) = @_; | |||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | if (blessed $value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | ▶ | if (! $value->isa($class)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 0 | Thrift::Parser::InvalidArgument->throw("$class compose() can't take a value of ".ref($value)); | |||||||||
| 43 | } | ||||||||||
| 44 | 0 | return $value; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 0 | ▶ | Thrift::Parser::InvalidArgument->throw("'undef' is not valid for $class") if ! defined $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | ▶ | if (my ($id) = $value =~ m{^_(\d+)$}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | return $class->new_from_id($id); | |||||||||
| 50 | } | ||||||||||
| 51 | else { | ||||||||||
| 52 | 0 | return $class->new_from_name($value); | |||||||||
| 53 | } | ||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | sub write { | ||||||||
|
Subroutine
write: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 57 | 0 | my ($self, $output) = @_; | |||||||||
| 58 | 0 | $output->writeI32($self->value); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | =head2 new_from_id | ||||||||||
| 62 | |||||||||||
| 63 | Same as L</compose> when called with '_' + digit. | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | sub new_from_id { | ||||||||
|
Subroutine
new_from_id: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 68 | 0 | my ($class, $id) = @_; | |||||||||
| 69 | 0 | my $name = $class->idl->value_id_name($id); | |||||||||
| 70 | 0 | ▶ | Thrift::Parser::InvalidArgument->throw( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | error => "No value found for enum index '$id' in type '".$class->name."'", | ||||||||||
| 72 | key => 'id', value => $id, | ||||||||||
| 73 | ) if ! defined $name; | ||||||||||
| 74 | 0 | return $class->new({ value => $id }); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | =head2 new_from_name | ||||||||||
| 78 | |||||||||||
| 79 | Same as L</compose> when called with an element name. | ||||||||||
| 80 | |||||||||||
| 81 | =cut | ||||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | sub new_from_name { | ||||||||
|
Subroutine
new_from_name: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 84 | 0 | my ($class, $name) = @_; | |||||||||
| 85 | 0 | my $id = $class->idl->value_named($name); | |||||||||
| 86 | 0 | ▶ | Thrift::Parser::InvalidArgument->throw( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | error => "No value found for enum index '$name' in type '".$class->name."'", | ||||||||||
| 88 | key => 'name', value => $name, | ||||||||||
| 89 | ) if ! defined $id; | ||||||||||
| 90 | 0 | return $class->new({ value => $id }); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | =head2 value_name | ||||||||||
| 94 | |||||||||||
| 95 | my $object = $enum->compose('PHP'); | ||||||||||
| 96 | $object->value_name == 'PHP'; | ||||||||||
| 97 | |||||||||||
| 98 | Returns the name of the element referenced by $self. | ||||||||||
| 99 | |||||||||||
| 100 | =cut | ||||||||||
| 101 | |||||||||||
| 102 | 0 | ▶ | sub value_name { | ||||||||
|
Subroutine
value_name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 103 | 0 | my $self = shift; | |||||||||
| 104 | 0 | return $self->idl->value_id_name($self->value); | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 0 | ▶ | sub value_plain { | ||||||||
|
Subroutine
value_plain: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 108 | 0 | my $self = shift; | |||||||||
| 109 | 0 | return $self->value_name; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | =head1 COPYRIGHT | ||||||||||
| 113 | |||||||||||
| 114 | Copyright (c) 2009 Eric Waters and XMission LLC (http://www.xmission.com/). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||
| 115 | |||||||||||
| 116 | The full text of the license can be found in the LICENSE file included with this module. | ||||||||||
| 117 | |||||||||||
| 118 | =head1 AUTHOR | ||||||||||
| 119 | |||||||||||
| 120 | Eric Waters <ewaters@gmail.com> | ||||||||||
| 121 | |||||||||||
| 122 | =cut | ||||||||||
| 123 | |||||||||||
| 124 | 6 | 1; | |||||||||