Documentation
¶
Overview ¶
Commons includes utility functions used across projects.
Index ¶
- Constants
- func AssertDeepEqual(t *testing.T, expected, actual any)
- func BoolString(b bool) string
- func BsonAToSlice(m any) ([]map[string]any, error)
- func BsonAnyToMap(v any) any
- func BsonClone(m map[string]any) map[string]any
- func BsonDFromSlice(in [][]any) bson.D
- func BsonDGet[T any](d bson.D, key string) (T, bool)
- func BsonDGetAny(d bson.D, key string) (any, bool)
- func BsonDGetString(d bson.D, key string) (string, bool)
- func BsonToMap(b bson.M) map[string]any
- func BsonToStruct(m bson.M, o any) error
- func ContextFromMap(m map[string]any) context.Context
- func ContextSerialize(ctx context.Context, fields []string) map[string]any
- func FileDeepScan(dir string, files *[]string)
- func Get(m map[string]any, path string) any
- func GetGeneric[T any](m map[string]any, path string) (T, bool)
- func GetGenericWithDefault[T any](m map[string]any, path string, def T) T
- func GetString(m map[string]any, path string) string
- func GetStringWithDefault(m map[string]any, path, def string) string
- func GetWithDefault(m map[string]any, path string, def any) any
- func JsonEqual(a, b string) bool
- func JsonPrint(a any)
- func JsonPrintError(err error, content []byte)
- func JsonSprint(a any) string
- func JsonSprintError(err error, content []byte) (error, string)
- func JsonToMap(s string) (map[string]any, error)
- func MapFromString(s string) (map[string]any, error)
- func MapFromStringExt(s string) (map[string]any, error)
- func MapIsSubset(subset, superset any) bool
- func MapIsSubsetOfStruct(m map[string]any, s any) bool
- func MapMerge[T ~map[string]any](maps ...T) T
- func MapOmit[K comparable, V any](m map[K]V, keys ...K) map[K]V
- func MapPick[K comparable, V any](m map[K]V, keys ...K) map[K]V
- func MapToStruct(m map[string]any, o any) error
- func MathMax[T ...](a, b T) T
- func MathMin[T ...](a, b T) T
- func Set(data map[string]interface{}, path string, value interface{})
- func StringBool(s string) bool
- func StringIsBlank(s string) bool
- func StringIsNotBlank(s string) bool
- func StringMissing(a, b string) string
- func StringNormalize(s string) string
- func StringTitle(s string) string
- func StructCopyMatching(source, target any)
- func StructHasBsonName(s any, targetName string) bool
- func StructHasJsonName(s any, targetName string) bool
- func StructHasNotStringValue(o any, field string) bool
- func StructHasProperty(value interface{}, name string) bool
- func StructHasStringValue(o any, field string) bool
- func StructMustToMap(data any) map[string]any
- func StructSetValue(o any, field string, value any)
- func StructToMapRecursive(obj any) any
- func StructToStruct(source any, target any)
- func Unset(doc any, path string)
- func UnsetSimple(data map[string]interface{}, path string)
- func Validate(schema string, dataAny any) error
- type WorkspaceInfo
- type WrapA
Examples ¶
Constants ¶
const EscapeBlue = "\033[34m"
const EscapeCyan = "\033[36m"
const EscapeDim = "\033[2m"
const EscapeGreen = "\033[32m"
const EscapeMagenta = "\033[35m"
const EscapeRed = "\033[31m"
const EscapeReset = "\033[0m"
const EscapeYellow = "\033[33m"
Variables ¶
This section is empty.
Functions ¶
func AssertDeepEqual ¶
AssertDeepEqual compares equality by comparing bytes.
func BoolString ¶
func BsonAToSlice ¶
BsonAToSlice will convert a bson.A to []map[string]any.
func BsonAnyToMap ¶
func BsonDFromSlice ¶
func FileDeepScan ¶
FileDeepScan will scan dir recursively and append any files found to files.
func GetGenericWithDefault ¶
func GetWithDefault ¶
GetWithDefault
func JsonPrintError ¶
func JsonSprintError ¶
JsonSprintError returns the Marshal/Unmarshal error with some helpful information.
func MapIsSubset ¶
MapIsSubset given two map[string]any m1 and m2 will determine if m1 is a subset of m2. Only fields' name is evaluated not their values.
func MapIsSubsetOfStruct ¶
MapIsSubsetOfStruct given a map[string]any and a struct will determine if the map is a subset of the struct. Only fields' name is evaluated not their values.
func MapOmit ¶
func MapOmit[K comparable, V any](m map[K]V, keys ...K) map[K]V
func MapPick ¶
func MapPick[K comparable, V any](m map[K]V, keys ...K) map[K]V
func MapToStruct ¶
MapToStruct will convert a map[string]any to a struct.
func MathMax ¶
func MathMax[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string](a, b T) T
MathMax will compare two integers and return the one with the smaller value.
func MathMin ¶
func MathMin[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string](a, b T) T
MathMin will compare two integers and return the one with the smaller value.
Example ¶
fmt.Printf("%d", MathMin[int64](int64(10), int64(20)))
Output: 10
func StringBool ¶
func StringIsBlank ¶
StringIsBlank checks if a string is blank. Will trim the string before the check takes place.
func StringIsNotBlank ¶
StringIsNotBlank check if a string is NOT blank.
func StringMissing ¶
func StringNormalize ¶
StringNormalize will replace arbitrary lengths of consecutive white space with a single one.
func StringTitle ¶
func StructCopyMatching ¶
func StructCopyMatching(source, target any)
StructCopyMatching copies the fields of one struct to another only if they have the same name and type.
func StructHasBsonName ¶
StructHasJsonName determines if a struct has the given bson tag name.
func StructHasJsonName ¶
StructHasJsonName determines if a struct has the given json tag name.
func StructHasNotStringValue ¶
StructHasNotStringValue given any struct will check if the requested property has a blank value.
func StructHasProperty ¶
StructHasProperty cheks if a struct has the requested property.
func StructHasStringValue ¶
StructHasStringValue given any struct will check if the requested property has a non-blank value.
func StructMustToMap ¶
StructMustTomap given any struct return the equivalent map[string]any or nil. Will never throw. Will also work for map[string]any.
func StructSetValue ¶
StructSetValue will update the value of the given field of struct o.
func StructToMapRecursive ¶
StructToMapRecursive given a struct or a primitive will return the equivalent map[string]any of the struct or the primitive as is.
func StructToStruct ¶
func UnsetSimple ¶
Types ¶
type WorkspaceInfo ¶
type WorkspaceInfo struct {
ParentDir string
}
func FileWorkspaceInfo ¶
func FileWorkspaceInfo(file string) *WorkspaceInfo
FileWorkspaceInfo given a file will return relevant information about its location, such as its parent directory.