Skip Menu |

This queue is for tickets about the MooseX-Types-Locale-Country CPAN distribution.

Report information
The Basics
Id: 71077
Status: open
Priority: 0/
Queue: MooseX-Types-Locale-Country

People
Owner: MORIYA [...] cpan.org
Requestors: xenoterracide [...] gmail.com
Cc:
AdminCc:

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



Subject: why not code conversions?
subtype CountryCode, as Alpha2Country ; So I've added the following to my own code base, and tested them and they seem to work. So why aren't they supported directly by the module? coerce CountryCode, from Alpha3Country, via { return uc country_code2code( $_ , LOCALE_CODE_ALPHA_3, LOCALE_CODE_ALPHA_2 ); } ; coerce CountryCode, from CountryName, via { return uc country2code( $_, LOCALE_CODE_ALPHA_2 ); } ;
Hello xeno, I appreciate your selecting my module and your querying about coercion. And I'm sorry for my wrong operation on RT (deletion of the ticket). I received your query: * "Why the coercions operating on only letter-case?" * "Why not code to code conversions?" * "Why not code to name conversions?" Just then, I will release the succeeding module "MooseX::Types::Locale::Codes" ( https://github.com/gardejo/p5-moosex-types-locale-codes/ ), until "MooseX::Types::Parameterizable" solves an issue about coercion ( https://github.com/jjn1056/MooseX-Types-Parameterizable/issues/5 ). So, I notify the reasons of the behaviors and my future plans to you. Case 1. some code set to other code set --------------------------------------- * concrete example: CountryCode->check('GB') * current result: true * coming result: true * concrete example: CountryCode->check('UK') * current result: false * coming result: false * concrete example: CountryCode->coerce('UK') * current result: 'UK' * coming result: 'UK' (not 'GB') For a technical reason, I'm taking the liberty to omission of the coercion. See "case1_code_to_code.t". Case 2. name to code -------------------- * concrete example: CountryCode->coerce('United States') * current result: 'UNITED STATES' * coming result: 'US' I have a good idea about what you mean. I will do it on the succeeding module, "MooseX::Types::Locale::Codes::Country". coerce CountryCode, from CountryName, ... CountryCode->coerce($country_name_from_alpha_2); See "case2_name_to_code.t". Also, I confirmed that all country names from all code sets don't conflict with all country codes by test cases. See "case2_name_does_not_conflict_with_code.t". By the way, you wouldn't want coercion from name (on some code set) into code (on "other" code set), would you? (CountryCode['alpha-3'])->coerce($country_name_from_alpha_2); Case 3. aliased name to canonical name -------------------------------------- * concrete example: CountryName->check('Great Britain') * current result: false * coming result: false * concrete example: CountryName->coerce('Great Britain') * current result: 'United Kingdom' * coming result: 'United Kingdom' I did it on the current module and the succeeding module, "MooseX::Types::Locale::Codes::Country". See "case3_alias_to_name.t". Case 4. code to name -------------------- * concrete example: CountryName->coerce('JP') * current result: 'JP' * coming result: (I have no suggestion yet) * Do you want 'Japan'? You didn't refer to the conversion, but it is. See "case4_code_to_name.t". In my humble opinion, there is low demand for the "reverse" coercion. Could you, an user of the module, tell me about your demand? Also, I confirmed that all country codes from all code sets don't conflict with all country names by test cases. See "case4_code_does_not_conflict_with_name.t". Regards, -- MORIYA Masaki, alias Gardejo
Subject: case1_code_to_code.t
use strict; use warnings; use Locale::Country; use MooseX::Types::Locale::Country qw(CountryCode); use Test::More; plan tests => 4; my $name = 'United Kingdom'; my $code = 'gb'; cmp_ok( code2country($code, 'alpha2'), 'eq', $name, 'Have a good tea time.', ); ok( CountryCode->check(uc $code), 'Type constraint follows the behavior of Locale::Country', ); cmp_ok( code2country($code, 'fips'), 'ne', $name, 'Their Queen is at not her country but Gabon.', ); cmp_ok( CountryCode->coerce('UK'), 'ne', 'GB', 'UK is not in country code as alpha-2 code set', ); __END__ =head1 DESCRIPTION If we add L<C<country_code2code()> |Locale::Country/country_code2code ( CODE ,CODESET ,CODESET2 )>-like code conversion to L<C<CountryCode>|MooseX::Types::Locale::Country/CountryCode> constraint, we will face with a confusion between code sets. I do not implement L<C<fips-10>|Locale::Country/fips-10> code set on L<MooseX::Types::Locale::Country> yet, but I will do it sooner or later on the succeeding module, L<C<MooseX::Types::Locale::Codes::Country> |https://github.com/gardejo/p5-moosex-types-locale-codes/>. Therefore, I am taking the liberty of omission of I<code to code> coercion.
Subject: case4_code_does_not_conflict_with_name.t
use strict; use warnings; use Locale::Country; use Test::More; my @code_sets = qw(alpha2 alpha3 num fips dom); foreach my $code_set_for_codes (@code_sets) { foreach my $code (all_country_codes($code_set_for_codes)) { foreach my $code_set_for_names (@code_sets) { my $converted = country2code($code, $code_set_for_names); # abuse ok( ( ! defined $converted # abuse brings no unexpected result || code2country($converted, $code_set_for_names) eq code2country( country2code($code, $code_set_for_names), $code_set_for_names ) # 'US', 'USA' and 'UK' are okay ), sprintf( 'Code %s from code set %s does not conflict name ' . 'from code sets %s.', $code, $code_set_for_codes, $code_set_for_names, ), ); } } } done_testing; __END__ =head1 DESCRIPTION I confirmed that all country codes from all code sets do not conflict with all country names by the test cases.
Subject: case2_name_to_code.t
use strict; use warnings; use Locale::Country; use MooseX::Types::Locale::Country qw(CountryCode CountryName); use Test::More; plan tests => 5; my $code_set = 'alpha2'; my $name = 'United States'; my $code = 'us'; cmp_ok( country2code($name, $code_set), 'eq', $code, "$name is in country name", ); ok( defined CountryName->check($name), "$name is in country name", ); ok( ! defined code2country($name, $code_set), # abuse "$name is not in country code", ); ok( ! defined CountryCode->check($name), # abuse "$name is not in country code", ); TODO: { local $TODO = 'Currently coercion does not support the conversion'; cmp_ok( CountryCode->coerce($name), 'eq', uc $code, "Attempt coercing from $name into $code", ); }; __END__ =head1 DESCRIPTION It is needed by some measurs. For example, C<United States> is C<us> as L<C<alpha-2>|Locale::Country/alpha-2> code set. I did not implement the I<name to code> behavior on L<MooseX::Types::Locale::Country> yet, but will do it sooner or later on the succeeding module, L<C<MooseX::Types::Locale::Codes::Country> |https://github.com/gardejo/p5-moosex-types-locale-codes/>. I will try to live up to your expectations!
Subject: case3_alias_to_name.t
use strict; use warnings; use Locale::Country; use MooseX::Types::Locale::Country qw(CountryName); use Test::More; plan tests => 5; my $code_set = 'alpha2'; my $name = 'United Kingdom'; cmp_ok( country2code($name, $code_set), 'eq', 'gb', "$name is in country name", ); ok( CountryName->check($name), "$name is in country name", ); my $alias = 'Great Britain'; cmp_ok( country2code($alias, $code_set), 'eq', 'gb', "$alias is in country name as canonical name $name", ); ok( ! CountryName->check($alias), "$alias is in country name but it is not canonical name $name", ); is( CountryName->coerce($alias), $name, "$alias is in country name as canonical name $name", ); __END__ =head1 DESCRIPTION For example, C<United Kingdom> and its aliased name C<Great Britain> are C<gb> as L<C<alpha-2>|Locale::Country/alpha-2> code set.
Subject: case2_name_does_not_conflict_with_code.t
use strict; use warnings; use Locale::Country; use Test::More; my @code_sets = qw(alpha2 alpha3 num fips dom); foreach my $code_set_for_names (@code_sets) { foreach my $name (all_country_names($code_set_for_names)) { foreach my $code_set_for_codes (@code_sets) { my $converted = code2country($name, $code_set_for_codes); # abuse ok( ! defined $converted, # abuse brings no unexpected result sprintf( 'Name %s from code set %s does not conflict code ' . 'from code sets %s.', $name, $code_set_for_names, $code_set_for_codes, ), ); } } } done_testing; __END__ =head1 DESCRIPTION I confirmed that all country names from all code sets do not conflict with all country codes by the test cases.
Subject: case4_code_to_name.t
use strict; use warnings; use Locale::Country; use MooseX::Types::Locale::Country qw(CountryCode CountryName); use Test::More; plan tests => 5; my $code_set = 'alpha2'; my $name = 'Japan'; my $code = 'jp'; cmp_ok( code2country($code, $code_set), 'eq', $name, "$code is in country code", ); ok( defined CountryCode->check(uc $code), "$code is in country code", ); ok( ! defined country2code($code, $code_set), # abuse "$code is not in country name", ); ok( ! defined CountryName->check($code), # abuse "$code is not in country name", ); TODO: { local $TODO = 'Currently coercion does not support the conversion'; cmp_ok( CountryName->coerce(uc $code), 'eq', $name, "Attempt coercing from $code into $name", ); }; __END__ =head1 DESCRIPTION It is needed by some measurs. For example, C<Japan> is C<jp> as L<C<alpha-2>|Locale::Country/alpha-2> code set. I did not implement the I<code to name> behavior on L<MooseX::Types::Locale::Country> yet, but maybe do it on the succeeding module, L<C<MooseX::Types::Locale::Codes::Country> |https://github.com/gardejo/p5-moosex-types-locale-codes/>.
Subject: Re: [rt.cpan.org #71077] why not code conversions?
Date: Wed, 21 Sep 2011 15:46:46 -0500
To: bug-MooseX-Types-Locale-Country [...] rt.cpan.org
From: Caleb Cushing <xenoterracide [...] gmail.com>
On Tue, Sep 20, 2011 at 12:27 PM, MORIYA Masaki via RT <bug-MooseX-Types-Locale-Country@rt.cpan.org> wrote: Show quoted text
> In my humble opinion, there is low demand for the "reverse" coercion. > Could you, an user of the module, tell me about your demand? > > Also, I confirmed that all country codes from all code sets don't conflict > with all country names by test cases. > See "case4_code_does_not_conflict_with_name.t".
My primary use case is in Business::CyberSource which requires that we send a 2 character country code, but unfortunately our crappy database has everything from name, 3 character, and 2 character country code. So I need to coerce them into a 2 character country code. For me other coercions seem complimentary is all. -- Caleb Cushing http://xenoterracide.com
Hello, Thank you for your reply. Show quoted text
> My primary use case is in Business::CyberSource which requires that we > send a 2 character country code, but unfortunately our crappy database > has everything from name, 3 character, and 2 character country code.
I understand your situation. All right, I will have the pleasure of granting your wishes (on following cases 2 and 4) by 'MooseX::Types::Locale::Codes::Country' in the succeeding distribution 'MooseX-Types-Locale-Codes' :) https://github.com/gardejo/p5-moosex-types-locale-codes/ So I began respond to the ticket on new branch 'rt-71077'. https://github.com/gardejo/p5-moosex-types-locale-codes/tree/rt-71077 Thank you for your patience until I accomplish it and dependency module MooseX::Types::Parameterizable solves an issue. Case 1. Canonize code about letter-case --------------------------------------- I have been supporting it already. * CountryCode->coerce('CA'); # It as 'Canada' is in 'alpha-2' * 'ca' # 'alpha-2' is lower case * $l = CountryCode['alpha-2']; $l->coerce('CA'); # same as above Note: These letter-cases may change from current behavior. It will respect for Locale::Codes::Country. Case 2. Convert name to code between same code sets --------------------------------------------------- I will support the conversion which can fit for you. * CountryCode->coerce('united states'); # It is in 'alpha-2' * 'us' # 'alpha-2' name to 'alpha-2' code * CountryCode->coerce('GREAT BRITAIN'); # It is in 'alpha-2' * 'gb' # 'alpha-2' name to 'alpha-2' code Case 3. Convert name to code between different code sets -------------------------------------------------------- I am not going to support the conversion. Because it would seem that it due to lack of demand. * CountryCode->coerce('soviet union'); # It isn't in 'alpha-2' * (throw exception) cf. Case 2. * $l = CountryCode['domain']; $l->coerce('soviet union'); * 'su' # 'domain' name to 'domain' code Case 4. Convert code from some code set to other one ---------------------------------------------------- I will *generality* support the conversion which can fit for you. * CountryCode->coerce('CAN'); It as 'Canada' is in 'alpha-3' * 'ca' # 'alpha-3' code to 'alpha-2' code But, I am *partially* taking the liberty of omission of the conversion between some combinations of code sets. Concretely, 2 characters code sets, namely 'alpha-2', 'fips-10' and 'domain'. * CountryCode->coerce('GM'); # It as 'Germany' is in 'fips-10' * (throw exception) # It (instead of 'DE') isn't in 'alpha-2' * CountryCode->coerce('EU'); # It as 'European Union' is in 'domain' * (throw exception) # It isn't in 'alpha-2' cf. Case 6. * CountryCode->coerce('UK'); # It as 'United Kingdom' is in 'domain' * 'gb' # 'alpha-2' alias to 'alpha-2' name # never 'domain' code to 'alpha-2' code What it comes down to is: from\to | alpha-2 alpha-3 numeric fips-10 domain --------+-------------------------------------------- alpha-2 | - convert convert no-op no-op alpha-3 | convert - convert convert convert numeric | convert convert - convert convert fips-10 | no-op convert convert - no-op domain | no-op convert convert no-op - Because 'fips-10' and 'domain' rather have a reduced frequency of use in comparison with 'alpha-2', it would seem that the limitation doesn't retard use cases with the inclusion of yours. Case 5. Canonize name about letter-case --------------------------------------- I have been supporting it already. * CountryName->coerce('canada'); * 'Canada' Case 6. Canonize name from alias -------------------------------- I have been supporting it already. * CountryName->coerce('united states of america'); * 'United States' # 'alpha-2' alias to 'alpha-2' name * CountryName->coerce('usa'); # It was intended as alias * 'United States' # 'alpha-2' alias to 'alpha-2' name # never 'alpha-3' code to 'alpha-2' name * CountryName->coerce('uk'); # It was intended as alias * 'United Kingdom' # 'alpha-2' alias to 'alpha-2' name # never 'fips-10' code to 'alpha-2' name Case 7. Convert code to name between same code sets --------------------------------------------------- I will use this opportunity to support the conversion. * CountryName->coerce('gb'); * 'United Kingdom' # 'alpha-2' code to 'alpha-2' name Case 8. Convert code to name between different code sets -------------------------------------------------------- I am not going to support the conversion. Because it would seem that it due to lack of demand. * CountryName->coerce('su'); # It is in 'domain' * (throw exception) # It isn't in 'alpha-2' cf. Case 6. * CountryName->coerce('uk'); # It is in 'fips-10' * 'United Kingdom' # 'alpha-2' alias to 'alpha-2' name # never 'fips-10' code to 'alpha-2' name cf. Case 7. * $l = CountryName['domain']; $l->coerce('su'); # It is in 'domain' * 'Soviet Union' # 'domain' code to 'domain' name Case 9. Convert name from some code set to other one ---------------------------------------------------- It has same reason as Case 8. * CountryName->coerce('soviet union'); # It is in 'domain' * (throw exception) # It isn't in 'alpha-2' Regards, -- MORIYA Masaki, alias Gardejo
Subject: Re: [rt.cpan.org #71077] why not code conversions?
Date: Thu, 22 Sep 2011 13:28:47 -0500
To: bug-MooseX-Types-Locale-Country [...] rt.cpan.org
From: Caleb Cushing <xenoterracide [...] gmail.com>
On Thu, Sep 22, 2011 at 12:50 PM, MORIYA Masaki via RT <bug-MooseX-Types-Locale-Country@rt.cpan.org> wrote: Show quoted text
> All right, I will have the pleasure of granting your wishes (on > following cases 2 and 4) by 'MooseX::Types::Locale::Codes::Country' > in the succeeding distribution 'MooseX-Types-Locale-Codes' :) > https://github.com/gardejo/p5-moosex-types-locale-codes/
I've been looking that over, and I opened an issue there regarding "sub countries". Is there anything I can do to help you accomplish these things? -- Caleb Cushing http://xenoterracide.com
Thank you for your remarkable suggestion. Please visit the issue on GitHub for further information. https://github.com/gardejo/p5-moosex-types-locale-codes/issues/1 I made a new RT ticket #71170 for issue(ticket)-driven development. https://rt.cpan.org/Ticket/Display.html?id=71170 I set you on CC, so you can receive the information. If it's not urgent, would you mind waiting for few days? I will try to make a prototype. Regards, -- MORIYA Masaki, alias Gardejo