Documentation
¶
Overview ¶
Package utils for converting stuff into other stuff.
Index ¶
- Variables
- func AppendCostReportItem(haystack *fleetcostpb.CostReportExpr, needles ...*fleetcostpb.CostReportItem)
- func ConsumeChannel[T any](ctx context.Context, opts *ConsumeChannelOptions, source <-chan T, ...) error
- func DeleteOneIfExists(ctx context.Context, entity interface{ ... }, ...) error
- func ErrorStringContains(e error, msg string) bool
- func FloatToMoney(val float64) *money.Money
- func IndicatorToString(indicator *fleetcostpb.CostIndicator) string
- func InsertOneWithoutReplacement(ctx context.Context, entity interface{ ... }, ...) error
- func MakeCostReportExpr(label string, operation string) *fleetcostpb.CostReportExpr
- func MakeCostReportItem(subtotalName string, rawFigure float64, perDeviceFigure float64, ...) *fleetcostpb.CostReportItem
- func MakeCostReportScalar(label string, value float64) *fleetcostpb.CostReportScalar
- func MaybeErrorf(ctx context.Context, e error) error
- func MoneyToFloat(v *money.Money) float64
- func PrintMultiError(a subcommands.Application, err error)
- func RunPerhapsInTransaction(ctx context.Context, callback func(context.Context) error, ...) error
- func SafeDivide(x float64, y float64) (float64, error)
- func SplitComma(msg string) []string
- func SumCostReportItem(items ...*fleetcostpb.CostReportItem) float64
- func ToCostCadence(x string) (fleetcostpb.CostCadence, error)
- func ToIndicatorType(x string) (fleetcostpb.IndicatorType, error)
- func ToLocation(x string) (fleetcostpb.Location, error)
- func ToUSD(x string) (*money.Money, error)
- type ConsumeChannelOptions
Constants ¶
This section is empty.
Variables ¶
var ErrItemExists = errors.New("item already exists, cannot replace")
ErrItemExists applies when we try to insert an item that already exists.
Functions ¶
func AppendCostReportItem ¶
func AppendCostReportItem(haystack *fleetcostpb.CostReportExpr, needles ...*fleetcostpb.CostReportItem)
func ConsumeChannel ¶
func ConsumeChannel[T any](ctx context.Context, opts *ConsumeChannelOptions, source <-chan T, callback func(context.Context, T) error) error
ConsumeChannel consumes a channel containing data until it is closed, then it hands back all the errors it encountered, up to a maximum.
ConsumeChannel does not leak any goroutines and hands control back to the caller when it is done.
Note, however, that ConsumeChannel expects the channel source to *CLOSE*. If the channel does not close, then it will hang forever.
TODO(gregorynisbet): This is a generic parallelism utility. Move it to a LUCI area or another general purpose library once it is mature enough to do so.
func DeleteOneIfExists ¶
func DeleteOneIfExists(ctx context.Context, entity interface { datastore.PropertyLoadSaver datastore.MetaGetterSetter }, options *datastore.TransactionOptions) error
DeleteOneIfExists deletes an entity if it exists.
func ErrorStringContains ¶
ErrorStringContains is a helper function that returns true if and only if the error in question contains the substring msg.
func FloatToMoney ¶
func IndicatorToString ¶
func IndicatorToString(indicator *fleetcostpb.CostIndicator) string
IndicatorToString converts a cost indicator to a friendly string.
func InsertOneWithoutReplacement ¶
func InsertOneWithoutReplacement(ctx context.Context, entity interface { datastore.PropertyLoadSaver datastore.MetaGetterSetter }, options *datastore.TransactionOptions) error
InsertOneWithoutReplacement inserts an item without replacement.
We insist on having a PropertyLoadSaver+MetaGetterSetter (rather than taking an any) because this function only inserts one thing without replacement. (It's not clear what the semantics should be if you want to replace multiple things without replacement).
func MakeCostReportExpr ¶
func MakeCostReportExpr(label string, operation string) *fleetcostpb.CostReportExpr
func MakeCostReportItem ¶
func MakeCostReportItem(subtotalName string, rawFigure float64, perDeviceFigure float64, category string) *fleetcostpb.CostReportItem
func MakeCostReportScalar ¶
func MakeCostReportScalar(label string, value float64) *fleetcostpb.CostReportScalar
func MaybeErrorf ¶
MaybeErrorf logs an error if it's non-nil and then returns it.
func MoneyToFloat ¶
func PrintMultiError ¶
func PrintMultiError(a subcommands.Application, err error)
PrintMultiError prints a multierror, unwrapping as necessary.
func RunPerhapsInTransaction ¶
func RunPerhapsInTransaction(ctx context.Context, callback func(context.Context) error, options *datastore.TransactionOptions) error
RunPerhapsInTransaction runs a datastore command perhaps in a transaction.
func SafeDivide ¶
SafeDivide divides two float64s and panics if the result is nonsensical.
func SumCostReportItem ¶
func SumCostReportItem(items ...*fleetcostpb.CostReportItem) float64
func ToCostCadence ¶
func ToCostCadence(x string) (fleetcostpb.CostCadence, error)
ToCostCadence converts a string to a cost cadence.
func ToIndicatorType ¶
func ToIndicatorType(x string) (fleetcostpb.IndicatorType, error)
ToIndicatorType converts a string to an indicator.
func ToLocation ¶
func ToLocation(x string) (fleetcostpb.Location, error)
ToLocation converts a string to a location.
func ToUSD ¶
ToUSD converts a string on the command line to US dollars.
Right now, it works by parsing a float. I'm not sure this is great, we really should be parsing the number as a big decimal without going through a float, but the resulting code for doing that, right now, ends up being more complex than is justified.
I'm writing this comment partially to express my frustration at not knowing a simpler way to parse an arbitrary-precision big decimal from the command line (none of the stuff in math/big is an exact match) and partially to exhort my future self or other readers to replace this function with something better.
Types ¶
type ConsumeChannelOptions ¶
ConsumeChannelOptions provides options for controlling the parallelism and maximum number of returned errors.
func (*ConsumeChannelOptions) GetJobs ¶
func (c *ConsumeChannelOptions) GetJobs() int
GetJobs gets the number of jobs and applies a default of 10000.
func (*ConsumeChannelOptions) GetMaxErrors ¶
func (c *ConsumeChannelOptions) GetMaxErrors() int
GetMaxErrors gets the number of errors and applies a default of 10000.