From 3bc5ca9addad62917f8f511be0c20866bc22a99f Mon Sep 17 00:00:00 2001 From: Tiago Peczenyj Date: Tue, 12 Dec 2023 11:57:16 +0100 Subject: [PATCH] update changes --- Changes | 2 +- lib/GDPR/IAB/TCFv2.pm | 11 ++---- lib/GDPR/IAB/TCFv2/BitField.pm | 43 +++++++++++++++++++-- lib/GDPR/IAB/TCFv2/PublisherRestrictions.pm | 2 +- lib/GDPR/IAB/TCFv2/RangeConsent.pm | 14 ++++++- lib/GDPR/IAB/TCFv2/RangeSection.pm | 31 +++++++++++++-- t/00-load.t | 8 ++-- 7 files changed, 90 insertions(+), 21 deletions(-) diff --git a/Changes b/Changes index 685e064..007ff8f 100644 --- a/Changes +++ b/Changes @@ -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 diff --git a/lib/GDPR/IAB/TCFv2.pm b/lib/GDPR/IAB/TCFv2.pm index c722ea7..359d1e2 100644 --- a/lib/GDPR/IAB/TCFv2.pm +++ b/lib/GDPR/IAB/TCFv2.pm @@ -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, diff --git a/lib/GDPR/IAB/TCFv2/BitField.pm b/lib/GDPR/IAB/TCFv2/BitField.pm index 01b8bb8..1fa254b 100644 --- a/lib/GDPR/IAB/TCFv2/BitField.pm +++ b/lib/GDPR/IAB/TCFv2/BitField.pm @@ -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; @@ -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. diff --git a/lib/GDPR/IAB/TCFv2/PublisherRestrictions.pm b/lib/GDPR/IAB/TCFv2/PublisherRestrictions.pm index 2164d1c..be3f6cb 100644 --- a/lib/GDPR/IAB/TCFv2/PublisherRestrictions.pm +++ b/lib/GDPR/IAB/TCFv2/PublisherRestrictions.pm @@ -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); } } diff --git a/lib/GDPR/IAB/TCFv2/RangeConsent.pm b/lib/GDPR/IAB/TCFv2/RangeConsent.pm index 7998311..a11ef26 100644 --- a/lib/GDPR/IAB/TCFv2/RangeConsent.pm +++ b/lib/GDPR/IAB/TCFv2/RangeConsent.pm @@ -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} }; } @@ -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). diff --git a/lib/GDPR/IAB/TCFv2/RangeSection.pm b/lib/GDPR/IAB/TCFv2/RangeSection.pm index 9350b61..7732922 100644 --- a/lib/GDPR/IAB/TCFv2/RangeSection.pm +++ b/lib/GDPR/IAB/TCFv2/RangeSection.pm @@ -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; @@ -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. diff --git a/t/00-load.t b/t/00-load.t index a5c11bb..d9bd1ce 100644 --- a/t/00-load.t +++ b/t/00-load.t @@ -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; + my @methods = qw; 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; - can_ok 'GDPR::IAB::TCFv2::PublisherRestrictions', 'new', - 'check_publisher_restriction', 'TO_JSON'; + can_ok 'GDPR::IAB::TCFv2::PublisherRestrictions', + qw; done_testing; };