units

package
v1.21.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// UnitNone is used for countable (rather than measurable) resources.
	//
	UnitNone = Unit{
				// contains filtered or unexported fields
	}

	// UnitBytes is exactly that.
	UnitBytes = makeBytesUnit(1)
	// UnitKibibytes is exactly that.
	UnitKibibytes = makeBytesUnit(1 << 10)
	// UnitMebibytes is exactly that.
	UnitMebibytes = makeBytesUnit(1 << 20)
	// UnitGibibytes is exactly that.
	UnitGibibytes = makeBytesUnit(1 << 30)
	// UnitTebibytes is exactly that.
	UnitTebibytes = makeBytesUnit(1 << 40)
	// UnitPebibytes is exactly that.
	UnitPebibytes = makeBytesUnit(1 << 50)
	// UnitExbibytes is exactly that.
	UnitExbibytes = makeBytesUnit(1 << 60)
)

Functions

func ParseInUnit

func ParseInUnit(u Unit, str string) (uint64, error)

ParseInUnit parses the string representation of a value with this unit (or any unit that can be converted to it).

ParseInUnit(UnitMebibytes, "10 MiB") -> 10
ParseInUnit(UnitMebibytes, "10 GiB") -> 10240
ParseInUnit(UnitMebibytes, "10 KiB") -> error: incompatible unit
ParseInUnit(UnitMebibytes, "10")     -> error: missing unit
ParseInUnit(UnitNone, "42")          -> 42
ParseInUnit(UnitNone, "42 MiB")      -> error: unexpected unit

Types

type Amount

type Amount struct {
	Base   BaseUnit
	Factor uint64
}

Amount describes an amount of a countable or measurable resource in terms of a base unit. This type provides basic serialization and deserialization for unit or amount strings, e.g. between "1 KiB" and Amount{"B", 1024}.

func ParseAmount

func ParseAmount(input string, formats Format) (Amount, error)

ParseAmount parses a string representation of an amount, in one of the following forms:

  • "<amount>", e.g. "42" (for BaseUnitNone)
  • "<amount> <unit>", e.g. "23 MiB"
  • "<unit>", e.g. "KiB" (only with allowBareUnit = true)

func (Amount) Format

func (a Amount) Format(formats Format) string

Format serializes this Amount into a string representation. Out of the given formats, the shortest possible format will be used. Panics if none of the allowed formats can represent this Amount.

func (Amount) MultiplyBy

func (a Amount) MultiplyBy(factor uint64) (Amount, error)

MultiplyBy multiplies this amount by the given factor.

Returns an error on integer overflow, e.g. when a bytes-based unit is larger than 2^64 bytes (16 EiB).

type BaseUnit

type BaseUnit string

BaseUnit enumerates relevant base units for units and values that appear in the Limes and LIQUID APIs.

const (
	// BaseUnitNone is used for countable (rather than measurable) resources.
	BaseUnitNone BaseUnit = ""
	// BaseUnitBytes is used for resources that are measured in bytes or any multiple thereof.
	BaseUnitBytes BaseUnit = "B"
)

type Format

type Format int

Format is a bitfield enumerating permissible formats for describing amounts. It is used by ParseAmount() and FormatAmount() to select which formats to accept/generate.

const (
	// EmptyFormat allows the empty string (denoting BaseUnitNone).
	EmptyFormat Format = 1 << iota
	// NumberOnlyFormat allows bare numbers like "42". Only positive integers are accepted.
	NumberOnlyFormat
	// UnitOnlyFormat allows bare units like "B" or "KiB". This does not include BareUnitNone.
	UnitOnlyFormat
	// NumberWithUnitFormat allows numbers with units, e.g. "23 MiB" or "1 B". This does not include BareUnitNone.
	NumberWithUnitFormat
)

func (Format) Description

func (f Format) Description() (output string, multipleFormats bool)

Description formats this set of formats as a description for use in error messages:

desc, _ := (units.UnitOnlyFormat | unit.NumberWithUnitFormat).String()
fmt.Println(desc) // prints: "<unit>" or "<number> <unit>"

type LimesV1ValueWithUnit

type LimesV1ValueWithUnit struct {
	Value uint64 `json:"value"`
	Unit  Unit   `json:"unit"`
}

LimesV1ValueWithUnit is used to represent values with units in subresources. As the name implies, this type is only exposed in the Limes v1 API.

func (LimesV1ValueWithUnit) ConvertTo

ConvertTo returns an equal value in the given Unit. An error is returned if:

  • the source unit cannot be converted to the target unit, or
  • the conversion does not yield an integer value in the new unit.

func (LimesV1ValueWithUnit) String

func (v LimesV1ValueWithUnit) String() string

String implements the fmt.Stringer interface. The value is serialized with the most appropriate unit:

// prints: 1000000 MiB
fmt.Println(LimesV1ValueWithUnit{1000000,UnitMebibytes})
// prints: 1 TiB
fmt.Println(LimesV1ValueWithUnit{1048576,UnitMebibytes})

type Unit

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

Unit represents the unit a resource or rate is measured in.

func (Unit) Base

func (u Unit) Base() (Unit, uint64)

Base returns the base unit of this unit. For units defined as a multiple of another unit, that unit is the base unit. Otherwise, the same unit and a multiple of 1 is returned.

func (Unit) IsZero

func (u Unit) IsZero() bool

IsZero implements the Zeroer interface used by the omitzero option in encoding/json.

func (Unit) MarshalJSON

func (u Unit) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Unit) MultiplyBy

func (u Unit) MultiplyBy(factor uint64) (Unit, error)

MultiplyBy multiplies this unit by the given factor. This should only be used to construct non-standard units:

// okay
customUnit, err := UnitGibibytes.MultiplyBy(4)
// do not do this, use UnitTebibytes directly
tebibytesUnit, err := UnitGibibytes.MultiplyBy(1024)

Returns an error on integer overflow, e.g. when a bytes-based unit is larger than 2^64 bytes (16 EiB).

Panics if factor is 0, since that would produce a nonsensical unit. Panics when trying to scale UnitNone, since that produces units that cannot be serialized under the current API specification.

func (*Unit) Scan

func (u *Unit) Scan(src any) (err error)

Scan implements the database/sql.Scanner interface.

func (Unit) String

func (u Unit) String() string

String implements the fmt.Stringer interface.

func (*Unit) UnmarshalJSON

func (u *Unit) UnmarshalJSON(buf []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. This method validates that the named unit actually exists.

func (Unit) Value

func (u Unit) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface.

Jump to

Keyboard shortcuts

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