From bacdc0a76d4e078cc62993b38762f1405bf4dbbb Mon Sep 17 00:00:00 2001 From: lararennie Date: Mon, 3 Jul 2017 15:45:42 +0200 Subject: [PATCH] Adding a new enum for CountryCodeSource UNSPECIFIED (#1829) This enum won't be noticed by most users, because the country_code_source field of the phone number is only populated with ParseAndKeepRawInput, and in this case, this enum won't be used. However, it provides a better result for users who incorrectly call parse() and then getCountryCodeSource() and expect to get a sensible answer; now they will get UNSPECIFIED instead of FROM_NUMBER_WITH_PLUS_SIGN which was the default before. In JS this method returns null, but getCountryCodeSourceOrDefault() will now return UNSPECIFIED instead. The numeric values of the existing enums have not changed (relevant for JS and C++) and in Java the new enum was added to the end so that any stored serialized phone numbers should be able to be restored as they were before. Suggested in issue #1218 --- cpp/test/phonenumbers/phonenumberutil_test.cc | 4 ++++ .../google/i18n/phonenumbers/Phonenumber.java | 7 +++--- .../phonenumbers/PhoneNumberUtilTest.java | 4 ++++ javascript/i18n/phonenumbers/demo-compiled.js | 8 +++---- .../i18n/phonenumbers/phonenumber.pb.js | 24 +++++++++++++------ .../i18n/phonenumbers/phonenumberutil_test.js | 6 +++++ pending_code_changes.txt | 5 ++++ resources/phonenumber.proto | 7 +++++- 8 files changed, 50 insertions(+), 15 deletions(-) diff --git a/cpp/test/phonenumbers/phonenumberutil_test.cc b/cpp/test/phonenumbers/phonenumberutil_test.cc index 9c88307e7..b70b815d9 100644 --- a/cpp/test/phonenumbers/phonenumberutil_test.cc +++ b/cpp/test/phonenumbers/phonenumberutil_test.cc @@ -3492,6 +3492,10 @@ TEST_F(PhoneNumberUtilTest, ParseNationalNumber) { EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, phone_util_.Parse("033316005", RegionCode::NZ(), &test_number)); EXPECT_EQ(nz_number, test_number); + // Some fields are not filled in by Parse, but only by ParseAndKeepRawInput. + EXPECT_FALSE(nz_number.has_country_code_source()); + EXPECT_EQ(PhoneNumber::UNSPECIFIED, nz_number.country_code_source()); + // National prefix missing. EXPECT_EQ(PhoneNumberUtil::NO_PARSING_ERROR, phone_util_.Parse("33316005", RegionCode::NZ(), &test_number)); diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/Phonenumber.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/Phonenumber.java index 6ac5b83db..fe88a465c 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/Phonenumber.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/Phonenumber.java @@ -32,11 +32,12 @@ public final class Phonenumber { FROM_NUMBER_WITH_PLUS_SIGN, FROM_NUMBER_WITH_IDD, FROM_NUMBER_WITHOUT_PLUS_SIGN, - FROM_DEFAULT_COUNTRY + FROM_DEFAULT_COUNTRY, + UNSPECIFIED } public PhoneNumber() { - countryCodeSource_ = CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN; + countryCodeSource_ = CountryCodeSource.UNSPECIFIED; } // required int32 country_code = 1; @@ -156,7 +157,7 @@ public final class Phonenumber { } public PhoneNumber clearCountryCodeSource() { hasCountryCodeSource = false; - countryCodeSource_ = CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN; + countryCodeSource_ = CountryCodeSource.UNSPECIFIED; return this; } diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index e4fe0cb9f..8cb798c43 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -2045,6 +2045,10 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { public void testParseNationalNumber() throws Exception { // National prefix attached. assertEquals(NZ_NUMBER, phoneUtil.parse("033316005", RegionCode.NZ)); + // Some fields are not filled in by parse, but only by parseAndKeepRawInput. + assertFalse(NZ_NUMBER.hasCountryCodeSource()); + assertEquals(CountryCodeSource.UNSPECIFIED, NZ_NUMBER.getCountryCodeSource()); + assertEquals(NZ_NUMBER, phoneUtil.parse("33316005", RegionCode.NZ)); // National prefix attached and some formatting present. assertEquals(NZ_NUMBER, phoneUtil.parse("03-331 6005", RegionCode.NZ)); diff --git a/javascript/i18n/phonenumbers/demo-compiled.js b/javascript/i18n/phonenumbers/demo-compiled.js index 1ec1a2408..4c8379c83 100644 --- a/javascript/i18n/phonenumbers/demo-compiled.js +++ b/javascript/i18n/phonenumbers/demo-compiled.js @@ -2,7 +2,7 @@ function ca(a,b){if(b){for(var c=ba,d=a.split("."),e=0;ea||1342177279>>=1)b+=b;return d}});ca("Math.sign",function(a){return a?a:function(a){a=Number(a);return!a||isNaN(a)?a:0c?Math.max(0,a.length+c):c;if(k(a))return k(b)&&1==b.length?a.indexOf(b,c):-1;for(;cb?1:ac?Math.max(0,a.length+c):c;if(k(a))return k(b)&&1==b.length?a.indexOf(b,c):-1;for(;cb?1:a