Documentation
¶
Index ¶
- Variables
- func ParseInUnit(u Unit, str string) (uint64, error)
- type Amount
- type BaseUnit
- type Format
- type LimesV1ValueWithUnit
- type Unit
- func (u Unit) Base() (Unit, uint64)
- func (u Unit) IsZero() bool
- func (u Unit) MarshalJSON() ([]byte, error)
- func (u Unit) MultiplyBy(factor uint64) (Unit, error)
- func (u *Unit) Scan(src any) (err error)
- func (u Unit) String() string
- func (u *Unit) UnmarshalJSON(buf []byte) error
- func (u Unit) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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)
type BaseUnit ¶
type BaseUnit string
BaseUnit enumerates relevant base units for units and values that appear in the Limes and LIQUID APIs.
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 ¶
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 ¶
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 ¶
func (v LimesV1ValueWithUnit) ConvertTo(u Unit) (LimesV1ValueWithUnit, error)
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 ¶
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 ¶
IsZero implements the Zeroer interface used by the omitzero option in encoding/json.
func (Unit) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (Unit) MultiplyBy ¶
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) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface. This method validates that the named unit actually exists.