func setPossibleLengths()

in builder.go [441:482]


func setPossibleLengths(lengths map[int32]bool, localOnlyLengths map[int32]bool, parentDesc *PhoneNumberDesc, desc *PhoneNumberDesc) {
	// We clear these fields since the metadata tends to inherit from the parent element for other
	// fields (via a mergeFrom).
	desc.PossibleLength = nil
	desc.PossibleLengthLocalOnly = nil

	// 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) {
		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.
	for length := range localOnlyLengths {
		if !lengths[length] {
			// We check it is covered by either of the possible length sets of the parent
			// PhoneNumberDesc, because for example 7 might be a valid localOnly length for mobile, but
			// 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.PossibleLengthLocalOnly = append(desc.PossibleLengthLocalOnly, length)
			} else {
				panic(fmt.Sprintf("Out-of-range local-only possible length found (%d), parent length %v.", length, parentDesc.PossibleLengthLocalOnly))
			}
		}
	}

	// 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] })
}