utils

package
v0.0.0-...-9febfc8 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2025 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package utils for converting stuff into other stuff.

Index

Constants

This section is empty.

Variables

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

func ErrorStringContains(e error, msg string) bool

ErrorStringContains is a helper function that returns true if and only if the error in question contains the substring msg.

func FloatToMoney

func FloatToMoney(val float64) *money.Money

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

func MaybeErrorf(ctx context.Context, e error) error

MaybeErrorf logs an error if it's non-nil and then returns it.

func MoneyToFloat

func MoneyToFloat(v *money.Money) float64

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

func SafeDivide(x float64, y float64) (float64, error)

SafeDivide divides two float64s and panics if the result is nonsensical.

func SplitComma

func SplitComma(msg string) []string

SplitComma splits a string on commas.

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

func ToUSD(x string) (*money.Money, error)

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

type ConsumeChannelOptions struct {
	Jobs      int
	MaxErrors int
}

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.

Jump to

Keyboard shortcuts

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