utils

package
v0.47.0-rc.7 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2023 License: MIT Imports: 10 Imported by: 4

Documentation

Overview

Package utils contains helper functions used across the project

Index

Constants

This section is empty.

Variables

View Source
var ErrArgNotStruct = errors.New("argument is not a struct")
View Source
var ErrCannotRender = errors.New("field inner type cannot be rendered")
View Source
var ErrNoFieldCollected = errors.New("no field was collected")
View Source
var ErrNoJSONTag = errors.New("field doesn't have a json tag")
View Source
var ErrNoValueMethod = errors.New("field doesn't have the expected Value method")
View Source
var ErrNotCollectable = fmt.Errorf("cannot be collected on %s %s", runtime.GOOS, runtime.GOARCH)
View Source
var ErrNotExported = errors.New("field not exported by the struct")

Functions

func AsJSON added in v0.47.0

func AsJSON[T any](info *T, useDefault bool) (interface{}, []string, error)

AsJSON takes a structure and returns a marshal-able object representing the fields of the struct, the lists of errors for fields for which the collection failed, and an error if it failed.

If useDefault is true, fields which failed to be collected will be included in the marshal-able object as their default value, otherwise they are ignored.

Fields which are not exported, don't have a json tag or are not of type Value[T] for a T which can be rendered cause the function to return an error.

The string list contain errors of fields which failed to be collected. Fields which are not collected on the platform are ignored.

If the error is non-nil, the first two parameters are unspecified.

func AssertDecodedValue added in v0.47.0

func AssertDecodedValue[T any](t *testing.T, decoded string, value *Value[T], unit string)

AssertDecodedValue asserts that either - the field is an error and the decoded string is empty - the field is not an error and the value with its unit is equal to the decoded string

decoded should be a string read by unmarshalling a json, and value the original Value[T] which corresponds to that string.

func GetFloat64

func GetFloat64(dict map[string]string, key string, warnings *[]string) float64

GetFloat64 returns the dict[key] or an empty uint64 if the entry doesn't exists

func GetString

func GetString(dict map[string]string, key string) string

GetString returns the dict[key] or an empty string if the entry doesn't exists

func GetStringInterface

func GetStringInterface(dict map[string]interface{}, key string) string

GetStringInterface returns the dict[key] or an empty string if the entry doesn't exists or the value is not a string

func GetUint64

func GetUint64(dict map[string]string, key string, warnings *[]string) uint64

GetUint64 returns the dict[key] or an empty uint64 if the entry doesn't exists

func ParseFakeExecCmdArgs

func ParseFakeExecCmdArgs() (string, []string)

ParseFakeExecCmdArgs parses the CLI's os.Args as passed by fakeExecCmd and returns the testRunName, and cmdList. Meant to be used from test functions that are called by a fakeExecCmd built with BuildFakeExecCmd.

func StringFromBytes added in v0.47.0

func StringFromBytes(slice []byte) string

StringFromBytes converts a null-terminated (C-style) string to a Go string

The given slice must be null-terminated.

func ValueParseFloat64Setter added in v0.47.0

func ValueParseFloat64Setter(value *Value[float64]) func(string, error)

ValueParseFloat64Setter returns a function which parses a float64 from its string argument and stores the result in the given Value

func ValueParseInt64Setter added in v0.47.0

func ValueParseInt64Setter(value *Value[uint64]) func(string, error)

ValueParseInt64Setter returns a function which parses an uint64 from its string argument and stores the result in the given Value

func ValueParseSetter added in v0.47.0

func ValueParseSetter[T any](value *Value[T], parse func(string) (T, error)) func(string, error)

ValueParseSetter returns a function which parses its input and stores the result in the given Value

func ValueStringSetter added in v0.47.0

func ValueStringSetter(value *Value[string]) func(string, error)

ValueStringSetter returns a function which stores a string and an error in the given Value

Types

type ExecCmdFunc

type ExecCmdFunc = func(name string, arg ...string) *exec.Cmd

ExecCmdFunc is a function type that matches exec.Command's signature

func BuildFakeExecCmd

func BuildFakeExecCmd(testName string, testRunName string) ExecCmdFunc

BuildFakeExecCmd returns a fakeExecCmd for the testName and testRunName. See platform/platform_common_test.go for an example of how to use it to mock exec.Cmd in tests.

type Value added in v0.47.0

type Value[T any] struct {
	// contains filtered or unexported fields
}

Value represents either an error or an actual value of type T.

The default value of the type is an error saying that the value was not initialized.

func NewErrorValue added in v0.47.0

func NewErrorValue[T any](err error) Value[T]

NewErrorValue initializes a Value[T] with the given error.

Note that if err is nil, the returned Value[T] is fundamentally equivalent to a Value[T] containing the default value of T and no error.

func NewValue added in v0.47.0

func NewValue[T any](value T) Value[T]

NewValue initializes a Value[T] with the given value of type T and no error.

func NewValueFrom added in v0.47.0

func NewValueFrom[T any](value T, err error) Value[T]

NewValueFrom returns a Value[T] from a value and an error. If the error is non-nil then it represents this error, otherwise it represents the value.

This is a convenient function to get a Value from a function which returns a value and an error.

func (*Value[T]) Error added in v0.47.0

func (value *Value[T]) Error() error

Error returns the error stored in the Value[T], or nil if it doesn't contain an error.

func (*Value[T]) Value added in v0.47.0

func (value *Value[T]) Value() (T, error)

Value returns the value and the error stored in the Value[T].

If the Value[T] represents an error, it returns the default value of type T and a non-nil error, otherwise the stored value of type T and a nil error.

Jump to

Keyboard shortcuts

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