Skip to content

Commit

Permalink
Merge pull request #115 from jefftt/shortnumber-ptrs
Browse files Browse the repository at this point in the history
shortnumber_info: Accept pointer in APIs
  • Loading branch information
rowanseymour authored Jun 22, 2022
2 parents d9769c3 + 44ba426 commit a4d0f6e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
20 changes: 10 additions & 10 deletions shortnumber_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func loadShortNumberMetadataFromFile() error {
// multiple regions, this returns true if it's possible in any of them. This provides a more
// lenient check than #isValidShortNumber.
// See IsPossibleShortNumberForRegion(PhoneNumber, string) for details.
func IsPossibleShortNumber(number PhoneNumber) bool {
func IsPossibleShortNumber(number *PhoneNumber) bool {
regionsCodes := GetRegionCodesForCountryCode(int(number.GetCountryCode()))
shortNumberLength := len(GetNationalSignificantNumber(&number))
shortNumberLength := len(GetNationalSignificantNumber(number))
for _, region := range regionsCodes {
phoneMetadata := getShortNumberMetadataForRegion(region)
if phoneMetadata == nil {
Expand All @@ -90,23 +90,23 @@ func IsPossibleShortNumber(number PhoneNumber) bool {

// Check whether a short number is a possible number when dialed from the given region. This
// provides a more lenient check than IsValidShortNumberForRegion.
func IsPossibleShortNumberForRegion(number PhoneNumber, regionDialingFrom string) bool {
func IsPossibleShortNumberForRegion(number *PhoneNumber, regionDialingFrom string) bool {
if !regionDialingFromMatchesNumber(number, regionDialingFrom) {
return false
}
phoneMetadata := getShortNumberMetadataForRegion(regionDialingFrom)
if phoneMetadata == nil {
return false
}
numberLength := len(GetNationalSignificantNumber(&number))
numberLength := len(GetNationalSignificantNumber(number))
return phoneMetadata.GeneralDesc.hasPossibleLength(int32(numberLength))
}

// Tests whether a short number matches a valid pattern. If a country calling code is shared by
// multiple regions, this returns true if it's valid in any of them. Note that this doesn't verify
// the number is actually in use, which is impossible to tell by just looking at the number
// itself. See IsValidShortNumberForRegion(PhoneNumber, String) for details.
func IsValidShortNumber(number PhoneNumber) bool {
func IsValidShortNumber(number *PhoneNumber) bool {
regionCodes := GetRegionCodesForCountryCode(int(number.GetCountryCode()))
regionCode := getRegionCodeForShortNumberFromRegionList(number, regionCodes)
if len(regionCodes) > 1 && regionCode != "" {
Expand All @@ -119,15 +119,15 @@ func IsValidShortNumber(number PhoneNumber) bool {

// Tests whether a short number matches a valid pattern in a region. Note that this doesn't verify
// the number is actually in use, which is impossible to tell by just looking at the number itself.
func IsValidShortNumberForRegion(number PhoneNumber, regionDialingFrom string) bool {
func IsValidShortNumberForRegion(number *PhoneNumber, regionDialingFrom string) bool {
if !regionDialingFromMatchesNumber(number, regionDialingFrom) {
return false
}
phoneMetadata := getShortNumberMetadataForRegion(regionDialingFrom)
if phoneMetadata == nil {
return false
}
shortNumber := GetNationalSignificantNumber(&number)
shortNumber := GetNationalSignificantNumber(number)
generalDesc := phoneMetadata.GeneralDesc
if !matchesPossibleNumberAndNationalNumber(shortNumber, generalDesc) {
return false
Expand All @@ -141,14 +141,14 @@ func getShortNumberMetadataForRegion(regionCode string) *PhoneMetadata {
return val
}

func getRegionCodeForShortNumberFromRegionList(number PhoneNumber, regionCodes []string) string {
func getRegionCodeForShortNumberFromRegionList(number *PhoneNumber, regionCodes []string) string {
if len(regionCodes) == 0 {
return ""
}
if len(regionCodes) == 1 {
return regionCodes[0]
}
nationalNumber := GetNationalSignificantNumber(&number)
nationalNumber := GetNationalSignificantNumber(number)
for _, regionCode := range regionCodes {
phoneMetadata := getShortNumberMetadataForRegion(regionCode)
if phoneMetadata != nil && matchesPossibleNumberAndNationalNumber(nationalNumber, phoneMetadata.GetShortCode()) {
Expand All @@ -161,7 +161,7 @@ func getRegionCodeForShortNumberFromRegionList(number PhoneNumber, regionCodes [

// Helper method to check that the country calling code of the number matches the region it's
// being dialed from.
func regionDialingFromMatchesNumber(number PhoneNumber, regionDialingFrom string) bool {
func regionDialingFromMatchesNumber(number *PhoneNumber, regionDialingFrom string) bool {
regionCodes := GetRegionCodesForCountryCode(int(number.GetCountryCode()))
for _, region := range regionCodes {
if region == regionDialingFrom {
Expand Down
16 changes: 8 additions & 8 deletions shortnumber_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ func TestIsPossibleShortNumber(t *testing.T) {
CountryCode: &countryCode,
NationalNumber: &nationalNumber,
}
assert.True(t, IsPossibleShortNumber(*possibleNumber))
assert.True(t, IsPossibleShortNumber(possibleNumber))

possibleNumber, err := Parse("123456", "FR")
if err != nil {
t.Errorf("Error parsing number: %s: %s", "123456", err)
}
assert.True(t, IsPossibleShortNumberForRegion(*possibleNumber, "FR"))
assert.True(t, IsPossibleShortNumberForRegion(possibleNumber, "FR"))

nationalNumber = 9
impossibleNumber := &PhoneNumber{
CountryCode: &countryCode,
NationalNumber: &nationalNumber,
}
assert.False(t, IsPossibleShortNumber(*impossibleNumber))
assert.False(t, IsPossibleShortNumber(impossibleNumber))

// Note that GB and GG share the country calling code 44, and that this number is possible but
// not valid.
Expand All @@ -41,7 +41,7 @@ func TestIsPossibleShortNumber(t *testing.T) {
CountryCode: &countryCode,
NationalNumber: &nationalNumber,
}
assert.True(t, IsPossibleShortNumber(*possibleNumber))
assert.True(t, IsPossibleShortNumber(possibleNumber))
}

func TestIsValidShortNumber(t *testing.T) {
Expand All @@ -51,24 +51,24 @@ func TestIsValidShortNumber(t *testing.T) {
CountryCode: &countryCode,
NationalNumber: &nationalNumber,
}
assert.True(t, IsValidShortNumber(*validNumber))
assert.True(t, IsValidShortNumber(validNumber))

validNumber, err := Parse("1010", "FR")
if err != nil {
t.Errorf("Error parsing number: %s: %s", "1010", err)
}
assert.True(t, IsValidShortNumberForRegion(*validNumber, "FR"))
assert.True(t, IsValidShortNumberForRegion(validNumber, "FR"))

nationalNumber = uint64(123456)
invalidNumber := &PhoneNumber{
CountryCode: &countryCode,
NationalNumber: &nationalNumber,
}
assert.False(t, IsValidShortNumber(*invalidNumber))
assert.False(t, IsValidShortNumber(invalidNumber))

invalidNumber, err = Parse("123456", "FR")
if err != nil {
t.Errorf("Error parsing number: %s: %s", "1010", err)
}
assert.False(t, IsValidShortNumberForRegion(*invalidNumber, "FR"))
assert.False(t, IsValidShortNumberForRegion(invalidNumber, "FR"))
}

0 comments on commit a4d0f6e

Please sign in to comment.