Skip Menu |
 

This queue is for tickets about the Data-Domain CPAN distribution.

Report information
The Basics
Id: 106002
Status: resolved
Priority: 0/
Queue: Data-Domain

People
Owner: Nobody in particular
Requestors: djerius [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Finding the key for an invalid value in a hash
Download (untitled) / with headers
text/plain 1.8k
I'm attempting to create an error message for a hash value which incorporates the value's key. The twist is that I'm using the -values option, and I don't know how to get Data::Domain to provide the key for a particular value. All values should be numeric, e.g. my $domain = Struct( -values => List( Num ) ); I'd like the error message to indicate which key had an invalid value. The default message structure looks like this: $Result = { '-values' => [ 'Num: invalid number' ] }; Which doesn't provide the key I thought I could use a lazy constructor and get access to the path via the context, but it doesn't create a path which is traversable, even for the value: my $domain = Struct( -values => List( sub { my $ctxt = shift; Dump( [ $ctxt->{path} ], ['Path'] ); Dump( [ node_from_path( $ctxt->{root}, @{ $ctxt->{path} } ) ], ['Node'] ); Num; } ) ); results in: $Path = [ '-values', 0 ]; $Node = undef; I had hoped to simply change the '-values' to '-keys'. Even if the path were traversable, that would require that the values and keys were iterated in the same order. Is there any means of identifying the key for a bogus value? Thanks, Diab Here's my sample code: use Data::Dumper; use Data::Domain -all; my %data = ( foo => 'bar' ); sub Dump { print Data::Dumper->new( @_ )->Dump; } my $domain = Struct( -values => List( Num ) ); Dump [ $domain->inspect( \%data ) ], [ 'Result' ]; my $domain = Struct( -values => List( sub { my $ctxt = shift; Dump( [ $ctxt->{path} ], ['Path'] ); Dump( [ node_from_path( $ctxt->{root}, @{ $ctxt->{path} } ) ], ['Node'] ); Num; } ) ); $domain->inspect( \%data );
Subject: Re: [rt.cpan.org #106002] Finding the key for an invalid value in a hash
Date: Tue, 21 Jul 2015 22:59:46 +0200
To: bug-Data-Domain [...] rt.cpan.org
From: Laurent Dami <laurent.dami [...] free.fr>
Download (untitled) / with headers
text/plain 2.6k
Le 21.07.2015 21:12, Diab Jerius via RT a écrit : Show quoted text
> Tue Jul 21 15:12:31 2015: Request 106002 was acted upon. > Transaction: Ticket created by DJERIUS > Queue: Data-Domain > Subject: Finding the key for an invalid value in a hash > Broken in: (no value) > Severity: (no value) > Owner: Nobody > Requestors: djerius@cpan.org > Status: new > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=106002 > > > > I'm attempting to create an error message for a hash value which > incorporates the value's key. > > The twist is that I'm using the -values option, and I don't know how > to get Data::Domain to provide the key for a particular value. > > All values should be numeric, e.g. > > my $domain = Struct( -values => List( Num ) ); > > I'd like the error message to indicate which key had an invalid value. > The default message structure looks like this: > > $Result = { > '-values' => [ > 'Num: invalid number' > ] > }; > > Which doesn't provide the key > > I thought I could use a lazy constructor and get access to the path > via the context, but it doesn't create a path which is traversable, > even for the value: > > my $domain = Struct( > -values => List( > sub { > my $ctxt = shift; > Dump( [ $ctxt->{path} ], ['Path'] ); > Dump( [ node_from_path( $ctxt->{root}, @{ $ctxt->{path} } ) ], > ['Node'] ); > Num; > } ) ); > > results in: > > $Path = [ > '-values', > 0 > ]; > $Node = undef; > > > I had hoped to simply change the '-values' to '-keys'. Even if the > path were traversable, that would require that the values and keys > were iterated in the same order. > > Is there any means of identifying the key for a bogus value? > > Thanks, > > Diab > > > > > Here's my sample code: > > > > use Data::Dumper; > > use Data::Domain -all; > > my %data = ( foo => 'bar' ); > > sub Dump { print Data::Dumper->new( @_ )->Dump; } > > > my $domain = Struct( -values => List( Num ) ); > > Dump [ $domain->inspect( \%data ) ], [ 'Result' ]; > > my $domain = Struct( > -values => List( > sub { > my $ctxt = shift; > Dump( [ $ctxt->{path} ], ['Path'] ); > Dump( [ node_from_path( $ctxt->{root}, @{ $ctxt->{path} } ) ], > ['Node'] ); > Num; > } ) ); > > $domain->inspect( \%data ); >
Hi, A lazy domain is meant to dynamically customize the constraints upon a domain; so this not what you want here. What you need to to is to subclass the Data::Domain::Struct class, and implement an _inspect method that incorporates the key information into the message. Cheers, Laurent D.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.