measurement

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package measurement provides value objects related to measurement information.

This package contains value objects that represent different types of measurements such as monetary values, file sizes, memory sizes, percentages, ratings, and temperatures. These value objects are immutable and follow the Value Object pattern from Domain-Driven Design.

Key value objects in this package:

  • Money: Represents a monetary value with amount and currency
  • FileSize: Represents a file size with different units (bytes, KB, MB, etc.)
  • MemSize: Represents a memory size with different units (bytes, KB, MB, etc.)
  • Percentage: Represents a percentage value
  • Rating: Represents a rating value (e.g., 1-5 stars)
  • Temperature: Represents a temperature value with different units

Each value object provides methods for:

  • Creating and validating instances
  • String representation
  • Equality comparison
  • Conversion between different units or formats

Many value objects also provide arithmetic operations appropriate to their domain:

  • Money: Addition, subtraction, multiplication, division
  • Percentage: Addition, subtraction, application to values
  • Temperature: Conversion between units (Celsius, Fahrenheit, Kelvin)

Example usage:

// Create a new money value
amount := decimal.NewFromFloat(99.99)
money, err := measurement.NewMoney(amount, "USD")
if err != nil {
    // Handle validation error
}

// Perform arithmetic operations
tax, err := measurement.NewMoney(decimal.NewFromFloat(8.50), "USD")
if err != nil {
    // Handle validation error
}
total, err := money.Add(tax)
if err != nil {
    // Handle error (e.g., currency mismatch)
}

// Create a percentage
discount, err := measurement.NewPercentage(15.0)
if err != nil {
    // Handle validation error
}

// Create a temperature
temp, err := measurement.NewTemperature(22.5, "C")
if err != nil {
    // Handle validation error
}
fahrenheit := temp.ToFahrenheit()

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 measurement provides value objects related to measurement information.

Package measurement provides value objects related to measurement information.

Package measurement provides value objects related to measurement information.

Package measurement provides value objects related to measurement information.

Package measurement provides value objects related to measurement information.

Package measurement provides value objects related to measurements and units.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileSize

type FileSize struct {
	base.BaseStructValueObject
	// contains filtered or unexported fields
}

FileSize represents a file size value object that uses base 10 decimal values in accordance with the International System of Units (SI Units). This is the standard for file sizes in most contexts, where 1 KB = 1000 bytes, 1 MB = 1000 KB, etc., as opposed to the binary-based units used for memory sizes.

func NewFileSize

func NewFileSize(value float64, unit FileSizeUnit) (FileSize, error)

NewFileSize creates a new FileSize with validation Uses base 10 decimal values in accordance with the International System of Units (SI Units), where 1 KB = 1000 bytes, 1 MB = 1000 KB, etc.

func NewFileSizeFromBytes

func NewFileSizeFromBytes(bytes uint64) FileSize

NewFileSizeFromBytes creates a new FileSize directly from bytes The resulting FileSize will use base 10 decimal values in accordance with the International System of Units (SI Units) for all conversions and formatting.

func ParseFileSize

func ParseFileSize(s string) (FileSize, error)

ParseFileSize creates a new FileSize from a string The parsed value will use base 10 decimal values in accordance with the International System of Units (SI Units), where 1 KB = 1000 bytes, 1 MB = 1000 KB, etc.

func (FileSize) Add

func (v FileSize) Add(other FileSize) FileSize

Add adds another FileSize and returns a new FileSize

func (FileSize) Bytes

func (v FileSize) Bytes() uint64

Bytes returns the size in bytes

func (FileSize) Equals

func (v FileSize) Equals(other FileSize) bool

Equals checks if two FileSizes are equal

func (FileSize) Format

func (v FileSize) Format(format string) string

Format returns the file size in the specified format Format options: - "B": Always in bytes - "KB": Always in kilobytes (base 10, ISO standard) - "MB": Always in megabytes (base 10, ISO standard) - "GB": Always in gigabytes (base 10, ISO standard) - "TB": Always in terabytes (base 10, ISO standard) - "PB": Always in petabytes (base 10, ISO standard) - "auto": Uses the most appropriate unit for readability (base 10, ISO standard) - "binary": Uses binary prefixes (KiB, MiB, GiB, etc.) - for compatibility only

func (FileSize) Gigabytes

func (v FileSize) Gigabytes() float64

Gigabytes returns the size in gigabytes (base 10, ISO standard)

func (FileSize) IsEmpty

func (v FileSize) IsEmpty() bool

IsEmpty checks if the FileSize is empty (zero value)

func (FileSize) IsLargerThan

func (v FileSize) IsLargerThan(other FileSize) bool

IsLargerThan checks if this FileSize is larger than another FileSize

func (FileSize) IsSmallerThan

func (v FileSize) IsSmallerThan(other FileSize) bool

IsSmallerThan checks if this FileSize is smaller than another FileSize

func (FileSize) Kilobytes

func (v FileSize) Kilobytes() float64

Kilobytes returns the size in kilobytes (base 10, ISO standard)

func (FileSize) MarshalJSON

func (v FileSize) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface

func (FileSize) Megabytes

func (v FileSize) Megabytes() float64

Megabytes returns the size in megabytes (base 10, ISO standard)

func (FileSize) Petabytes

func (v FileSize) Petabytes() float64

Petabytes returns the size in petabytes (base 10, ISO standard)

func (FileSize) String

func (v FileSize) String() string

String returns the string representation of the FileSize Uses the most appropriate unit for readability

func (FileSize) Subtract

func (v FileSize) Subtract(other FileSize) FileSize

Subtract subtracts another FileSize and returns a new FileSize If the result would be negative, it returns zero

func (FileSize) Terabytes

func (v FileSize) Terabytes() float64

Terabytes returns the size in terabytes (base 10, ISO standard)

func (FileSize) ToMap

func (v FileSize) ToMap() map[string]interface{}

ToMap converts the FileSize to a map[string]interface{}

func (FileSize) Validate

func (v FileSize) Validate() error

Validate checks if the FileSize is valid

type FileSizeUnit

type FileSizeUnit string

FileSizeUnit represents a file size unit (using base 10 decimal values per ISO standards)

const (
	// FileSizeBytes unit (B)
	FileSizeBytes FileSizeUnit = "B"
	// Kilobytes unit (KB) - 10^3 bytes
	Kilobytes FileSizeUnit = "KB"
	// Megabytes unit (MB) - 10^6 bytes
	Megabytes FileSizeUnit = "MB"
	// Gigabytes unit (GB) - 10^9 bytes
	Gigabytes FileSizeUnit = "GB"
	// Terabytes unit (TB) - 10^12 bytes
	Terabytes FileSizeUnit = "TB"
	// Petabytes unit (PB) - 10^15 bytes
	Petabytes FileSizeUnit = "PB"
)

type MemSize

type MemSize struct {
	base.BaseStructValueObject
	// contains filtered or unexported fields
}

MemSize represents a memory size value object using base 2 binary values This is specifically for computer memory measurements, not file sizes.

func NewMemSize

func NewMemSize(value float64, unit MemSizeUnit) (MemSize, error)

NewMemSize creates a new MemSize with validation

func ParseMemSize

func ParseMemSize(s string) (MemSize, error)

ParseMemSize creates a new MemSize from a string

func (MemSize) Add

func (ms MemSize) Add(other MemSize) MemSize

Add adds another MemSize and returns a new MemSize

func (MemSize) Bytes

func (ms MemSize) Bytes() uint64

Bytes returns the size in bytes

func (MemSize) Equals

func (ms MemSize) Equals(other MemSize) bool

Equals checks if two MemSizes are equal

func (MemSize) Format

func (ms MemSize) Format(format string) string

Format returns the memory size in the specified format Format options: - "B": Always in bytes - "KiB": Always in kibibytes - "MiB": Always in mebibytes - "GiB": Always in gibibytes - "TiB": Always in tebibytes - "PiB": Always in pebibytes - "auto": Uses the most appropriate unit for readability

func (MemSize) Gibibytes

func (ms MemSize) Gibibytes() float64

Gibibytes returns the size in gibibytes (GiB)

func (MemSize) IsEmpty

func (ms MemSize) IsEmpty() bool

IsEmpty checks if the MemSize is empty (zero value)

func (MemSize) IsLargerThan

func (ms MemSize) IsLargerThan(other MemSize) bool

IsLargerThan checks if this MemSize is larger than another MemSize

func (MemSize) IsSmallerThan

func (ms MemSize) IsSmallerThan(other MemSize) bool

IsSmallerThan checks if this MemSize is smaller than another MemSize

func (MemSize) Kibibytes

func (ms MemSize) Kibibytes() float64

Kibibytes returns the size in kibibytes (KiB)

func (MemSize) MarshalJSON

func (ms MemSize) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface

func (MemSize) Mebibytes

func (ms MemSize) Mebibytes() float64

Mebibytes returns the size in mebibytes (MiB)

func (MemSize) Pebibytes

func (ms MemSize) Pebibytes() float64

Pebibytes returns the size in pebibytes (PiB)

func (MemSize) String

func (ms MemSize) String() string

String returns the string representation of the MemSize Uses the most appropriate unit for readability with binary (base 2) units

func (MemSize) Subtract

func (ms MemSize) Subtract(other MemSize) MemSize

Subtract subtracts another MemSize and returns a new MemSize If the result would be negative, it returns zero

func (MemSize) Tebibytes

func (ms MemSize) Tebibytes() float64

Tebibytes returns the size in tebibytes (TiB)

func (MemSize) ToMap

func (ms MemSize) ToMap() map[string]interface{}

ToMap converts the MemSize to a map[string]interface{}

func (MemSize) Validate

func (ms MemSize) Validate() error

Validate checks if the MemSize is valid

type MemSizeUnit

type MemSizeUnit string

MemSizeUnit represents a memory size unit (using base 2 binary values)

const (
	// Bytes unit (B)
	Bytes MemSizeUnit = "B"
	// Kibibytes unit (KiB) - 2^10 bytes
	Kibibytes MemSizeUnit = "KiB"
	// Mebibytes unit (MiB) - 2^20 bytes
	Mebibytes MemSizeUnit = "MiB"
	// Gibibytes unit (GiB) - 2^30 bytes
	Gibibytes MemSizeUnit = "GiB"
	// Tebibytes unit (TiB) - 2^40 bytes
	Tebibytes MemSizeUnit = "TiB"
	// Pebibytes unit (PiB) - 2^50 bytes
	Pebibytes MemSizeUnit = "PiB"
)

type Money

type Money struct {
	base.BaseStructValueObject
	// contains filtered or unexported fields
}

Money represents a monetary value object with amount and currency

func NewMoney

func NewMoney(amount decimal.Decimal, currency string) (Money, error)

NewMoney creates a new Money with validation

func NewMoneyFromFloat64

func NewMoneyFromFloat64(amount float64, currency string) (Money, error)

NewMoneyFromFloat64 creates a new Money with validation from a float64 amount

func NewMoneyFromString

func NewMoneyFromString(amount string, currency string) (Money, error)

NewMoneyFromString creates a new Money with validation from a string amount This is more precise than using a float64 for monetary values

func Parse

func Parse(s string) (Money, error)

Parse creates a Money object from a string representation like "10.99 USD"

func (Money) Abs

func (v Money) Abs() Money

Abs returns the absolute value of the Money

func (Money) Add

func (v Money) Add(other Money) (Money, error)

Add adds another Money value and returns a new Money Both Money values must have the same currency

func (Money) Amount

func (v Money) Amount() decimal.Decimal

Amount returns the amount as a decimal.Decimal

func (Money) AmountInCents

func (v Money) AmountInCents() int64

AmountInCents returns the amount in the smallest currency unit (e.g., cents)

func (Money) Currency

func (v Money) Currency() string

Currency returns the currency code

func (Money) Divide

func (v Money) Divide(divisor decimal.Decimal) (Money, error)

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) Equals

func (v Money) Equals(other Money) bool

Equals checks if two Moneys are equal

func (Money) IsEmpty

func (v Money) IsEmpty() bool

IsEmpty checks if the Money is empty (zero value)

func (Money) IsGreaterThan

func (v Money) IsGreaterThan(other Money) (bool, error)

IsGreaterThan checks if the Money amount is greater than another Money amount Returns an error if the currencies are different

func (Money) IsLessThan

func (v Money) IsLessThan(other Money) (bool, error)

IsLessThan checks if the Money amount is less than another Money amount Returns an error if the currencies are different

func (Money) IsNegative

func (v Money) IsNegative() bool

IsNegative checks if the Money amount is negative

func (Money) IsPositive

func (v Money) IsPositive() bool

IsPositive checks if the Money amount is positive

func (Money) MarshalJSON

func (v Money) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface

func (Money) Multiply

func (v Money) Multiply(factor decimal.Decimal) Money

Multiply multiplies the money amount by a factor (e.g., for calculating interest) and returns a new Money object

func (Money) Negate

func (v Money) Negate() Money

Negate returns the negated value of the Money

func (Money) String

func (v Money) String() string

String returns the string representation of the Money

func (Money) Subtract

func (v Money) Subtract(other Money) (Money, error)

Subtract subtracts another Money value and returns a new Money Both Money values must have the same currency

func (Money) ToMap

func (v Money) ToMap() map[string]interface{}

ToMap converts the Money to a map[string]interface{}

func (Money) Validate

func (v Money) Validate() error

Validate checks if the Money is valid

type Percentage

type Percentage struct {
	base.BaseStructValueObject
	// contains filtered or unexported fields
}

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 (v 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 (v Percentage) AsDecimal() float64

AsDecimal returns the percentage as a decimal (e.g., 75% -> 0.75)

func (Percentage) Equals

func (v Percentage) Equals(other Percentage) bool

Equals checks if two Percentages are equal

func (Percentage) Inverse

func (v Percentage) Inverse() (Percentage, error)

Inverse returns the inverse percentage (100% - p)

func (Percentage) IsEmpty

func (v Percentage) IsEmpty() bool

IsEmpty checks if the Percentage is empty (zero value)

func (Percentage) MarshalJSON

func (v Percentage) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface

func (Percentage) Of

func (v Percentage) Of(value float64) float64

Of calculates the percentage of a given value For example: 25% of 200 = 50

func (Percentage) String

func (v Percentage) String() string

String returns the string representation of the Percentage

func (Percentage) Subtract

func (v Percentage) Subtract(other Percentage) (Percentage, error)

Subtract subtracts another percentage and returns a new Percentage The result is floored at 0%

func (Percentage) ToMap

func (v Percentage) ToMap() map[string]interface{}

ToMap converts the Percentage to a map[string]interface{}

func (Percentage) Validate

func (v Percentage) Validate() error

Validate checks if the Percentage is valid

func (Percentage) Value

func (v Percentage) Value() float64

Value returns the raw float64 value

type Rating

type Rating struct {
	base.BaseStructValueObject
	// contains filtered or unexported fields
}

Rating represents a rating value object (e.g., 1-5 stars)

func NewRating

func NewRating(value float64, maxValue float64) (Rating, error)

NewRating creates a new Rating with validation

func ParseRating

func ParseRating(s string) (Rating, error)

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

func (v Rating) Equals(other Rating) bool

Equals checks if two Ratings are equal

func (Rating) Format

func (v Rating) Format(format string) string

Format returns the rating in the specified format Format options: - "decimal": "4.5/5.0" - "percentage": "90%" - "stars": "★★★★½" - "fraction": "9/10"

func (Rating) IsEmpty

func (v Rating) IsEmpty() bool

IsEmpty checks if the Rating is empty (zero value)

func (Rating) IsHigher

func (v Rating) IsHigher(other Rating) bool

IsHigher checks if this rating is higher than another rating This compares the normalized values to handle different scales

func (Rating) IsLower

func (v Rating) IsLower(other Rating) bool

IsLower checks if this rating is lower than another rating This compares the normalized values to handle different scales

func (Rating) MarshalJSON

func (v Rating) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface

func (Rating) MaxValue

func (v Rating) MaxValue() float64

MaxValue returns the maximum rating value

func (Rating) Normalized

func (v Rating) Normalized() float64

Normalized returns the rating normalized to a 0-1 scale

func (Rating) Percentage

func (v Rating) Percentage() float64

Percentage returns the rating as a percentage of the maximum value

func (Rating) Stars

func (v Rating) Stars() string

Stars returns the rating as a string of stars (★) and empty stars (☆) For example, a rating of 3.5/5 would return "★★★½☆"

func (Rating) String

func (v Rating) String() string

String returns the string representation of the Rating

func (Rating) ToMap

func (v Rating) ToMap() map[string]interface{}

ToMap converts the Rating to a map[string]interface{}

func (Rating) ToScale

func (v Rating) ToScale(newMaxValue float64) (Rating, error)

ToScale converts the rating to a different scale

func (Rating) Validate

func (v Rating) Validate() error

Validate checks if the Rating is valid

func (Rating) Value

func (v Rating) Value() float64

Value returns the rating value

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) CompareTo

func (t Temperature) CompareTo(other Temperature) int

CompareTo compares this temperature with another and returns: -1 if this < other

0 if this == other
1 if this > other

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

func (Temperature) Validate

func (t Temperature) Validate() error

Validate checks if the Temperature is valid

func (Temperature) Value

func (t Temperature) Value() float64

Value returns the temperature value

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"
)

Jump to

Keyboard shortcuts

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