commons

package
v0.0.0-...-f47afbd Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Commons includes utility functions used across projects.

Index

Examples

Constants

View Source
const EscapeBlue = "\033[34m"
View Source
const EscapeCyan = "\033[36m"
View Source
const EscapeDim = "\033[2m"
View Source
const EscapeGreen = "\033[32m"
View Source
const EscapeMagenta = "\033[35m"
View Source
const EscapeRed = "\033[31m"
View Source
const EscapeReset = "\033[0m"
View Source
const EscapeYellow = "\033[33m"

Variables

This section is empty.

Functions

func AssertDeepEqual

func AssertDeepEqual(t *testing.T, expected, actual any)

AssertDeepEqual compares equality by comparing bytes.

func BoolString

func BoolString(b bool) string

func BsonAToSlice

func BsonAToSlice(m any) ([]map[string]any, error)

BsonAToSlice will convert a bson.A to []map[string]any.

func BsonAnyToMap

func BsonAnyToMap(v any) any

func BsonClone

func BsonClone(m map[string]any) map[string]any

func BsonDFromSlice

func BsonDFromSlice(in [][]any) bson.D

func BsonDGet

func BsonDGet[T any](d bson.D, key string) (T, bool)

func BsonDGetAny

func BsonDGetAny(d bson.D, key string) (any, bool)

func BsonDGetString

func BsonDGetString(d bson.D, key string) (string, bool)

func BsonToMap

func BsonToMap(b bson.M) map[string]any

func BsonToStruct

func BsonToStruct(m bson.M, o any) error

func ContextFromMap

func ContextFromMap(m map[string]any) context.Context

func ContextSerialize

func ContextSerialize(ctx context.Context, fields []string) map[string]any

func FileDeepScan

func FileDeepScan(dir string, files *[]string)

FileDeepScan will scan dir recursively and append any files found to files.

func Get

func Get(m map[string]any, path string) any

func GetGeneric

func GetGeneric[T any](m map[string]any, path string) (T, bool)

func GetGenericWithDefault

func GetGenericWithDefault[T any](m map[string]any, path string, def T) T

func GetString

func GetString(m map[string]any, path string) string

func GetStringWithDefault

func GetStringWithDefault(m map[string]any, path, def string) string

func GetWithDefault

func GetWithDefault(m map[string]any, path string, def any) any

GetWithDefault

func JsonEqual

func JsonEqual(a, b string) bool

JsonEqual check if two json string representations are equal.

func JsonPrint

func JsonPrint(a any)

func JsonPrintError

func JsonPrintError(err error, content []byte)

func JsonSprint

func JsonSprint(a any) string

JsonPrint bson.M

func JsonSprintError

func JsonSprintError(err error, content []byte) (error, string)

JsonSprintError returns the Marshal/Unmarshal error with some helpful information.

func JsonToMap

func JsonToMap(s string) (map[string]any, error)

func MapFromString

func MapFromString(s string) (map[string]any, error)

func MapFromStringExt

func MapFromStringExt(s string) (map[string]any, error)

func MapIsSubset

func MapIsSubset(subset, superset any) bool

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

func MapIsSubsetOfStruct(m map[string]any, s any) bool

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 MapMerge

func MapMerge[T ~map[string]any](maps ...T) T

MapMerge merges maps into one.

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

func MapToStruct(m map[string]any, o any) error

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 Set

func Set(data map[string]interface{}, path string, value interface{})

func StringBool

func StringBool(s string) bool

func StringIsBlank

func StringIsBlank(s string) bool

StringIsBlank checks if a string is blank. Will trim the string before the check takes place.

func StringIsNotBlank

func StringIsNotBlank(s string) bool

StringIsNotBlank check if a string is NOT blank.

func StringMissing

func StringMissing(a, b string) string

func StringNormalize

func StringNormalize(s string) string

StringNormalize will replace arbitrary lengths of consecutive white space with a single one.

func StringTitle

func StringTitle(s string) string

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

func StructHasBsonName(s any, targetName string) bool

StructHasJsonName determines if a struct has the given bson tag name.

func StructHasJsonName

func StructHasJsonName(s any, targetName string) bool

StructHasJsonName determines if a struct has the given json tag name.

func StructHasNotStringValue

func StructHasNotStringValue(o any, field string) bool

StructHasNotStringValue given any struct will check if the requested property has a blank value.

func StructHasProperty

func StructHasProperty(value interface{}, name string) bool

StructHasProperty cheks if a struct has the requested property.

func StructHasStringValue

func StructHasStringValue(o any, field string) bool

StructHasStringValue given any struct will check if the requested property has a non-blank value.

func StructMustToMap

func StructMustToMap(data any) map[string]any

StructMustTomap given any struct return the equivalent map[string]any or nil. Will never throw. Will also work for map[string]any.

func StructSetValue

func StructSetValue(o any, field string, value any)

StructSetValue will update the value of the given field of struct o.

func StructToMapRecursive

func StructToMapRecursive(obj any) any

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 StructToStruct(source any, target any)

func Unset

func Unset(doc any, path string)

func UnsetSimple

func UnsetSimple(data map[string]interface{}, path string)

func Validate

func Validate(schema string, dataAny any) error

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.

type WrapA

type WrapA struct {
	Items []map[string]any
}

Jump to

Keyboard shortcuts

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