Documentation ¶
Index ¶
- Variables
- func Add(a, b float64) float64
- func AssertEntry(_ Value) error
- func AssertTransformsAvailable(a, b *Unit) error
- func AssertValid(v Value) error
- func CalculatorExists(id string) bool
- func Divide(a, b float64) float64
- func Equal(a, b float64) bool
- func FalseComparator(_, _ float64) bool
- func GetCalculatorIDs() []string
- func GetGroupIDs() []string
- func GetTransforms() map[string][]string
- func GreaterThan(a, b float64) bool
- func GreaterThanEqual(a, b float64) bool
- func IsNegative(f float64) bool
- func IsOne(f float64) bool
- func IsPositive(f float64) bool
- func IsZero(f float64) bool
- func LessThan(a, b float64) bool
- func LessThanEqual(a, b float64) bool
- func Multiply(a, b float64) float64
- func NewBasicBiTransform(u1, u2 *Unit, factor float64)
- func NewBasicTransform(from, to *Unit, factor float64)
- func NewBiTransform(u1, u2 *Unit, u1ToU2, u2ToU1 Transformer)
- func NewCalculator(id string, calc Calculator)
- func NewTransform(from, to *Unit, t Transformer)
- func NopTransformer(f float64) (float64, error)
- func NotEqual(a, b float64) bool
- func ResetMap(ctx context.Context) error
- func Subtract(a, b float64) float64
- func Transform(f float64, from, to *Unit) (float64, error)
- func TransformAvailable(from, to *Unit) bool
- func TransformsAvailable(a, b *Unit) bool
- func TrueComparator(_, _ float64) bool
- func WithMap(ctx context.Context) context.Context
- func Within(a, b, c float64) bool
- func Without(a, b, c float64) bool
- type Assertion
- type Calculation
- type Calculator
- func AssertCalculator(c Calculator, a ...Assertion) Calculator
- func Basic1ArgCalculator(f func(float64) float64) Calculator
- func Basic2ArgCalculator(f func(float64, float64) float64) Calculator
- func Calculator2arg(f func(_, _ Value) (Value, error)) Calculator
- func Calculator3arg(f func(_, _, _ Value) (Value, error)) Calculator
- func GetCalculator(id string) (Calculator, error)
- type Comparator
- type Group
- type Map
- type Range
- type Time
- type Transformer
- type Unit
- func GetUnit(id string) (*Unit, bool)
- func GetUnitByHash(h uint64) (*Unit, bool)
- func GetUnits() []*Unit
- func NewBoundedUnit(id, name, unit string, precision int, min, max float64) *Unit
- func NewLowerBoundUnit(id, name, unit string, precision int, min float64) *Unit
- func NewUnit(id, name, unit string, precision int) *Unit
- func NewUpperBoundUnit(id, name, unit string, precision int, max float64) *Unit
- func (u *Unit) AssertUnit(b *Unit) error
- func (u *Unit) AssertValue(v Value) error
- func (u *Unit) BoundsError(f float64) error
- func (u *Unit) Category() string
- func (u *Unit) Equals(b *Unit) bool
- func (u *Unit) Group() *Group
- func (u *Unit) HasMax() bool
- func (u *Unit) HasMin() bool
- func (u *Unit) Hash() uint64
- func (u *Unit) ID() string
- func (u *Unit) IsErr(err error) bool
- func (u *Unit) Max() float64
- func (u *Unit) Min() float64
- func (u *Unit) Name() string
- func (u *Unit) PlainString(f float64) string
- func (u *Unit) Precision() int
- func (u *Unit) String(f float64) string
- func (u *Unit) Unit() string
- func (u *Unit) Valid(f float64) bool
- func (u *Unit) Value(v float64) Value
- type Value
- func (v Value) Add(b Value) (Value, error)
- func (v Value) As(to *Unit) (Value, error)
- func (v Value) AsGuard(to *Unit) Value
- func (v Value) BoundsError() error
- func (v Value) Calculate(b Value, f Calculation) (Value, error)
- func (v Value) Compare(b Value, f Comparator) (bool, error)
- func (v Value) CompareGuard(b Value, f Comparator) bool
- func (v Value) Divide(b Value) (Value, error)
- func (v Value) Equals(b Value) (bool, error)
- func (v Value) Float() float64
- func (v Value) GreaterThan(b Value) (bool, error)
- func (v Value) GreaterThanEqual(b Value) (bool, error)
- func (v Value) IsNegative() (bool, error)
- func (v Value) IsOne() (bool, error)
- func (v Value) IsPositive() (bool, error)
- func (v Value) IsValid() bool
- func (v Value) IsZero() (bool, error)
- func (v Value) LessThan(b Value) (bool, error)
- func (v Value) LessThanEqual(b Value) (bool, error)
- func (v Value) Multiply(b Value) (Value, error)
- func (v Value) NotEqual(b Value) (bool, error)
- func (v Value) PlainString() string
- func (v Value) String() string
- func (v Value) Subtract(b Value) (Value, error)
- func (v Value) Unit() *Unit
- func (v Value) Value(f float64) Value
- func (v Value) Within(b, c Value) (bool, error)
Constants ¶
This section is empty.
Variables ¶
var ( // BasicGroup is a group of basic values BasicGroup *Group // Integer is effectively an integer value with no unit. Integer *Unit // Float is a value with no unit Float *Unit // Percent is a Unit bounded by 0..100 and has no decimal places Percent *Unit )
Functions ¶
func AssertEntry ¶
AssertEntry always returns nil. It's used as a placeholder to AssertCalculator to indicate a Value is required but can be any Value
func AssertTransformsAvailable ¶
AssertTransformsAvailable returns an error if it's not possible to transform between two units in either direction.
func AssertValid ¶
AssertValid is a simple Assertion that tests a Value is valid. This is the same as Value.BoundsError()
func CalculatorExists ¶
func Equal ¶
Equal returns true if both values are Equal.
Equality here is if the two values are Within 1e-9 of each other to account for rounding errors Within float64.
func FalseComparator ¶
FalseComparator is a Comparator that always returns false
func GetCalculatorIDs ¶
func GetCalculatorIDs() []string
GetCalculatorIDs return's the ID's of all registered Calculator's
func GetGroupIDs ¶
func GetGroupIDs() []string
GetGroupIDs returns a slice of all registered group ID's. The returned ID's will be sorted.
func GetTransforms ¶
GetTransforms returns a slice of TransformDef representing the defined transforms. The result will be sorted by Start and then by End.
func GreaterThan ¶
GreaterThan returns true if a > b, accounting for different units. It will return false if |a-b|<=1e-9 to account for rounding errors in float64.
func GreaterThanEqual ¶
GreaterThanEqual returns true if a >= b, accounting for different units. It will return true if |a-b|<=1e-9 to account for rounding errors in float64.
func IsNegative ¶
IsNegative returns true if the value is negative. 0 is neither positive nor negative/ Specifically if v < -1e-9 to account for rounding errors in float64.
func IsOne ¶
IsOne returns true if the value is 1. Specifically if |v-1|<1e-9 to account for rounding errors in float64.
func IsPositive ¶
IsPositive returns true if the value is positive. 0 is neither positive nor negative/ Specifically if v > 1e-9 to account for rounding errors in float64.
func IsZero ¶
IsZero returns true if the value is zero. Specifically if |v|<1e-9 to account for rounding errors in float64.
func LessThan ¶
LessThan returns true if a < b, accounting for different units. It will return false if |a-b|<=1e-9 to account for rounding errors in float64.
func LessThanEqual ¶
LessThanEqual returns true if a <= b, accounting for different units. It will return true if |a-b|<=1e-9 to account for rounding errors in float64.
func NewBasicBiTransform ¶
NewBasicBiTransform registers two transforms using a constant conversion factor. This allows for conversions between two units.
func NewBasicTransform ¶
NewBasicTransform is the same as NewTransform(from, to, BasicTransform(factor))
func NewBiTransform ¶
func NewBiTransform(u1, u2 *Unit, u1ToU2, u2ToU1 Transformer)
NewBiTransform registers two transforms, one for u1->u2 and one for u2->u1
func NewCalculator ¶
func NewCalculator(id string, calc Calculator)
NewCalculator registers a named Calculator
func NewTransform ¶
func NewTransform(from, to *Unit, t Transformer)
NewTransform registers a Transformer that will handle transforming from one Unit to Another. If a specific transform has already been registered or if both from and to represent the same Unit then this will panic.
func NotEqual ¶
NotEqual returns true if both values are Equal. It's the same as !Equal() and follows the same rules.
func Transform ¶
Transform will transform a float64 between two units. An error is returned if the requested transform has not been defined.
func TransformAvailable ¶
TransformAvailable returns true if it's possible to transfrom between two units.
func TransformsAvailable ¶
TransformsAvailable returns true if it's possible to transform between two units in either direction.
func TrueComparator ¶
TrueComparator is a Comparator that always returns true
Types ¶
type Calculation ¶
Calculation is a function that applies some mathematical calculation against two values. It is guaranteed that both values passed are in the same Unit.
type Calculator ¶
Calculator performs a calculation to generate a Value. It takes a time.Time to represent when this calculation represents
func AssertCalculator ¶
func AssertCalculator(c Calculator, a ...Assertion) Calculator
AssertCalculator wraps a Calculator to enforce the number and type of Value's passed to the Calculator. If the type of value is not required to be enforced, just that it exists then use AssertEntry or AssertValid.
func Basic1ArgCalculator ¶
func Basic1ArgCalculator(f func(float64) float64) Calculator
func Basic2ArgCalculator ¶
func Basic2ArgCalculator(f func(float64, float64) float64) Calculator
func Calculator2arg ¶
func Calculator2arg(f func(_, _ Value) (Value, error)) Calculator
Calculator2arg utility to convert a function that takes two Value's into a Calculator
func Calculator3arg ¶
func Calculator3arg(f func(_, _, _ Value) (Value, error)) Calculator
Calculator3arg utility to convert a function that takes three Value's into a Calculator
func GetCalculator ¶
func GetCalculator(id string) (Calculator, error)
GetCalculator returns a named Calculator
func (Calculator) As ¶
func (c Calculator) As(to *Unit) Calculator
As returns a Calculator which will attempt to transform the returned value from the wrapped Calculator to the required Unit
type Comparator ¶
Comparator is a function that can be passed to Compare two Values. It is guaranteed that the values supplied to it will be of the same Unit.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group represents a set of Unit's which are related. e.g. Units Celsius, Fahrenheit and Kelivn are members of the Temperature group.
func GetGroup ¶
GetGroup returns a registered Unit based on its name. If the unit is not registered then this returns (nil,false). Names are case insensitive.
func GetGroups ¶
func GetGroups() []*Group
GetGroups returns a slice of all registered group's. The returned Group's will be sorted by Group.Name.
func NewGroup ¶
NewGroup creates a group of Units.
This will panic if there are no Transform's available between any sibling Unit and the base Unit, or if it cannot create a Transform between any sibling and another via the base Unit.
func (*Group) AssertUnit ¶
AssertUnit will return an error if the supplied Unit is not a member of this Group. If either Group or Unit are nil then this returns nil.
func (*Group) AssertValue ¶
AssertValue will return an error if the supplied Value is not a member of this Group. If Group is nil then this returns nil.
func (*Group) IsError ¶
IsError returns true if the error was from either Assert or AssertUnit from this Group. If Group is nil then this returns false.
func (*Group) IsUnit ¶
IsUnit returns true if the unit is a member of this Group. If Group or Unit is nil then this returns false.
type Range ¶
type Range struct {
// contains filtered or unexported fields
}
func (*Range) Add ¶
Add a value to this Range. This will return an error if the supplied value cannot be transformed to the unit of this Range.
type Time ¶
type Time interface { // Time of some event Time() time.Time // SetTime sets the time, used so a single instance can be used // in iterators for the same location SetTime(time.Time) Time // Add a time.Duration to this Time Add(d time.Duration) // Location on Earth's surface Location() *globe.Coord // Altitude at Location() Altitude() float64 // Clone this Time. Clone() Time // ForEach will call a function for each step for duration time ForEach(step, duration time.Duration, f func(Time) error) error }
type Transformer ¶
Transformer is an operation that will transform a float64 between Unit's. This is the core of how a Unit can be transformed to another Unit.
func BasicInverseTransform ¶
func BasicInverseTransform(factor float64) Transformer
BasicInverseTransform returns a Transformer that divides a value with a constant conversion factor.
This is the same as BasicTransform(1.0/factor)
func BasicTransform ¶
func BasicTransform(factor float64) Transformer
BasicTransform returns a Transformer that multiplies a value with a constant conversion factor.
This is used for transforms like meters per second to kilometers per hour
func GetTransform ¶
func GetTransform(from, to *Unit) (Transformer, error)
GetTransform returns the Transformer that will transform between two units. An error is returned if the requested transform has not been defined.
func Of ¶
func Of(transforms ...Transformer) Transformer
Of allows for a sequence of Transformer's to be chained together to form a new Transform.
An example is value.Of(fahrenheitCelsius, celsiusKelvin) which creates a Transform that converts Fahrenheit to Kelvin by converting it to Celsius first.
func (Transformer) Then ¶
func (a Transformer) Then(b Transformer) Transformer
Then allows for one Transformer to pass its result to another one. This is used for transforming between two Units with an intermediate one. e.g. Fahrenheit to Kelvin is actually Fahrenheit -> Celsius -> Kelvin
type Unit ¶
type Unit struct {
// contains filtered or unexported fields
}
Unit represents a unit of some kind. For example for temperature we have Kelvin, Celsius and Fahrenheit.
Unit's support transformations with other units, which are registered with NewTransform. If no transform is registered for two units then they cannot be transformed.
func GetUnit ¶
GetUnit returns a registered Unit based on its id. If the unit is not registered then this returns (nil,false). Ids are case insensitive.
func GetUnitByHash ¶
GetUnitByHash returns a registered Unit based on its hash. If the unit is not registered then this returns (nil,false). This is usually used when we want to store the unit in a compressed form
func NewBoundedUnit ¶
NewBoundedUnit creates a new Unit which has both min and max values.
func NewLowerBoundUnit ¶
NewLowerBoundUnit creates a new Unit which has a lower limit on it's permitted values.
func NewUpperBoundUnit ¶
NewUpperBoundUnit creates a new Unit which has an upper limit on it's permitted values.
func (*Unit) AssertUnit ¶
AssertUnit will return an error if the two Unit's do not match. If either Unit is nil then this returns nil.
func (*Unit) AssertValue ¶
AssertValue returns an error if the Value's Unit does not match this Unit.
func (*Unit) BoundsError ¶
BoundsError returns an error if the unit is outside its bounds, NaN or Infinity
func (*Unit) Equals ¶
Equals returns true if the unit's names are identical. This is case-insensitive.
func (*Unit) Group ¶
Group returns the Group this Unit is a member of, or nil if it's not a member of one.
func (*Unit) PlainString ¶
func (*Unit) String ¶
String returns a float64 in it's supported format for this unit. This will be the value with the string from Unit() appended to it.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is a float64 with an associated Unit. Value's can be transformed between different Unit's if those unit's support a specific conversion.
func (Value) Add ¶
Add returns the sum of two values. The result is the same unit as v. An error is returned if either value is invalid, b could not be transformed into the same unit as v or if the result is itself invalid.
func (Value) As ¶
As converts this Value to another Unit. This will return an error if this value is invalid, if there is no available transform from this Value's Unit to the requested one, or if the result is invalid.
func (Value) AsGuard ¶
AsGuard is the same as the As function except that if an error would be returned then a panic is issued. This function is normally used Within tests
func (Value) BoundsError ¶
BoundsError returns an error if IsValid() returns false, nil otherwise.
func (Value) Calculate ¶
func (v Value) Calculate(b Value, f Calculation) (Value, error)
Calculate applies a Calculation against v and b returning a new Value or an error if either v or b are invalid, if b cannot be transformed into v's unit, or if the result is itself invalid.
func (Value) Compare ¶
func (v Value) Compare(b Value, f Comparator) (bool, error)
Compare will return the result of a Comparator when it's passed values from v and b. It will transform b to the same unit as v before passing it to the Comparator. An error will be returned if either value is invalid or it's not possible to transform b to v.
func (Value) CompareGuard ¶
func (v Value) CompareGuard(b Value, f Comparator) bool
CompareGuard is the same as Compare except that if the comparison fails it will panic. This is normally used Within tests.
func (Value) Divide ¶
Divide divides this value with b. The result is the same unit as v. An error is returned if either value is invalid, b could not be transformed into the same unit as v or if the result is itself invalid. If the transformed value of b is zero then a normal divide
func (Value) Equals ¶
Equals returns true if both values are Equal. This accounts for differing units. Returns an error if either value is invalid or if it's not possible to transform b to the same unit as v.
Equality here is if the two values are Within 1e-9 of each other to account for rounding errors Within float64.
func (Value) GreaterThan ¶
GreaterThan returns true if v > b, accounting for different units. It will return false if |v-b|<=1e-9 to account for rounding errors in float64.
func (Value) GreaterThanEqual ¶
GreaterThanEqual returns true if v >= b, accounting for different units. It will return true if |v-b|<=1e-9 to account for rounding errors in float64.
func (Value) IsNegative ¶
IsNegative returns true if the value is negative. 0 is neither positive nor negative/ Specifically if v < -1e-9 to account for rounding errors in float64.
func (Value) IsOne ¶
IsOne returns true if the value is 1. Specifically if |v-1|<1e-9 to account for rounding errors in float64.
func (Value) IsPositive ¶
IsPositive returns true if the value is positive. 0 is neither positive nor negative/ Specifically if v > 1e-9 to account for rounding errors in float64.
func (Value) IsValid ¶
IsValid returns true if the Value is valid, specifically if it's Within the bounds of the Unit.
If the value is NaN or either Infinity then this returns false.
func (Value) IsZero ¶
IsZero returns true if the value is zero. Specifically if |v|<1e-9 to account for rounding errors in float64.
func (Value) LessThan ¶
LessThan returns true if v < b, accounting for different units. It will return false if |v-b|<=1e-9 to account for rounding errors in float64.
func (Value) LessThanEqual ¶
LessThanEqual returns true if v <= b, accounting for different units. It will return true if |v-b|<=1e-9 to account for rounding errors in float64.
func (Value) Multiply ¶
Multiply returns the product of two values. The result is the same unit as v. An error is returned if either value is invalid, b could not be transformed into the same unit as v or if the result is itself invalid.
func (Value) NotEqual ¶
NotEqual returns true if both values are Equal. It's the same as !Equal() and follows the same rules.
func (Value) PlainString ¶
func (Value) Subtract ¶
Subtract subtracts b from this value. The result is the same unit as v. An error is returned if either value is invalid, b could not be transformed into the same unit as v or if the result is itself invalid.