Skip to content

Commit

Permalink
save possible lengths to metadata, change implementation to use, add …
Browse files Browse the repository at this point in the history
…IS_POSSIBLE_LOCAL_ONLY and INVALID_LENGTH as possible return values to IsPossibleNumberWithReason
  • Loading branch information
nicpottier committed Aug 30, 2018
1 parent 0ba3a2b commit ab2a338
Show file tree
Hide file tree
Showing 12 changed files with 700 additions and 531 deletions.
52 changes: 16 additions & 36 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package phonenumbers
import (
"encoding/xml"
"fmt"
"math"
"regexp"
"sort"
"strconv"
"strings"
)
Expand Down Expand Up @@ -405,11 +405,6 @@ func processPhoneNumberDescElement(parentDesc *PhoneNumberDesc, element *PhoneNu
numberDesc := PhoneNumberDesc{}
if element == nil {
numberDesc.NationalNumberPattern = sp("NA")
numberDesc.PossibleNumberPattern = sp("NA")
// -1 will never match a possible phone number length, so is safe to use to ensure this never
// matches. We don't leave it empty, since for compression reasons, we use the empty list to
// mean that the generalDesc possible lengths apply.
//numberDesc.PossibleLength = append(numberDesc.PossibleLength, -1) [golang doesnt use possible length numbers]
return &numberDesc
}
if parentDesc != nil {
Expand Down Expand Up @@ -452,19 +447,19 @@ func setPossibleLengths(lengths map[int32]bool, localOnlyLengths map[int32]bool,

// Only add the lengths to this sub-type if they aren't exactly the same as the possible
// lengths in the general desc (for metadata size reasons).
// if parentDesc == nil || !arePossibleLengthsEqual(lengths, parentDesc) { [TODO: nicp: golang doesn't optimize?]
for length := range lengths {
if parentDesc == nil || parentDesc.hasPossibleLength(length) {
desc.PossibleLength = append(desc.PossibleLength, length)
} else {
// We shouldn't have possible lengths defined in a child element that are not covered by
// the general description. We check this here even though the general description is
// derived from child elements because it is only derived from a subset, and we need to
// ensure *all* child elements have a valid possible length.
panic(fmt.Sprintf("Out-of-range possible length found (%d), parent lengths %v.", length, parentDesc.PossibleLength))
if parentDesc == nil || !arePossibleLengthsEqual(lengths, parentDesc) {
for length := range lengths {
if parentDesc == nil || parentDesc.hasPossibleLength(length) {
desc.PossibleLength = append(desc.PossibleLength, length)
} else {
// We shouldn't have possible lengths defined in a child element that are not covered by
// the general description. We check this here even though the general description is
// derived from child elements because it is only derived from a subset, and we need to
// ensure *all* child elements have a valid possible length.
panic(fmt.Sprintf("Out-of-range possible length found (%d), parent lengths %v.", length, parentDesc.PossibleLength))
}
}
}
//}
// We check that the local-only length isn't also a normal possible length (only relevant for
// the general-desc, since within elements such as fixed-line we would throw an exception if we
// saw this) before adding it to the collection of possible local-only lengths.
Expand All @@ -475,31 +470,16 @@ func setPossibleLengths(lengths map[int32]bool, localOnlyLengths map[int32]bool,
// a valid national length for fixedLine, so the generalDesc would have the 7 removed from
// localOnly.
if parentDesc == nil || parentDesc.hasPossibleLength(length) || parentDesc.hasPossibleLengthLocalOnly(length) {
desc.PossibleLength = append(desc.PossibleLength, length)
desc.PossibleLengthLocalOnly = append(desc.PossibleLengthLocalOnly, length)
} else {
panic(fmt.Sprintf("Out-of-range local-only possible length found (%d), parent length %v.", length, parentDesc.PossibleLengthLocalOnly))
}
}
}

// generate our possible pattern from our min and max
if len(desc.PossibleLength) > 0 {
min := int32(math.MaxInt32)
max := int32(math.MinInt32)
for _, l := range desc.PossibleLength {
if l < min {
min = l
}
if l > max {
max = l
}
}
if min == max {
desc.PossibleNumberPattern = sp(fmt.Sprintf("\\d{%d}", min))
} else {
desc.PossibleNumberPattern = sp(fmt.Sprintf("\\d{%d,%d}", min, max))
}
}
// Need to sort both lists, possible lengths need to be ordered
sort.Slice(desc.PossibleLength, func(i, j int) bool { return desc.PossibleLength[i] < desc.PossibleLength[j] })
sort.Slice(desc.PossibleLengthLocalOnly, func(i, j int) bool { return desc.PossibleLengthLocalOnly[i] < desc.PossibleLengthLocalOnly[j] })
}

/**
Expand Down
5 changes: 0 additions & 5 deletions cmd/buildmetadata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,6 @@ func buildMetadata() *phonenumbers.PhoneMetadataCollection {
log.Fatalf("Error converting XML: %s", err)
}

// now that we've generated our possible patterns we can get rid of possible lengths in our proto buffers
for _, md := range collection.Metadata {
md.ClearPossibleLengths()
}

// write it out as a protobuf
data, err := proto.Marshal(collection)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion metadata_bin.go

Large diffs are not rendered by default.

31 changes: 0 additions & 31 deletions metadata_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,3 @@ func (pd *PhoneNumberDesc) hasPossibleLengthLocalOnly(length int32) bool {
}
return false
}

// utility function called by the builder to remove possible lengths from our protobufs since
// we don't want to write them
func (pd *PhoneNumberDesc) clearPossibleLengths() {
if pd == nil {
return
}
pd.PossibleLength = nil
pd.PossibleLengthLocalOnly = nil
}

// ClearPossibleLengths is called by the builder to remove possible lengths from our protobufs since
// we don't want to write them
func (md *PhoneMetadata) ClearPossibleLengths() {
md.GeneralDesc.clearPossibleLengths()
md.NoInternationalDialling.clearPossibleLengths()
md.FixedLine.clearPossibleLengths()
md.Mobile.clearPossibleLengths()
md.Pager.clearPossibleLengths()
md.TollFree.clearPossibleLengths()
md.PremiumRate.clearPossibleLengths()
md.SharedCost.clearPossibleLengths()
md.PersonalNumber.clearPossibleLengths()
md.Voip.clearPossibleLengths()
md.Uan.clearPossibleLengths()
md.Voicemail.clearPossibleLengths()
md.StandardRate.clearPossibleLengths()
md.ShortCode.clearPossibleLengths()
md.Emergency.clearPossibleLengths()
md.CarrierSpecific.clearPossibleLengths()
}
Loading

0 comments on commit ab2a338

Please sign in to comment.