Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Properly handle repeated field attribute generation
Create our own container_descriptor and auto_viv_container to properly
handle array-valued (repeated field) attributes.
  • Loading branch information
Geoffrey Broadwell committed Nov 23, 2013
1 parent f0f3e45 commit ef8832c
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions lib/PB/Model/Generator.pm
Expand Up @@ -97,8 +97,34 @@ class PB::Model::Generator {
when 'repeated' { RepeatClass::REPEATED }
}

my $attr = PB::Attribute.new(:name('$!' ~ $field.name), :$type,
:package($class), :has_accessor);
my ($sigil, $container, $constraint);
if $repeat ~~ RepeatClass::REPEATED {
# XXXX: Specifically not parameterizing this to avoid
# XXXX: constraint checking bugs and unintuitiveness
$sigil = '@';
$container := Array;
$constraint := Positional;
}
else {
$sigil = '$';
$container := Scalar;
$constraint := $type;
}

my $attr-name = $sigil ~ '!' ~ $field.name;
my Mu $cd := ContainerDescriptor.new(:name($attr-name), :rw,
:of($type),
:default($type));
my Mu $cont := nqp::create($container);
nqp::bindattr($cont, $container, '$!descriptor', $cd);
nqp::bindattr($cont, $container, '$!value', $type)
if $container =:= Scalar;

my Mu $attr := PB::Attribute.new(:name($attr-name),
:type($constraint),
:package($class), :has_accessor,
:auto_viv_container($cont),
:container_descriptor($cd));
$attr.pb_type = $pb_type;
$attr.pb_name = $field.name;
$attr.pb_number = $field.number;
Expand Down

0 comments on commit ef8832c

Please sign in to comment.