| 1 | package Thrift::Parser::FieldSet; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Thrift::Parser::FieldSet - A set of fields in a structure | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use Params::Validate qw(validate_with); | ||
| 12 | use Data::Dumper; | ||
| 13 | use Scalar::Util qw(blessed); | ||
| 14 | use base qw(Class::Accessor::Grouped); | ||
| 15 | __PACKAGE__->mk_group_accessors(simple => qw(fields)); | ||
| 16 | |||
| 17 | =head1 USAGE | ||
| 18 | |||
| 19 | =head2 fields | ||
| 20 | |||
| 21 | Returns an array ref of all the fields in this set. | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my $class = shift; | ||
| 27 | my %self = validate_with( | ||
| 28 | params => shift, | ||
| 29 | spec => { | ||
| 30 | fields => 1, | ||
| 31 | }, | ||
| 32 | ); | ||
| 33 | return bless \%self, $class; | ||
| 34 | } | ||
| 35 | |||
| 36 | =head2 named | ||
| 37 | |||
| 38 | my $value = $field_set->named('id'); | ||
| 39 | |||
| 40 | Searches the fields in the set for the field named $name. Returns the value of that field. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub named { | ||
| 45 | my ($self, $name) = @_; | ||
| 46 | foreach my $field (@{ $self->{fields} }) { | ||
| 47 | if ($field->name eq $name) { | ||
| 48 | return $field->value; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | *field_named = \&named; | ||
| 54 | |||
| 55 | =head2 id | ||
| 56 | |||
| 57 | my $value = $field_set->id(2); | ||
| 58 | |||
| 59 | Searches the fields in the set for the field with id $id. Returns the value of that field. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub id { | ||
| 64 | my ($self, $id) = @_; | ||
| 65 | foreach my $field (@{ $self->{fields} }) { | ||
| 66 | if ($field->id eq $id) { | ||
| 67 | return $field->value; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 ids | ||
| 74 | |||
| 75 | Returns an array ref of the ids of this field set. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub ids { | ||
| 80 | my $self = shift; | ||
| 81 | my @ids = sort { $a <=> $b } map { $_->id } @{ $self->{fields} }; | ||
| 82 | return \@ids; | ||
| 83 | } | ||
| 84 | |||
| 85 | =head2 field_values | ||
| 86 | |||
| 87 | Returns an array ref of the values of the fields in the set. | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub field_values { | ||
| 92 | my $self = shift; | ||
| 93 | my @values = map { $_->value } @{ $self->{fields} }; | ||
| 94 | return \@values; | ||
| 95 | } | ||
| 96 | |||
| 97 | =head2 keyed_field_values | ||
| 98 | |||
| 99 | Returns a hashref where the keys are the names of the fields and the values are the values of the fields. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub keyed_field_values { | ||
| 104 | my $self = shift; | ||
| 105 | my %hash = map { $_->name => $_->value } @{ $self->{fields} }; | ||
| 106 | return \%hash; | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 keyed_field_values_plain | ||
| 110 | |||
| 111 | Returns a hashref where the keys are the names of the fields and the values are the plain values of the fields. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub keyed_field_values_plain { | ||
| 116 | my $self = shift; | ||
| 117 | my %hash = map { $_->name => $_->value->value_plain } @{ $self->{fields} }; | ||
| 118 | return \%hash; | ||
| 119 | } | ||
| 120 | |||
| 121 | =head2 compose | ||
| 122 | |||
| 123 | Used internally by L<Thrift::Parser::Method> and L<Thrift::Parser::Struct>. Given a list of key/value pairs, returns a FieldSet object informed by the IDL. | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub compose { | ||
| 128 | my ($self_class, $class, %args) = @_; | ||
| 129 | |||
| 130 | if (! $class->idl) { | ||
| 131 | die "Requires an IDL"; | ||
| 132 | } | ||
| 133 | |||
| 134 | if (! $class->idl->can('field_id')) { | ||
| 135 | Thrift::Parser::InvalidArgument->throw( | ||
| 136 | key => 'class', value => $class, error => "Doesn't support field_id()", | ||
| 137 | ); | ||
| 138 | } | ||
| 139 | |||
| 140 | # Check for missing non-optional fields | ||
| 141 | |||
| 142 | foreach my $field (@{ $class->idl->fields }) { | ||
| 143 | my $default_value = $field->default_value; | ||
| 144 | |||
| 145 | # User may pass '_:id' or the name of the field | ||
| 146 | my $key = defined $args{ '_' . $field->id } ? '_' . $field->id : $field->name; | ||
| 147 | |||
| 148 | if (defined $default_value && ! defined $args{$key}) { | ||
| 149 | $args{$key} = $default_value; | ||
| 150 | next; | ||
| 151 | } | ||
| 152 | |||
| 153 | if (! defined $default_value && ! defined $args{$key} && ! $field->optional && $class->isa('Thrift::Parser::Type::Struct')) { | ||
| 154 | Thrift::Parser::InvalidArgument->throw("Missing value for field '".$field->name."' in $class compose()"); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | my @fields; | ||
| 159 | foreach my $key (keys %args) { | ||
| 160 | # Determine the IDL type of the field | ||
| 161 | my $idl_field; | ||
| 162 | if (my ($id) = $key =~ m{^_(\d+)$}) { | ||
| 163 | $idl_field = $class->idl->field_id($id); | ||
| 164 | } | ||
| 165 | else { | ||
| 166 | $idl_field = $class->idl->field_named($key); | ||
| 167 | } | ||
| 168 | if (! $idl_field) { | ||
| 169 | Thrift::Parser::InvalidArgument->throw( | ||
| 170 | error => "Failed to find referenced field '$key' in the $class IDL spec", | ||
| 171 | key => $key, value => $args{$key}, | ||
| 172 | ); | ||
| 173 | } | ||
| 174 | my $type = $idl_field->type; | ||
| 175 | |||
| 176 | # Cast into the new value | ||
| 177 | |||
| 178 | my $type_class; | ||
| 179 | if ($type->isa('Thrift::IDL::Type::Custom')) { | ||
| 180 | my $type_name = $type->full_name; | ||
| 181 | my $referenced_type = $class->idl_doc->object_full_named($type_name); | ||
| 182 | if (! $referenced_type) { | ||
| 183 | Thrift::Parser::InvalidSpec->throw("Couldn't find definition of custom type '".$type_name."'"); | ||
| 184 | } | ||
| 185 | my $namespace = $referenced_type->{header}->namespace('perl'); | ||
| 186 | $type_class = join '::', (defined $namespace ? ($namespace) : ()), $type->local_name; | ||
| 187 | } | ||
| 188 | else { | ||
| 189 | $type_class = 'Thrift::Parser::Type::' . $type->name; | ||
| 190 | } | ||
| 191 | |||
| 192 | my $value = $type_class->compose_with_idl($type, $args{$key}); | ||
| 193 | |||
| 194 | my $field = Thrift::Parser::Field->new({ | ||
| 195 | id => $idl_field->id, | ||
| 196 | value => $value, | ||
| 197 | name => $idl_field->name, | ||
| 198 | }); | ||
| 199 | push @fields, $field; | ||
| 200 | } | ||
| 201 | |||
| 202 | return $self_class->new({ fields => \@fields }); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub write { | ||
| 206 | my ($self, $output) = @_; | ||
| 207 | |||
| 208 | $output->writeStructBegin(); | ||
| 209 | foreach my $field (@{ $self->{fields} }) { | ||
| 210 | $field->write($output); | ||
| 211 | } | ||
| 212 | $output->writeFieldStop(); | ||
| 213 | $output->writeStructEnd(); | ||
| 214 | } | ||
| 215 | |||
| 216 | =head1 COPYRIGHT | ||
| 217 | |||
| 218 | 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. | ||
| 219 | |||
| 220 | The full text of the license can be found in the LICENSE file included with this module. | ||
| 221 | |||
| 222 | =head1 AUTHOR | ||
| 223 | |||
| 224 | Eric Waters <ewaters@gmail.com> | ||
| 225 | |||
| 226 | =cut | ||
| 227 | |||
| 228 | 1; |