countryphone

package
v1.100.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package countryphone provides geographical information of phone numbers. Country phone codes are defined by the International Telecommunication Union (ITU) in ITU-T standards E.123 and E.164.

The default phone prefixes data can be overridden with a custom dataset. The data is stored in a trie for fast prefix matching.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data struct {
	// contains filtered or unexported fields
}

Data is the main data structure that stores phone number prefixes and their information.

func New

func New(data InData) *Data

New initialize the search trie with the given data. If data is nil, the embedded default dataset is used.

func (*Data) AreaType

func (d *Data) AreaType(t int) (string, error)

AreaType returns the string representation of the area type.

func (*Data) NumberInfo

func (d *Data) NumberInfo(num string) (*NumInfo, error)

NumberInfo returns the number type and geographical information for the given phone number prefix.

NOTE: see the "github.com/Vonage/gosrvlib/pkg/countrycode" package to get the country information from the Alpha2 code.

func (*Data) NumberType

func (d *Data) NumberType(t int) (string, error)

NumberType returns the string representation of the number type.

Example
package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/countryphone"
)

func main() {
	// load defaut data
	data := countryphone.New(nil)

	info, err := data.NumberInfo("1357123456")
	if err != nil {
		log.Fatal(err)
	}

	b, err := json.MarshalIndent(info, "", "  ")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(b))

}
Output:

{
  "type": 1,
  "geo": [
    {
      "alpha2": "US",
      "area": "California",
      "type": 1
    }
  ]
}

type GeoInfo

type GeoInfo struct {
	// Alpha2 is the ISO-3166 Alpha-2 Country Code.
	Alpha2 string `json:"alpha2"`

	// Area is the geographical area.
	Area string `json:"area"`

	// Type is the type of area:
	//   - 0 = ""
	//   - 1 = "state"
	//   - 2 = "province or territory"
	//   - 3 = "nation or territory"
	//   - 4 = "non-geographic"
	//   - 5 = "other"
	Type int `json:"type"`
}

GeoInfo stores geographical information of a phone number.

type InCountryData

type InCountryData struct {
	// CC is the Country Calling code (e.g. "1" for "US" and "CA").
	CC string `json:"cc"`

	// Groups is a list of phone prefixes information grouped by geographical
	// area or type.
	Groups []InPrefixGroup `json:"groups"`
}

InCountryData stores all the phone number prefixes information for a country.

type InData

type InData = map[string]*InCountryData

InData is a type alias for a map of country Alpha-2 codes to InCountryData.

type InPrefixGroup

type InPrefixGroup struct {
	// Name is the name of the group or geographical area.
	Name string `json:"name"`

	// Type is the type of group or geographical area:
	//   - 0 = ""
	//   - 1 = "state"
	//   - 2 = "province or territory"
	//   - 3 = "nation or territory"
	//   - 4 = "non-geographic"
	//   - 5 = "other"
	Type int `json:"type"`

	// PrefixType is the type of phone number prefix:
	//   - 0 = ""
	//   - 1 = "landline"
	//   - 2 = "mobile"
	//   - 3 = "pager"
	//   - 4 = "satellite"
	//   - 5 = "special service"
	//   - 6 = "virtual"
	//   - 7 = "other"
	PrefixType int `json:"prefixType"`

	// Prefixes is a list of phone number prefixes (including the Country Code).
	Prefixes []string `json:"prefixes"`
}

InPrefixGroup stores the type and geographical information of a group of phone number prefixes.

type NumInfo

type NumInfo struct {
	// Type is the type of number:
	//   - 0 = ""
	//   - 1 = "landline"
	//   - 2 = "mobile"
	//   - 3 = "pager"
	//   - 4 = "satellite"
	//   - 5 = "special service"
	//   - 6 = "virtual"
	//   - 7 = "other"
	Type int `json:"type"`

	// Geo is the geographical information.
	Geo []*GeoInfo `json:"geo"`
}

NumInfo stores the number type and geographical information of a phone number.

type PrefixData

type PrefixData = map[string]*NumInfo

PrefixData is a type alias for a map of phone number prefixes to NumData.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL