contact

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 4 Imported by: 0

README

Contact Value Objects

Overview

The contact package provides value objects related to contact information, such as email addresses, phone numbers, and physical addresses. These value objects encapsulate validation logic and provide methods for common operations on contact information.

Value Objects

Email

The Email value object represents an email address with validation.

// Create a new email
email, err := contact.NewEmail("user@example.com")
if err != nil {
    // Handle error
}

// Check if email is empty
isEmpty := email.IsEmpty()

// Compare emails (case insensitive)
otherEmail, _ := contact.NewEmail("USER@EXAMPLE.COM")
areEqual := email.Equals(otherEmail) // true

// Get string representation
emailStr := email.String() // "user@example.com"

// Validate an existing email
err = email.Validate()
Address

The Address value object represents a postal address with validation.

// Create a new address
address, err := contact.NewAddress("123 Main St, Anytown, CA 12345")
if err != nil {
    // Handle error
}

// Check if address is empty
isEmpty := address.IsEmpty()

// Compare addresses (case insensitive)
otherAddress, _ := contact.NewAddress("123 MAIN ST, ANYTOWN, CA 12345")
areEqual := address.Equals(otherAddress) // true

// Get string representation
addressStr := address.String() // "123 Main St, Anytown, CA 12345"

// Validate an existing address
err = address.Validate()
Phone

The Phone value object represents a phone number with validation and formatting options.

// Create a new phone number
phone, err := contact.NewPhone("+1-555-123-4567")
if err != nil {
    // Handle error
}

// Format in different ways
e164 := phone.Format("e164")         // "+15551234567"
national := phone.Format("national")      // "(555) 123-4567"
international := phone.Format("international") // "+1 555 123 4567"

// Get country code
countryCode := phone.CountryCode() // "1"

// Check if valid for country
isValidUS := phone.IsValidForCountry("1") // true

// Get normalized version (E.164 format)
normalized := phone.Normalized() // "+15551234567"

// Compare phone numbers (ignoring formatting)
otherPhone, _ := contact.NewPhone("555-123-4567")
areEqual := phone.Equals(otherPhone) // true

Usage

To use the contact value objects, import the package:

import "github.com/abitofhelp/servicelib/valueobject/contact"

Then create and use the value objects as shown in the examples above.

Backward Compatibility

For backward compatibility, Email and Phone value objects are also available through the main valueobject package. However, the Address value object is now only available in the contact package:

import "github.com/abitofhelp/servicelib/valueobject"
import "github.com/abitofhelp/servicelib/valueobject/contact"

// Create a new email
email, err := valueobject.NewEmail("user@example.com")

// Create a new phone number
phone, err := valueobject.NewPhone("+1-555-123-4567")

// Create a new address (only available in contact package)
address, err := contact.NewAddress("123 Main St, Anytown, CA 12345")

However, new code should use the contact package directly to take advantage of the more organized structure and additional functionality.

Documentation

Overview

Package contact provides value objects related to contact information.

Package contact provides value objects related to contact information.

Package contact provides value objects related to contact information.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Address

type Address string

Address represents a postal address value object

func NewAddress

func NewAddress(address string) (Address, error)

NewAddress creates a new Address with validation

func (Address) Equals

func (a Address) Equals(other Address) bool

Equals checks if two Addresses are equal

func (Address) IsEmpty

func (a Address) IsEmpty() bool

IsEmpty checks if the Address is empty

func (Address) String

func (a Address) String() string

String returns the string representation of the Address

func (Address) Validate

func (a Address) Validate() error

Validate checks if the Address is valid

type Email

type Email string

Email represents an email address value object

func NewEmail

func NewEmail(email string) (Email, error)

NewEmail creates a new Email with validation

func (Email) Equals

func (e Email) Equals(other Email) bool

Equals checks if two Emails are equal

func (Email) IsEmpty

func (e Email) IsEmpty() bool

IsEmpty checks if the Email is empty

func (Email) String

func (e Email) String() string

String returns the string representation of the Email

func (Email) Validate

func (e Email) Validate() error

Validate checks if the Email is valid

type Phone

type Phone string

Phone represents a phone number value object

func NewPhone

func NewPhone(phone string) (Phone, error)

NewPhone creates a new Phone with validation

func (Phone) CountryCode

func (p Phone) CountryCode() string

CountryCode returns the country code of the phone number

func (Phone) Equals

func (p Phone) Equals(other Phone) bool

Equals checks if two Phones are equal

func (Phone) Format

func (p Phone) Format(format string) string

Format formats the phone number according to the specified format Supported formats: - "e164": E.164 format (e.g., "+12345678901") - "national": National format (e.g., "(234) 567-8901") - "international": International format (e.g., "+1 234 567 8901") If an unsupported format is specified, the phone number is returned as is.

func (Phone) IsEmpty

func (p Phone) IsEmpty() bool

IsEmpty checks if the Phone is empty

func (Phone) IsValidForCountry

func (p Phone) IsValidForCountry(countryCode string) bool

IsValidForCountry checks if the phone number is valid for the specified country

func (Phone) Normalized

func (p Phone) Normalized() string

Normalized returns the phone number in E.164 format

func (Phone) String

func (p Phone) String() string

String returns the string representation of the Phone

func (Phone) Validate

func (p Phone) Validate() error

Validate checks if the Phone is valid

Jump to

Keyboard shortcuts

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