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 RequireMarshallJSON[T Jsonable, U any](t *testing.T, info T, decoded *U)
- 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 Jsonable
- type Value
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoFieldCollected means no field could be collected ErrNoFieldCollected = errors.New("no field was collected") // ErrArgNotStruct means the argument should be a struct but wasn't ErrArgNotStruct = errors.New("argument is not a struct") // ErrNotCollectable means the value can't be collected on the given platform ErrNotCollectable = fmt.Errorf("cannot be collected on %s %s", runtime.GOOS, runtime.GOARCH) // ErrNotExported means the struct has an unexported field ErrNotExported = errors.New("field not exported by the struct") // ErrCannotRender means a field which cannot be rendered ErrCannotRender = errors.New("field inner type cannot be rendered") // ErrNoValueMethod means a field doesn't have a Value method ErrNoValueMethod = errors.New("field doesn't have the expected Value method") // ErrNoJSONTag means a field doesn't have a json tag ErrNoJSONTag = errors.New("field doesn't have a json tag") )
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 RequireMarshallJSON ¶ added in v0.48.0
RequireMarshallJSON checks that - calling the AsJSON function succeeds - the object returned generates a json without error - the JSON can be unmarshalled into the given decoded struct - the JSON doesn't contain unexpected fields
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 Jsonable ¶ added in v0.48.0
Jsonable represents a type which can be converted to a mashallable object
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.
func (*Value[T]) Error ¶ added in v0.47.0
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
Value returns the value and the error stored in the Value[T].
If it contains an error, the returned value is unspecified.
func (*Value[T]) ValueOrDefault ¶ added in v0.48.0
func (value *Value[T]) ValueOrDefault() T
ValueOrDefault returns the value stored in the Value[T], or the default value of the type in case of error.