cast

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: May 19, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowAll

func AllowAll() optIsString

AllowAll option allows all options (makes it the most non-strict)

func AllowBytesConversion

func AllowBytesConversion() optIsString

AllowBytesConversion option allows conversion from []byte to string for IsString checks.

func AllowCustomTypes

func AllowCustomTypes() optIsString

AllowCustomTypes option allows the use of custom string types for IsString checks.

func AllowDeepPointers

func AllowDeepPointers() optIsString

AllowDeepPointers option allows deep checking of values under pointers for IsString checks.

func AllowPointers

func AllowPointers() optIsString

AllowPointers option allows checking of values under pointers for IsString checks.

func AsBool

func AsBool(a any) bool

AsBool converts the given input into a bool. It supports various input types, including actual bool values and pointers to bool. Input values may also be pointers.

It panics if it's not possible to perform the conversion.

Example Usage:

value = AsBool(true) // Converts a bool, returns true
value = AsBool(&boolValue) // Converts a pointer to a bool, returns the bool value

This function is designed for converting different input types into bool values, and it is useful for various testing scenarios where boolean values are expected.

func AsBytes

func AsBytes(a any) []byte

AsBytes converts the given input into a []byte or a []byte-like representation. It supports various input types, including byte slices, JSON RawMessage, and strings.

Input values may also be pointers.

It panics if it's not possible to perform the conversion.

Example Usage:

bytes = AsBytes([]byte("byte_data")) // Converts a byte slice, returns []byte with the same content
bytes = AsBytes(jsonRawMessage) // Converts a JSON RawMessage
bytes = AsBytes("example") // Converts a string, returns the corresponding []byte

This function is useful for converting diverse input types into a []byte representation, and it is designed to provide a convenient []byte conversion for various testing scenarios.

func AsFloat

func AsFloat(a any) float64

AsFloat converts the given input into a float64. It supports various input types, including float64 values and int values (converted to float64). Input values may also be pointers.

It panics if it's not possible to perform the conversion.

Example Usage:

floatValue = AsFloat(3.14) // Converts a float64, returns 3.14
floatValue = AsFloat(&floatValuePtr) // Converts a pointer to float64, returns the float64 value
floatValue = AsFloat(42) // Converts an int to a float64, returns 42.0

This function is designed for converting different input types into float64 values, and it is useful for various testing scenarios where floating-point values are expected.

func AsInt

func AsInt(a any) int

AsInt converts the given input into an int. It supports various input types, including int values, float64 values (for integral floats), and pointers to int or float64. Input values may also be pointers.

Note (1): Float64 values are converted to int only if they are integral floats (e.g., 42.0). Otherwise, use AsFloat. Note (2): Depending on the machine where the code is compiled, the resulting int may be of different sizes (e.g., int32).

It panics if it's not possible to perform the conversion.

Example Usage:

intValue := AsInt(42) // Converts an int, returns 42
intValue := AsInt(&intValuePtr) // Converts a pointer to int, returns the int value
intValue := AsInt(42.0) // Converts an integral float, returns 42

This function is designed for converting different input types into int values, and it is useful for various testing scenarios where integer values are expected.

func AsKind

func AsKind(a any) reflect.Kind

AsKind converts the given input into a reflect.Kind. It supports various input types, including reflect.Kind values and pointers to reflect.Kind. Input values may also be pointers.

It panics if it's not possible to perform the conversion.

Example Usage:

kind = AsKind(reflect.Int) // Converts a reflect.Kind, returns reflect.Int
kind = AsKind(&kindPtr) // Converts a pointer to reflect.Kind, returns the reflect.Kind value

This function is designed for converting different input types into reflect.Kind values, and it is useful for various testing scenarios where reflection is used.

func AsSliceOfAny

func AsSliceOfAny(v any) []any

AsSliceOfAny converts the given input into a []any. It supports various input types, including slices, arrays, and pointers to slices or arrays of any type.

It panics if it's not possible to perform the conversion.

Example Usage:

anySlice := AsSliceOfAny([]string{"a", "b", "c"}) // Converts a string slice, returns []any{"a", "b", "c"}
anySlice := AsSliceOfAny(&intArrayPtr) // Converts a pointer to an array, returns []any with the array content

This function is designed for converting different input types into a []any, and it is useful for various testing scenarios where a slice of arbitrary types is expected.

func AsString

func AsString(a any) string

AsString converts the given input into a string or a string-like representation. It supports various input types, including actual strings, byte slices, JSON RawMessage, and custom string types.

Input values may also be pointers.

Note: If the input is []byte and contains a non-UTF-8 valid sequence, the resulting string may be invalid.

It panics if it's not possible to perform the conversion.

Example Usage:

str = AsString("example") // Converts a string, returns "example"
str = AsString([]byte("byte_data")) // Converts a byte slice, returns "byte_data"
str = AsString(CustomStringType("example")) // Converts a custom string type

This function is useful for converting diverse input types into a string representation, and it is designed to provide a convenient string conversion for various testing scenarios.

func AsStrings

func AsStrings(v any) []string

func AsTime

func AsTime(a any) time.Time

AsTime converts the given input into a time.Time. It supports various input types, including time.Time values and pointers to time.Time.

It panics if it's not possible to perform the conversion.

Example Usage:

timestamp = AsTime(time.Now()) // Converts a time.Time, returns the current time
timestamp = AsTime(&timeValuePtr) // Converts a pointer to time.Time, returns the time.Time value

This function is designed for converting different input types into time.Time values.

func ConfigureIsStringConfig

func ConfigureIsStringConfig(opts ...optIsString)

ConfigureIsStringConfig sets the default configuration for IsString checks.

func IsNil

func IsNil(a any) bool

IsNil checks if the given input is a nil value.

func IsString

func IsString(a any, opts ...optIsString) bool

IsString checks if the given input is a string or string-like. To avoid duplicating type-checking logic, it provides extensive configuration options for customizing the type-checking behavior, making it a versatile utility for testing code. It supports both strict and non-strict mode checks, allowing you to precisely control which types are considered string-like. It also provides options for handling custom types, pointer de-referencing.

Example Usage:

// In a non-strict check, allows custom types, pointer de-referencing.
IsString("example", AllowCustomTypes(), AllowPointers())) // returns true

// In a strict check, only actual strings are accepted
IsString("example", Strict()) // Returns true
IsString([]byte("example"), Strict()) // Returns false

func IsStringish

func IsStringish(a any) (ok bool)

IsStringish checks if the given input is a string or string-like value. To prevent code duplication, it employs panic recovery to handle type conversion and is designed for use in testing code, where panics are acceptable.

Example Usage:

IsStringish("example") // Returns true
IsStringish([]byte("example")) // Returns true
IsStringish(CustomStringType("example")) // Returns true

This function is suitable for scenarios where you want to quickly determine if a value can be treated as a string without handling detailed conversion errors.

func IsStrings

func IsStrings(a any) (ok bool)

func IsTime

func IsTime(a any) (ok bool)

IsTime checks if the given input is a time.Time value (pointers and/or custom types are OK) To prevent code duplication, it employs panic recovery to handle type conversion and is designed for use in testing code, where panics are acceptable.

Types

This section is empty.

Jump to

Keyboard shortcuts

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