Skip to content

Commit

Permalink
update changes
Browse files Browse the repository at this point in the history
  • Loading branch information
peczenyj committed Dec 12, 2023
1 parent 3060940 commit 3bc5ca9
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
- increase TO_JSON performance by 17% on bitfields and 70% on range based
- increase TO_JSON performance by 2x on bitfields and 26x on range based vendor section
- add json section 'publisher' and include all publisher restriction, if any, per purpose id, vendor id and restriction type

0.081
Expand Down
11 changes: 3 additions & 8 deletions lib/GDPR/IAB/TCFv2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,9 @@ sub TO_JSON {
),
},
vendor => {
consents => $self->_format_json_subsection2(
$self->{vendor_consents}->all,
$self->max_vendor_id_consent,
),
legitimate_interests => $self->_format_json_subsection2(
$self->{vendor_legitimate_interests}->all,
$self->max_vendor_id_legitimate_interest,
),
consents => $self->{vendor_consents}->TO_JSON,
legitimate_interests =>
$self->{vendor_legitimate_interests}->TO_JSON,
},
publisher => {
restrictions => $self->{publisher_restrictions}->TO_JSON,
Expand Down
43 changes: 40 additions & 3 deletions lib/GDPR/IAB/TCFv2/BitField.pm
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,47 @@ sub all {

my @data = split //, $self->{data};

return [ grep { $data[ $_ - 1 ] } 1 .. $self->{max_vendor_id} ];
}

sub TO_JSON {
my $self = shift;

my @data = split //, $self->{data};

if ( !!$self->{options}->{json}->{compact} ) {
return [ grep { $data[ $_ - 1 ] } 1 .. $self->{max_vendor_id} ];
}

my ( $false, $true ) = @{ $self->{options}->{json}->{boolean_values} };

return { map { $_ => $data[ $_ - 1 ] ? $true : $false }
1 .. $self->{max_vendor_id} };
if ( !!$self->{options}->{json}->{verbose} ) {
return { map { $_ => $data[ $_ - 1 ] ? $true : $false }
1 .. $self->{max_vendor_id} };
}

return {
map { $_ => $true }
grep { $data[ $_ - 1 ] } 1 .. $self->{max_vendor_id}
};
}

sub _format_json_subsection2 {
my ( $self, $data, $max ) = @_;

my ( $false, $true ) = @{ $self->{options}->{json}->{boolean_values} };

if ( !!$self->{options}->{json}->{compact} ) {
return [
grep { $data->{$_} } 1 .. $max,
];
}

my $verbose = !!$self->{options}->{json}->{verbose};

return $data if $verbose;

return { map { $_ => $true } grep { $data->{$_} } keys %{$data} };
}

1;
Expand Down Expand Up @@ -117,4 +154,4 @@ Returns the max vendor id.
=head2 all
Returns an hashref of all vendors mapped to the bit enable (returns true or false).
Returns an array of all vendors mapped with the bit enabled.
2 changes: 1 addition & 1 deletion lib/GDPR/IAB/TCFv2/PublisherRestrictions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ sub TO_JSON {
foreach my $restrict_type ( keys %{$restriction_map} ) {
my $vendors = $restriction_map->{$restrict_type}->all;

foreach my $vendor ( grep { $vendors->{$_} } keys %{$vendors} ) {
foreach my $vendor ( @{$vendors} ) {
$purpose_restrictions{$vendor} = int($restrict_type);
}
}
Expand Down
14 changes: 13 additions & 1 deletion lib/GDPR/IAB/TCFv2/RangeConsent.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ sub all {

my ( $false, $true ) = @{ $self->{options}->{json}->{boolean_values} };

return [ $self->{start} .. $self->{end} ];
}

sub TO_JSON {
my $self = shift;

if ( !!$self->{options}->{json}->{compact} ) {
return [ $self->{start} .. $self->{end} ];
}

my ( $false, $true ) = @{ $self->{options}->{json}->{boolean_values} };

return { map { $_ => $true } $self->{start} .. $self->{end} };
}

Expand Down Expand Up @@ -83,4 +95,4 @@ Return true if the id is present on the range [start, end]
=head2 all
Returns an hashref of all vendors mapped to the bit enable (returns true or false).
Returns an arrayref of all vendors mapped with the bit enabled (or all vendors in range).
31 changes: 28 additions & 3 deletions lib/GDPR/IAB/TCFv2/RangeSection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,37 @@ sub contains {
sub all {
my $self = shift;

my @vendors;
foreach my $range_consent ( @{ $self->{range_consents} } ) {
push @vendors, @{ $range_consent->all };
}

return \@vendors;
}

sub TO_JSON {
my $self = shift;

if ( !!$self->{options}->{json}->{compact} ) {
my @vendors;

foreach my $range_consent ( @{ $self->{range_consents} } ) {
push @vendors, @{ $range_consent->TO_JSON }
; # todo use all when we convert back to arrayref
}

return \@vendors;
}

my ( $false, $true ) = @{ $self->{options}->{json}->{boolean_values} };

my %map = map { $_ => $false } 1 .. $self->{max_vendor_id};
my %map;
if ( !!$self->{options}->{json}->{verbose} ) {
%map = map { $_ => $false } 1 .. $self->{max_vendor_id};
}

foreach my $range_consent ( @{ $self->{range_consents} } ) {
%map = ( %map, %{ $range_consent->all } );
%map = ( %map, %{ $range_consent->TO_JSON } );
}

return \%map;
Expand Down Expand Up @@ -185,6 +210,6 @@ Returns the max vendor id.
=head2 all
Returns an hashref of all vendors mapped to the bit enable (returns true or false).
Returns an arrayref of all vendors mapped with the bit enabled.
It is a combination of all the responses of L<GDPR::IAB::TCFv2::RangeConsent#all>.
8 changes: 4 additions & 4 deletions t/00-load.t
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ subtest "check interfaces" => sub {
isa_ok 'GDPR::IAB::TCFv2::Constants::SpecialFeature', 'Exporter';
isa_ok 'GDPR::IAB::TCFv2::Constants::RestrictionType', 'Exporter';

my @methods = qw<Parse max_vendor_id contains all>;
my @methods = qw<Parse max_vendor_id contains all TO_JSON>;

can_ok 'GDPR::IAB::TCFv2::BitField', @methods;
can_ok 'GDPR::IAB::TCFv2::RangeSection', @methods;

can_ok 'GDPR::IAB::TCFv2::RangeConsent', 'new', 'contains', 'all';
can_ok 'GDPR::IAB::TCFv2::RangeConsent', qw<new contains all TO_JSON>;

can_ok 'GDPR::IAB::TCFv2::PublisherRestrictions', 'new',
'check_publisher_restriction', 'TO_JSON';
can_ok 'GDPR::IAB::TCFv2::PublisherRestrictions',
qw<new check_publisher_restriction TO_JSON>;

done_testing;
};
Expand Down

0 comments on commit 3bc5ca9

Please sign in to comment.