network

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package network provides value objects related to network information.

This package contains value objects that represent network-related concepts such as IP addresses and URLs. These value objects are immutable and follow the Value Object pattern from Domain-Driven Design.

Key value objects in this package:

  • IPAddress: Represents an IP address (IPv4 or IPv6)
  • URL: Represents a URL with validation and parsing capabilities

The IPAddress value object provides methods for:

  • Creating and validating IP addresses
  • String representation
  • Equality comparison
  • Checking IP address properties (IPv4, IPv6, loopback, private)
  • JSON marshaling

The URL value object provides methods for:

  • Creating and validating URLs
  • String representation
  • Equality comparison
  • Extracting URL components (domain, path, query)
  • Validation of scheme and host

Example usage:

// Create a new IP address
ip, err := network.NewIPAddress("192.168.1.1")
if err != nil {
    // Handle validation error
}

// Check IP address properties
if ip.IsIPv4() {
    // Handle IPv4 address
}

if ip.IsPrivate() {
    // Handle private IP address
}

// Create a new URL
url, err := network.NewURL("https://example.com/path?query=value")
if err != nil {
    // Handle validation error
}

// Extract URL components
domain, err := url.Domain()
if err != nil {
    // Handle error
}

path, err := url.Path()
if err != nil {
    // Handle error
}

All value objects in this package are designed to be immutable, so they cannot be changed after creation. To modify a value object, create a new instance with the desired values.

Package network provides value objects related to network information.

Package network provides value objects related to network information.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IPAddress

type IPAddress string

IPAddress represents an IP address value object that supports both IPv4 and IPv6 formats

func NewIPAddress

func NewIPAddress(ip string) (IPAddress, error)

NewIPAddress creates a new IPAddress with validation for both IPv4 and IPv6 formats It uses Go's net.ParseIP function which supports both IPv4 (e.g., "192.168.1.1") and IPv6 (e.g., "2001:db8::1") address formats

func (IPAddress) Equals

func (ip IPAddress) Equals(other IPAddress) bool

Equals checks if two IPAddresses are equal Properly handles comparison of both IPv4 and IPv6 addresses in different representations (e.g., IPv4 mapped to IPv6, or different IPv6 notations for the same address)

func (IPAddress) IsEmpty

func (ip IPAddress) IsEmpty() bool

IsEmpty checks if the IPAddress is empty An IPAddress is considered empty only if it's an empty string Invalid IP addresses are not considered empty

func (IPAddress) IsIPv4

func (ip IPAddress) IsIPv4() bool

IsIPv4 checks if the IP address is in IPv4 format (e.g., "192.168.1.1") Returns true only for valid IPv4 addresses, false for IPv6 addresses or invalid formats

func (IPAddress) IsIPv6

func (ip IPAddress) IsIPv6() bool

IsIPv6 checks if the IP address is in IPv6 format (e.g., "2001:db8::1") Returns true only for valid IPv6 addresses, false for IPv4 addresses or invalid formats

func (IPAddress) IsLoopback

func (ip IPAddress) IsLoopback() bool

IsLoopback checks if the IP address is a loopback address Works with both IPv4 loopback (127.0.0.0/8) and IPv6 loopback (::1) addresses

func (IPAddress) IsPrivate

func (ip IPAddress) IsPrivate() bool

IsPrivate checks if the IP address is in a private range Supports both IPv4 private ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) and IPv6 private ranges (fc00::/7)

func (IPAddress) MarshalJSON

func (ip IPAddress) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface

func (IPAddress) String

func (ip IPAddress) String() string

String returns the string representation of the IPAddress For IPv4 addresses, it removes leading zeros from each segment

func (IPAddress) Validate

func (ip IPAddress) Validate() error

Validate checks if the IPAddress is valid

type URL

type URL string

URL represents a URL value object

func NewURL

func NewURL(rawURL string) (URL, error)

NewURL creates a new URL with validation

func (URL) Domain

func (u URL) Domain() (string, error)

Domain returns the domain part of the URL

func (URL) Equals

func (u URL) Equals(other URL) bool

Equals checks if two URLs are equal

func (URL) IsEmpty

func (u URL) IsEmpty() bool

IsEmpty checks if the URL is empty

func (URL) Path

func (u URL) Path() (string, error)

Path returns the path part of the URL

func (URL) Query

func (u URL) Query() (url.Values, error)

Query returns the query part of the URL

func (URL) String

func (u URL) String() string

String returns the string representation of the URL

func (URL) Validate

func (u URL) Validate() error

Validate checks if the URL is valid

Jump to

Keyboard shortcuts

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