Documentation ¶
Overview ¶
Package utils contains helper functions used across the project
Index ¶
- Variables
- func AsJSON[T any](info *T, useDefault bool) (interface{}, []string, error)
- func AssertDecodedValue[T any](t *testing.T, decoded string, value *Value[T], unit string)
- func GetFloat64(dict map[string]string, key string, warnings *[]string) float64
- func GetString(dict map[string]string, key string) string
- func GetStringInterface(dict map[string]interface{}, key string) string
- func GetUint64(dict map[string]string, key string, warnings *[]string) uint64
- func ParseFakeExecCmdArgs() (string, []string)
- func StringFromBytes(slice []byte) string
- func ValueParseFloat64Setter(value *Value[float64]) func(string, error)
- func ValueParseInt64Setter(value *Value[uint64]) func(string, error)
- func ValueParseSetter[T any](value *Value[T], parse func(string) (T, error)) func(string, error)
- func ValueStringSetter(value *Value[string]) func(string, error)
- type ExecCmdFunc
- type Value
Constants ¶
This section is empty.
Variables ¶
var ErrArgNotStruct = errors.New("argument is not a struct")
var ErrCannotRender = errors.New("field inner type cannot be rendered")
var ErrNoFieldCollected = errors.New("no field was collected")
var ErrNoJSONTag = errors.New("field doesn't have a json tag")
var ErrNoValueMethod = errors.New("field doesn't have the expected Value method")
var ErrNotCollectable = fmt.Errorf("cannot be collected on %s %s", runtime.GOOS, runtime.GOARCH)
var ErrNotExported = errors.New("field not exported by the struct")
Functions ¶
func AsJSON ¶ added in v0.47.0
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
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 ¶
GetFloat64 returns the dict[key] or an empty uint64 if the entry doesn't exists
func GetStringInterface ¶
GetStringInterface returns the dict[key] or an empty string if the entry doesn't exists or the value is not a string
func ParseFakeExecCmdArgs ¶
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
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
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
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
ValueParseSetter returns a function which parses its input and stores the result in the given Value
Types ¶
type ExecCmdFunc ¶
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
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
NewValue initializes a Value[T] with the given value of type T and no error.
func NewValueFrom ¶ added in v0.47.0
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.