value

package
v0.0.0-...-99d84d4 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 Add

func Add(a, b float64) float64

func AssertEntry

func AssertEntry(_ Value) error

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

func AssertTransformsAvailable(a, b *Unit) error

AssertTransformsAvailable returns an error if it's not possible to transform between two units in either direction.

func AssertValid

func AssertValid(v Value) error

AssertValid is a simple Assertion that tests a Value is valid. This is the same as Value.BoundsError()

func CalculatorExists

func CalculatorExists(id string) bool

func Divide

func Divide(a, b float64) float64

func Equal

func Equal(a, b float64) bool

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

func FalseComparator(_, _ float64) bool

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

func GetTransforms() map[string][]string

GetTransforms returns a slice of TransformDef representing the defined transforms. The result will be sorted by Start and then by End.

func GreaterThan

func GreaterThan(a, b float64) bool

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

func GreaterThanEqual(a, b float64) bool

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

func IsNegative(f float64) bool

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

func IsOne(f float64) bool

IsOne returns true if the value is 1. Specifically if |v-1|<1e-9 to account for rounding errors in float64.

func IsPositive

func IsPositive(f float64) bool

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

func IsZero(f float64) bool

IsZero returns true if the value is zero. Specifically if |v|<1e-9 to account for rounding errors in float64.

func LessThan

func LessThan(a, b float64) bool

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

func LessThanEqual(a, b float64) bool

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 Multiply

func Multiply(a, b float64) float64

func NewBasicBiTransform

func NewBasicBiTransform(u1, u2 *Unit, factor float64)

NewBasicBiTransform registers two transforms using a constant conversion factor. This allows for conversions between two units.

func NewBasicTransform

func NewBasicTransform(from, to *Unit, factor float64)

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 NopTransformer

func NopTransformer(f float64) (float64, error)

NopTransformer does nothing.

func NotEqual

func NotEqual(a, b float64) bool

NotEqual returns true if both values are Equal. It's the same as !Equal() and follows the same rules.

func ResetMap

func ResetMap(ctx context.Context) error

func Subtract

func Subtract(a, b float64) float64

func Transform

func Transform(f float64, from, to *Unit) (float64, error)

Transform will transform a float64 between two units. An error is returned if the requested transform has not been defined.

func TransformAvailable

func TransformAvailable(from, to *Unit) bool

TransformAvailable returns true if it's possible to transfrom between two units.

func TransformsAvailable

func TransformsAvailable(a, b *Unit) bool

TransformsAvailable returns true if it's possible to transform between two units in either direction.

func TrueComparator

func TrueComparator(_, _ float64) bool

TrueComparator is a Comparator that always returns true

func WithMap

func WithMap(ctx context.Context) context.Context

func Within

func Within(a, b, c float64) bool

Within returns true if b >= a <= c

func Without

func Without(a, b, c float64) bool

Without returns true if a <= b || a >= c

Types

type Assertion

type Assertion func(Value) error

Assertion tests a Value and returns an error if something is wrong

type Calculation

type Calculation func(float64, float64) float64

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

type Calculator func(Time, ...Value) (Value, error)

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

type Comparator func(a, b float64) bool

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

func GetGroup(id string) (*Group, bool)

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

func NewGroup(name string, base *Unit, siblings ...*Unit) *Group

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

func (g *Group) AssertUnit(u *Unit) error

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

func (g *Group) AssertValue(v Value) error

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

func (g *Group) IsError(err error) bool

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

func (g *Group) IsUnit(u *Unit) bool

IsUnit returns true if the unit is a member of this Group. If Group or Unit is nil then this returns false.

func (*Group) IsValue

func (g *Group) IsValue(v Value) bool

IsValue returns true if the Value's Unit is a member of this Group. If Group is nil then this returns false.

func (*Group) Name

func (g *Group) Name() string

Name of the Group

func (*Group) Units

func (g *Group) Units() []*Unit

Units returns the units forming this group

type Map

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

func MapFromContext

func MapFromContext(ctx context.Context) *Map

func (*Map) Get

func (m *Map) Get(k string) Value

func (*Map) GetAll

func (m *Map) GetAll(keys ...string) []Value

func (*Map) GetKeys

func (m *Map) GetKeys() []string

func (*Map) Put

func (m *Map) Put(k string, v Value)

func (*Map) Reset

func (m *Map) Reset()

type Range

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

func NewRange

func NewRange(u *Unit) *Range

func (*Range) Add

func (r *Range) Add(v Value) error

Add a value to this Range. This will return an error if the supplied value cannot be transformed to the unit of this Range.

func (*Range) Clone

func (r *Range) Clone() *Range

func (*Range) Include

func (r *Range) Include(b *Range) error

func (*Range) IsValid

func (r *Range) IsValid() bool

IsValid returns true if the Range is valid. Specifically that it has had a value entered into it so that min<=max

func (*Range) Max

func (r *Range) Max() Value

func (*Range) Min

func (r *Range) Min() Value

func (*Range) Range

func (r *Range) Range() (Value, Value)

func (*Range) Unit

func (r *Range) Unit() *Unit

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
}

func BasicTime

func BasicTime(t time.Time, loc *globe.Coord, alt float64) Time

BasicTime returns a time with the static coordinates

func PlainTime

func PlainTime(t time.Time) Time

PlainTime is a Time with no positional component

type Transformer

type Transformer func(f float64) (float64, error)

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

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

func GetUnit(id string) (*Unit, bool)

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

func GetUnitByHash(h uint64) (*Unit, bool)

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 GetUnits

func GetUnits() []*Unit

GetUnits returns a slice of all Unit's

func NewBoundedUnit

func NewBoundedUnit(id, name, unit string, precision int, min, max float64) *Unit

NewBoundedUnit creates a new Unit which has both min and max values.

func NewLowerBoundUnit

func NewLowerBoundUnit(id, name, unit string, precision int, min float64) *Unit

NewLowerBoundUnit creates a new Unit which has a lower limit on it's permitted values.

func NewUnit

func NewUnit(id, name, unit string, precision int) *Unit

NewUnit creates a new Unit, registering it with the system.

func NewUpperBoundUnit

func NewUpperBoundUnit(id, name, unit string, precision int, max float64) *Unit

NewUpperBoundUnit creates a new Unit which has an upper limit on it's permitted values.

func (*Unit) AssertUnit

func (u *Unit) AssertUnit(b *Unit) error

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

func (u *Unit) AssertValue(v Value) error

AssertValue returns an error if the Value's Unit does not match this Unit.

func (*Unit) BoundsError

func (u *Unit) BoundsError(f float64) error

BoundsError returns an error if the unit is outside its bounds, NaN or Infinity

func (*Unit) Category

func (u *Unit) Category() string

func (*Unit) Equals

func (u *Unit) Equals(b *Unit) bool

Equals returns true if the unit's names are identical. This is case-insensitive.

func (*Unit) Group

func (u *Unit) Group() *Group

Group returns the Group this Unit is a member of, or nil if it's not a member of one.

func (*Unit) HasMax

func (u *Unit) HasMax() bool

func (*Unit) HasMin

func (u *Unit) HasMin() bool

func (*Unit) Hash

func (u *Unit) Hash() uint64

func (*Unit) ID

func (u *Unit) ID() string

func (*Unit) IsErr

func (u *Unit) IsErr(err error) bool

IsErr returns true if the error was returned by AssertUnit or AssertValue.

func (*Unit) Max

func (u *Unit) Max() float64

func (*Unit) Min

func (u *Unit) Min() float64

func (*Unit) Name

func (u *Unit) Name() string

Name of the Unit. e.g. "Celsius"

func (*Unit) PlainString

func (u *Unit) PlainString(f float64) string

func (*Unit) Precision

func (u *Unit) Precision() int

func (*Unit) String

func (u *Unit) String(f float64) 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.

func (*Unit) Unit

func (u *Unit) Unit() string

Unit for strings, e.g. "°C"

func (*Unit) Valid

func (u *Unit) Valid(f float64) bool

Valid returns true if f is Within the bounds of this unit. e.g. For Kelvin f must be >=0 as negative values would be invalid as you cannot have temperatures below Absolute Zero.

If the value is NaN or either Infinity then this returns false.

func (*Unit) Value

func (u *Unit) Value(v float64) Value

Value returns a Value with this Unit. This is the only method to create a Value.

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

func (v Value) Add(b Value) (Value, error)

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

func (v Value) As(to *Unit) (Value, error)

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

func (v Value) AsGuard(to *Unit) Value

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

func (v Value) BoundsError() error

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

func (v Value) Divide(b Value) (Value, error)

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

func (v Value) Equals(b Value) (bool, error)

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

func (v Value) Float() float64

Float returns the float64 in the Value's Unit

func (Value) GreaterThan

func (v Value) GreaterThan(b Value) (bool, error)

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

func (v Value) GreaterThanEqual(b Value) (bool, error)

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

func (v Value) IsNegative() (bool, error)

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

func (v Value) IsOne() (bool, error)

IsOne returns true if the value is 1. Specifically if |v-1|<1e-9 to account for rounding errors in float64.

func (Value) IsPositive

func (v Value) IsPositive() (bool, error)

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

func (v Value) IsValid() bool

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

func (v Value) IsZero() (bool, error)

IsZero returns true if the value is zero. Specifically if |v|<1e-9 to account for rounding errors in float64.

func (Value) LessThan

func (v Value) LessThan(b Value) (bool, error)

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

func (v Value) LessThanEqual(b Value) (bool, error)

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

func (v Value) Multiply(b Value) (Value, error)

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

func (v Value) NotEqual(b Value) (bool, error)

NotEqual returns true if both values are Equal. It's the same as !Equal() and follows the same rules.

func (Value) PlainString

func (v Value) PlainString() string

func (Value) String

func (v Value) String() string

String returns this Value as a string with the appropriate Unit attached

func (Value) Subtract

func (v Value) Subtract(b Value) (Value, error)

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.

func (Value) Unit

func (v Value) Unit() *Unit

Unit returns the Unit for this Value

func (Value) Value

func (v Value) Value(f float64) Value

Value returns a new Value with the same unit as this one. This is the same as v.Unit().Value(f).

If the value has no Unit then this returns an invalid Unit.

If the value of the new unit is outside its bounds, it will still be returned however it will indicate it's not valid.

func (Value) Within

func (v Value) Within(b, c Value) (bool, error)

Within returns true if b >= v <= c. It returns an error if any of the three values are invalid or if it's not possible to transform b and c to the same unit as v.

Jump to

Keyboard shortcuts

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