Documentation
¶
Index ¶
- type Address
- type Color
- type Coordinate
- func (c Coordinate) DistanceTo(other Coordinate) float64
- func (c Coordinate) Equals(other Coordinate) bool
- func (c Coordinate) Format(format string) string
- func (c Coordinate) IsEastOf(other Coordinate) bool
- func (c Coordinate) IsEmpty() bool
- func (c Coordinate) IsNorthOf(other Coordinate) bool
- func (c Coordinate) IsSouthOf(other Coordinate) bool
- func (c Coordinate) IsWestOf(other Coordinate) bool
- func (c Coordinate) Latitude() float64
- func (c Coordinate) Longitude() float64
- func (c Coordinate) String() string
- type DateOfBirth
- type DateOfDeath
- func (dod DateOfDeath) Date() time.Time
- func (dod DateOfDeath) Equals(other DateOfDeath) bool
- func (dod DateOfDeath) Format(layout string) string
- func (dod DateOfDeath) IsEmpty() bool
- func (dod DateOfDeath) IsRecent() bool
- func (dod DateOfDeath) String() string
- func (dod DateOfDeath) TimeSinceDeath() int
- type Duration
- func (d Duration) Add(other Duration) (Duration, error)
- func (d Duration) Equals(other Duration) bool
- func (d Duration) Format(format string) string
- func (d Duration) Hours() float64
- func (d Duration) IsEmpty() bool
- func (d Duration) Milliseconds() int64
- func (d Duration) Minutes() float64
- func (d Duration) Seconds() float64
- func (d Duration) String() string
- func (d Duration) Subtract(other Duration) (Duration, error)
- func (d Duration) Value() time.Duration
- type Email
- type FileSize
- func (fs FileSize) Add(other FileSize) FileSize
- func (fs FileSize) Bytes() uint64
- func (fs FileSize) Equals(other FileSize) bool
- func (fs FileSize) Format(format string) string
- func (fs FileSize) Gigabytes() float64
- func (fs FileSize) IsEmpty() bool
- func (fs FileSize) IsLargerThan(other FileSize) bool
- func (fs FileSize) IsSmallerThan(other FileSize) bool
- func (fs FileSize) Kilobytes() float64
- func (fs FileSize) Megabytes() float64
- func (fs FileSize) Petabytes() float64
- func (fs FileSize) String() string
- func (fs FileSize) Subtract(other FileSize) FileSize
- func (fs FileSize) Terabytes() float64
- type FileSizeUnit
- type Gender
- type ID
- type IPAddress
- type Money
- func Max(a, b Money) (Money, error)
- func Min(a, b Money) (Money, error)
- func NewMoneyFromFloat64(amount float64, currency string) (Money, error)
- func NewMoneyFromString(amount string, currency string) (Money, error)
- func NewMoneyFromUint64(amount uint64, currency string) (Money, error)
- func Parse(s string) (Money, error)
- func (m Money) Abs() Money
- func (m Money) Add(other Money) (Money, error)
- func (m Money) Amount() float64
- func (m Money) AmountInCents() int64
- func (m Money) Currency() string
- func (m Money) Divide(divisor decimal.Decimal) (Money, error)
- func (m Money) Equals(other Money) bool
- func (m Money) IsGreaterThan(other Money) (bool, error)
- func (m Money) IsLessThan(other Money) (bool, error)
- func (m Money) IsNegative() bool
- func (m Money) IsPositive() bool
- func (m Money) IsZero() bool
- func (m Money) MarshalJSON() ([]byte, error)
- func (m Money) Multiply(factor decimal.Decimal) Money
- func (m Money) Negate() Money
- func (m Money) Round(places int32) Money
- func (m Money) Scale(factor decimal.Decimal, newCurrency string) (Money, error)
- func (m Money) String() string
- func (m Money) Subtract(other Money) (Money, error)
- func (m *Money) UnmarshalJSON(data []byte) error
- type MoneyJSON
- type Name
- type Password
- type Percentage
- func (p Percentage) Add(other Percentage) (Percentage, error)
- func (p Percentage) AsDecimal() float64
- func (p Percentage) Equals(other Percentage) bool
- func (p Percentage) Inverse() (Percentage, error)
- func (p Percentage) IsEmpty() bool
- func (p Percentage) Of(value float64) float64
- func (p Percentage) String() string
- func (p Percentage) Subtract(other Percentage) (Percentage, error)
- func (p Percentage) Value() float64
- type Phone
- type Rating
- func (r Rating) Equals(other Rating) bool
- func (r Rating) Format(format string) string
- func (r Rating) IsEmpty() bool
- func (r Rating) IsHigher(other Rating) bool
- func (r Rating) IsLower(other Rating) bool
- func (r Rating) MaxValue() float64
- func (r Rating) Normalized() float64
- func (r Rating) Percentage() float64
- func (r Rating) Stars() string
- func (r Rating) String() string
- func (r Rating) ToScale(newMaxValue float64) (Rating, error)
- func (r Rating) Value() float64
- type Temperature
- func (t Temperature) Add(other Temperature) Temperature
- func (t Temperature) Equals(other Temperature) bool
- func (t Temperature) Format(format string) string
- func (t Temperature) IsBoiling() bool
- func (t Temperature) IsEmpty() bool
- func (t Temperature) IsFreezing() bool
- func (t Temperature) String() string
- func (t Temperature) Subtract(other Temperature) (Temperature, error)
- func (t Temperature) ToCelsius() Temperature
- func (t Temperature) ToFahrenheit() Temperature
- func (t Temperature) ToKelvin() Temperature
- func (t Temperature) Unit() TemperatureUnit
- func (t Temperature) Value() float64
- type TemperatureUnit
- type URL
- type Username
- type Version
- func (v Version) Build() string
- func (v Version) CompareTo(other Version) int
- func (v Version) Equals(other Version) bool
- func (v Version) IsEmpty() bool
- func (v Version) IsPreRelease() bool
- func (v Version) Major() int
- func (v Version) Minor() int
- func (v Version) NextMajor() Version
- func (v Version) NextMinor() Version
- func (v Version) NextPatch() Version
- func (v Version) Patch() int
- func (v Version) PreRelease() string
- func (v Version) String() string
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 ¶
NewAddress creates a new Address with validation
type Color ¶
type Color string
Color represents a color value object in hexadecimal format (#RRGGBB)
type Coordinate ¶
type Coordinate struct {
// contains filtered or unexported fields
}
Coordinate represents a geographic coordinate (latitude and longitude)
func NewCoordinate ¶
func NewCoordinate(latitude, longitude float64) (Coordinate, error)
NewCoordinate creates a new Coordinate with validation
func ParseCoordinate ¶
func ParseCoordinate(s string) (Coordinate, error)
ParseCoordinate creates a new Coordinate from a string in format "lat,lng"
func (Coordinate) DistanceTo ¶
func (c Coordinate) DistanceTo(other Coordinate) float64
DistanceTo calculates the distance to another coordinate in kilometers using the Haversine formula
func (Coordinate) Equals ¶
func (c Coordinate) Equals(other Coordinate) bool
Equals checks if two Coordinates are equal
func (Coordinate) Format ¶
func (c Coordinate) Format(format string) string
Format returns the coordinate in the specified format Format options: - "dms": Degrees, minutes, seconds (e.g., "41°24'12.2"N, 2°10'26.5"E") - "dm": Degrees and decimal minutes (e.g., "41°24.2033'N, 2°10.4417'E") - "dd": Decimal degrees (e.g., "41.40339, 2.17403")
func (Coordinate) IsEastOf ¶
func (c Coordinate) IsEastOf(other Coordinate) bool
IsEastOf checks if this coordinate is east of another coordinate
func (Coordinate) IsEmpty ¶
func (c Coordinate) IsEmpty() bool
IsEmpty checks if the Coordinate is empty (zero value)
func (Coordinate) IsNorthOf ¶
func (c Coordinate) IsNorthOf(other Coordinate) bool
IsNorthOf checks if this coordinate is north of another coordinate
func (Coordinate) IsSouthOf ¶
func (c Coordinate) IsSouthOf(other Coordinate) bool
IsSouthOf checks if this coordinate is south of another coordinate
func (Coordinate) IsWestOf ¶
func (c Coordinate) IsWestOf(other Coordinate) bool
IsWestOf checks if this coordinate is west of another coordinate
func (Coordinate) Latitude ¶
func (c Coordinate) Latitude() float64
Latitude returns the latitude value
func (Coordinate) Longitude ¶
func (c Coordinate) Longitude() float64
Longitude returns the longitude value
func (Coordinate) String ¶
func (c Coordinate) String() string
String returns the string representation of the Coordinate
type DateOfBirth ¶
type DateOfBirth struct {
// contains filtered or unexported fields
}
DateOfBirth represents a date of birth value object
func NewDateOfBirth ¶
func NewDateOfBirth(year, month, day int) (DateOfBirth, error)
NewDateOfBirth creates a new DateOfBirth with validation
func ParseDateOfBirth ¶
func ParseDateOfBirth(dateStr string) (DateOfBirth, error)
ParseDateOfBirth creates a new DateOfBirth from a string in format "YYYY-MM-DD"
func (DateOfBirth) Age ¶
func (dob DateOfBirth) Age() int
Age calculates the age based on the date of birth and current date
func (DateOfBirth) Date ¶
func (dob DateOfBirth) Date() time.Time
Date returns the underlying time.Time value
func (DateOfBirth) Equals ¶
func (dob DateOfBirth) Equals(other DateOfBirth) bool
Equals checks if two DateOfBirth values are equal
func (DateOfBirth) Format ¶
func (dob DateOfBirth) Format(layout string) string
Format returns the date formatted according to the given layout
func (DateOfBirth) IsAdult ¶
func (dob DateOfBirth) IsAdult() bool
IsAdult checks if the person is an adult (18 years or older)
func (DateOfBirth) IsEmpty ¶
func (dob DateOfBirth) IsEmpty() bool
IsEmpty checks if the DateOfBirth is empty (zero value)
func (DateOfBirth) String ¶
func (dob DateOfBirth) String() string
String returns the string representation of the DateOfBirth
type DateOfDeath ¶
type DateOfDeath struct {
// contains filtered or unexported fields
}
DateOfDeath represents a date of death value object
func NewDateOfDeath ¶
func NewDateOfDeath(year, month, day int) (DateOfDeath, error)
NewDateOfDeath creates a new DateOfDeath with validation
func ParseDateOfDeath ¶
func ParseDateOfDeath(dateStr string) (DateOfDeath, error)
ParseDateOfDeath creates a new DateOfDeath from a string in format "YYYY-MM-DD"
func (DateOfDeath) Date ¶
func (dod DateOfDeath) Date() time.Time
Date returns the underlying time.Time value
func (DateOfDeath) Equals ¶
func (dod DateOfDeath) Equals(other DateOfDeath) bool
Equals checks if two DateOfDeath values are equal
func (DateOfDeath) Format ¶
func (dod DateOfDeath) Format(layout string) string
Format returns the date formatted according to the given layout
func (DateOfDeath) IsEmpty ¶
func (dod DateOfDeath) IsEmpty() bool
IsEmpty checks if the DateOfDeath is empty (zero value)
func (DateOfDeath) IsRecent ¶
func (dod DateOfDeath) IsRecent() bool
IsRecent checks if the death occurred within the last year
func (DateOfDeath) String ¶
func (dod DateOfDeath) String() string
String returns the string representation of the DateOfDeath
func (DateOfDeath) TimeSinceDeath ¶
func (dod DateOfDeath) TimeSinceDeath() int
TimeSinceDeath calculates the time elapsed since death in years
type Duration ¶
type Duration struct {
// contains filtered or unexported fields
}
Duration represents a time duration value object
func NewDuration ¶
NewDuration creates a new Duration with validation
func ParseDuration ¶
ParseDuration creates a new Duration from a string
func (Duration) Format ¶
Format returns a formatted string representation of the duration Format options: - "short": "1h 30m 45s" - "long": "1 hour 30 minutes 45 seconds" - "compact": "1:30:45"
func (Duration) Milliseconds ¶
Milliseconds returns the duration as an integer number of milliseconds
type Email ¶
type Email string
Email represents an email address value object
type FileSize ¶
type FileSize struct {
// contains filtered or unexported fields
}
FileSize represents a file size value object
func NewFileSize ¶
func NewFileSize(value float64, unit FileSizeUnit) (FileSize, error)
NewFileSize creates a new FileSize with validation
func ParseFileSize ¶
ParseFileSize creates a new FileSize from a string
func (FileSize) Format ¶
Format returns the file size in the specified format Format options: - "B": Always in bytes - "KB": Always in kilobytes - "MB": Always in megabytes - "GB": Always in gigabytes - "TB": Always in terabytes - "PB": Always in petabytes - "auto": Uses the most appropriate unit for readability - "binary": Uses binary prefixes (KiB, MiB, GiB, etc.) - "decimal": Uses decimal prefixes (KB, MB, GB, etc.)
func (FileSize) IsLargerThan ¶
IsLargerThan checks if this FileSize is larger than another FileSize
func (FileSize) IsSmallerThan ¶
IsSmallerThan checks if this FileSize is smaller than another FileSize
func (FileSize) String ¶
String returns the string representation of the FileSize Uses the most appropriate unit for readability
type FileSizeUnit ¶
type FileSizeUnit string
FileSizeUnit represents a file size unit
const ( // Bytes unit (B) Bytes FileSizeUnit = "B" // Kilobytes unit (KB) Kilobytes FileSizeUnit = "KB" // Megabytes unit (MB) Megabytes FileSizeUnit = "MB" // Gigabytes unit (GB) Gigabytes FileSizeUnit = "GB" // Terabytes unit (TB) Terabytes FileSizeUnit = "TB" // Petabytes unit (PB) Petabytes FileSizeUnit = "PB" )
type Gender ¶
type Gender string
Gender represents a person's gender value object
type IPAddress ¶
type IPAddress string
IPAddress represents an IP address value object that supports both IPv4 and IPv6 formats
func NewIPAddress ¶
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 ¶
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) IsIPv4 ¶
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 ¶
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 ¶
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
type Money ¶
type Money struct {
// contains filtered or unexported fields
}
Money represents a monetary value object with amount and currency Uses decimal.Decimal to store amount with arbitrary precision to avoid floating-point precision issues
This implementation uses the github.com/shopspring/decimal package instead of int64 for the following reasons: 1. Decimal arithmetic is more precise than floating-point arithmetic for monetary values 2. It can handle arbitrary precision, which is important for financial calculations 3. It provides methods for rounding, scaling, and other operations that are useful for monetary values 4. It avoids common floating-point errors like 0.1 + 0.2 != 0.3
Note: This implementation requires the github.com/shopspring/decimal package to be added to the project's dependencies with: go get github.com/shopspring/decimal
func Min ¶
Min returns the smaller of two Money values Both Money values must have the same currency
func NewMoneyFromFloat64 ¶
NewMoneyFromFloat64 creates a new Money with validation from a float64 amount
func NewMoneyFromString ¶
NewMoneyFromString creates a new Money with validation from a string amount This is more precise than using a float64 for monetary values
func NewMoneyFromUint64 ¶
NewMoneyFromUint64 creates a new Money with validation from a uint64 amount This is useful for representing monetary values in the smallest currency unit (e.g., cents)
func (Money) Add ¶
Add adds another Money value and returns a new Money Both Money values must have the same currency
func (Money) Amount ¶
Amount returns the amount as a float64 in the standard currency unit (e.g., dollars) This is for backward compatibility with the previous API
func (Money) AmountInCents ¶
AmountInCents returns the amount in the smallest currency unit (e.g., cents)
func (Money) Divide ¶
Divide divides the money amount by a divisor (e.g., for splitting payments) and returns a new Money object Returns an error if the divisor is zero
func (Money) IsGreaterThan ¶
IsGreaterThan checks if the Money amount is greater than another Money amount Returns an error if the currencies are different
func (Money) IsLessThan ¶
IsLessThan checks if the Money amount is less than another Money amount Returns an error if the currencies are different
func (Money) IsNegative ¶
IsNegative checks if the Money amount is negative
func (Money) IsPositive ¶
IsPositive checks if the Money amount is positive
func (Money) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface
func (Money) Multiply ¶
Multiply multiplies the money amount by a factor (e.g., for calculating interest) and returns a new Money object
func (Money) Round ¶
Round rounds the money amount to the specified number of decimal places and returns a new Money object
func (Money) Scale ¶
Scale scales the money amount by a factor (e.g., for currency conversion) and returns a new Money with the specified currency
func (Money) Subtract ¶
Subtract subtracts another Money value and returns a new Money Both Money values must have the same currency
func (*Money) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface
type Password ¶
type Password struct {
// contains filtered or unexported fields
}
Password represents a password value object It stores the hashed password, not the plain text
func NewPassword ¶
NewPassword creates a new Password with validation and hashing
func (Password) Equals ¶
Equals checks if two Password values are equal This compares the underlying hashed passwords
func (Password) HashedPassword ¶
HashedPassword returns the base64-encoded hashed password
type Percentage ¶
type Percentage float64
Percentage represents a percentage value object
func NewPercentage ¶
func NewPercentage(value float64) (Percentage, error)
NewPercentage creates a new Percentage with validation
func ParsePercentage ¶
func ParsePercentage(s string) (Percentage, error)
ParsePercentage creates a new Percentage from a string
func (Percentage) Add ¶
func (p Percentage) Add(other Percentage) (Percentage, error)
Add adds another percentage and returns a new Percentage The result is capped at 100%
func (Percentage) AsDecimal ¶
func (p Percentage) AsDecimal() float64
AsDecimal returns the percentage as a decimal (e.g., 75% -> 0.75)
func (Percentage) Equals ¶
func (p Percentage) Equals(other Percentage) bool
Equals checks if two Percentages are equal
func (Percentage) Inverse ¶
func (p Percentage) Inverse() (Percentage, error)
Inverse returns the inverse percentage (100% - p)
func (Percentage) IsEmpty ¶
func (p Percentage) IsEmpty() bool
IsEmpty checks if the Percentage is zero
func (Percentage) Of ¶
func (p Percentage) Of(value float64) float64
Of calculates the percentage of a given value For example: 25% of 200 = 50
func (Percentage) String ¶
func (p Percentage) String() string
String returns the string representation of the Percentage
func (Percentage) Subtract ¶
func (p Percentage) Subtract(other Percentage) (Percentage, error)
Subtract subtracts another percentage and returns a new Percentage The result is floored at 0%
type Phone ¶
type Phone string
Phone represents a phone number value object
type Rating ¶
type Rating struct {
// contains filtered or unexported fields
}
Rating represents a rating value object (e.g., 1-5 stars)
func ParseRating ¶
ParseRating creates a new Rating from a string Formats supported: - "4" (assumes max value of 5) - "4/5" (explicit max value) - "8/10" (explicit max value)
func (Rating) Equals ¶
Equals checks if two Ratings are equal This compares the normalized values (value/maxValue)
func (Rating) Format ¶
Format returns the rating in the specified format Format options: - "decimal": "4.5/5.0" - "percentage": "90%" - "stars": "★★★★½" - "fraction": "9/10"
func (Rating) IsHigher ¶
IsHigher checks if this rating is higher than another rating This compares the normalized values to handle different scales
func (Rating) IsLower ¶
IsLower checks if this rating is lower than another rating This compares the normalized values to handle different scales
func (Rating) Normalized ¶
Normalized returns the rating normalized to a 0-1 scale
func (Rating) Percentage ¶
Percentage returns the rating as a percentage of the maximum value
func (Rating) Stars ¶
Stars returns the rating as a string of stars (★) and empty stars (☆) For example, a rating of 3.5/5 would return "★★★½☆"
type Temperature ¶
type Temperature struct {
// contains filtered or unexported fields
}
Temperature represents a temperature value object
func NewTemperature ¶
func NewTemperature(value float64, unit TemperatureUnit) (Temperature, error)
NewTemperature creates a new Temperature with validation
func ParseTemperature ¶
func ParseTemperature(s string) (Temperature, error)
ParseTemperature creates a new Temperature from a string
func (Temperature) Add ¶
func (t Temperature) Add(other Temperature) Temperature
Add adds another temperature and returns a new Temperature
func (Temperature) Equals ¶
func (t Temperature) Equals(other Temperature) bool
Equals checks if two Temperatures are equal This compares the actual temperature values, not just the raw values and units
func (Temperature) Format ¶
func (t Temperature) Format(format string) string
Format returns the temperature in the specified format Format options: - "short": "25°C" - "long": "25 degrees Celsius" - "scientific": "298.15K"
func (Temperature) IsBoiling ¶
func (t Temperature) IsBoiling() bool
IsBoiling checks if the temperature is at or above the boiling point of water
func (Temperature) IsEmpty ¶
func (t Temperature) IsEmpty() bool
IsEmpty checks if the Temperature is empty (zero value)
func (Temperature) IsFreezing ¶
func (t Temperature) IsFreezing() bool
IsFreezing checks if the temperature is at or below the freezing point of water
func (Temperature) String ¶
func (t Temperature) String() string
String returns the string representation of the Temperature
func (Temperature) Subtract ¶
func (t Temperature) Subtract(other Temperature) (Temperature, error)
Subtract subtracts another temperature and returns a new Temperature
func (Temperature) ToCelsius ¶
func (t Temperature) ToCelsius() Temperature
ToCelsius converts the temperature to Celsius
func (Temperature) ToFahrenheit ¶
func (t Temperature) ToFahrenheit() Temperature
ToFahrenheit converts the temperature to Fahrenheit
func (Temperature) ToKelvin ¶
func (t Temperature) ToKelvin() Temperature
ToKelvin converts the temperature to Kelvin
func (Temperature) Unit ¶
func (t Temperature) Unit() TemperatureUnit
Unit returns the temperature unit
type TemperatureUnit ¶
type TemperatureUnit string
TemperatureUnit represents a temperature unit
const ( // Celsius temperature unit (°C) Celsius TemperatureUnit = "C" // Fahrenheit temperature unit (°F) Fahrenheit TemperatureUnit = "F" // Kelvin temperature unit (K) Kelvin TemperatureUnit = "K" )
type Username ¶
type Username string
Username represents a username value object
func NewUsername ¶
NewUsername creates a new Username with validation
func (Username) ContainsSubstring ¶
ContainsSubstring checks if the username contains the given substring (case insensitive)
type Version ¶
type Version struct {
// contains filtered or unexported fields
}
Version represents a semantic version (major.minor.patch)
func NewVersion ¶
NewVersion creates a new Version with validation
func ParseVersion ¶
ParseVersion creates a new Version from a string in format "major.minor.patch[-prerelease][+build]"
func (Version) CompareTo ¶
CompareTo compares this version to another version Returns:
-1 if this version is less than the other 0 if this version is equal to the other 1 if this version is greater than the other
func (Version) IsPreRelease ¶
IsPreRelease checks if this is a pre-release version
func (Version) PreRelease ¶
PreRelease returns the pre-release identifier