object

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: MIT Imports: 7 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	THOUSAND_SEPARATOR byte = ','
	DECIMAL_SEPARATOR  byte = '.'
)

Use var so later we can change it, it is crude for now but can do: object.THOUSAND_SEPARATOR='.' before calling any of the number methods

View Source
var (
	DEBUG_KEY_SEPARATOR string = ": "
)

Functions

func Abs

func Abs[T Number](num T) T

Simpler generic ABS func, no need to import math. Need Number Type

func ArrayAContainsBString

func ArrayAContainsBString(a []string, b string) bool

String Array A contains item (string) B NOTE: maybe change to generics?

func Combine

func Combine(msgs ...any) interface{}

Trick to combine multiple any/interface variables into 1 interface NOTE: NOT USED

func CombineToStringWithField

func CombineToStringWithField(msgs ...any) string

combined key-value (json usually) into string using marshal NOT USED

func CombineToStringWithSeparator

func CombineToStringWithSeparator(sep string, msgs ...any) string

Combine array of strings with seperator (sep)

func CombineToStringWithSpace

func CombineToStringWithSpace(msgs ...any) string

Combined with space

func CombineTwoMaps

func CombineTwoMaps(a, b map[string]interface{})

Combines map A,B into A without checking if A already have the key or not, just overwrite

func CombineTwoMapsDistinct

func CombineTwoMapsDistinct(bwins bool, a, b map[string]interface{})

If bwins is set means b will overwrite a for the same key

func ExistInArrayInt

func ExistInArrayInt(num int, arr []int) bool

Return true if num exists in arr

func GetDBTag

func GetDBTag(field reflect.StructField) string

func GetJSONOrDBTag

func GetJSONOrDBTag(field reflect.StructField) string

Get json or db tag from a struct. First try to get JSON tag, if not exist get the DB tag. If not exist, return empty string.

func GetJSONTag

func GetJSONTag(field reflect.StructField) string

Get json only tag First try to get JSON tag, if not exist get the DB tag. If not exist, return empty string.

func GetType

func GetType(a interface{}, removeDots bool) string

Get Struct Type (or any types, but mostly using this to get what type of struct) in generics functions removeDots will remove the filename.[structName] and return only structName

func Int

func Int(smallNumber string, abs bool) int

Faster Atoi, only for string number that is less than 10 digits. No error checking, if there is error, then return 0. Skipping the decimals (anything after . like: 123.123 ==> 123) Ignoring the thousand separator abs = true, return only absolute value. NOTE: only works on round numbers (integer) cannot be decimals

func IntPlus

func IntPlus(str string, zeroValue int) int

PLEASE NOTE TO USE THIS ONLY IF YOU ARE SURE THAT THE STRING IS INTEGER Mostly only to convert simple string index back to positive integer NOTE: only works on decimals and POSITIVE value, so string "-123" will not work

func LastStringAfterDelimiter

func LastStringAfterDelimiter(s, d string) string

Return the last string s after delimiter d, example: s='this;is;delimited;by;semicolon' and d=';' then this function return "semicolon"

func MapToString

func MapToString(d map[string]interface{}) string

NOTE: Not USED!

func MapToStringSlice

func MapToStringSlice(d map[string]interface{}) []string

NOTE: For debugging usually Use this to log/print map into key: value array of string

func MapToStruct

func MapToStruct[T any](dict map[string]interface{}) T

This can be written using reflect and loop through the fields and json tag. But marshall works as well

func MapToStructSlow

func MapToStructSlow[T any](dict map[string]interface{}) T

MapToStruct that convert map to struct with key defined in `json` tag

func MapToStructSlowDB

func MapToStructSlowDB[T any](dict map[string]interface{}) T

MapToStruct that convert map to struct with key defined in `json` tag

func MapToStructSlowJSON

func MapToStructSlowJSON[T any](dict map[string]interface{}) T

MapToStruct that convert map to struct with key defined in `json` tag

func MaxMinIntArray

func MaxMinIntArray(arr []int) (max, min, maxindex, minindex int)

Return the maximum, Minimum values and index in the array of integer

func StringContainsSplitBySpace

func StringContainsSplitBySpace(str, rtype, separator string) bool

str is multiple value separated by space, ie: str="token id_token" cannot use contain because rtype="token" might hit id_token as well

func StringSliceDebug

func StringSliceDebug(msgs []string) string

NOTE: Not USED! Use this to log/print array of string combined with DEBUG_SEPARATOR

func StructToMap

func StructToMap[T any](input T) map[string]interface{}

StructToMap converts a struct to a map using generics with JSON or DB tags and default options

func StructToMapDB

func StructToMapDB[T any](input T) map[string]interface{}

StructToMapDB converts a struct to a map with DB tags and default options

func StructToMapDBWithOptions

func StructToMapDBWithOptions[T any](input T, opts MapOptions) map[string]interface{}

StructToMapDBWithOptions converts a struct to a map with DB tags and custom options

func StructToMapJSON

func StructToMapJSON[T any](input T) map[string]interface{}

StructToMapJSON converts a struct to a map with JSON tags and default options

func StructToMapJSONWithOptions

func StructToMapJSONWithOptions[T any](input T, opts MapOptions) map[string]interface{}

StructToMapJSONWithOptions converts a struct to a map with JSON tags and custom options

func StructToMapWithOptions

func StructToMapWithOptions[T any](input T, opts MapOptions) map[string]interface{}

StructToMapWithOptions converts a struct to a map using generics with JSON or DB tags and custom options

Types

type Float

type Float interface {
	~float32 | ~float64
}

type Integer

type Integer interface {
	Signed | Unsigned
}

type MapOptions

type MapOptions struct {
	SkipZeroNumbers  bool   // Skip integers/floats that are 0
	SkipEmptyStrings bool   // Skip strings that are empty
	SkipFalseBools   bool   // Skip booleans that are false
	SkipZeroTimes    bool   // Skip time.Time that are zero (Jan 1, 0001 UTC)
	SkipEmptySlices  bool   // Skip empty slices and arrays
	SkipEmptyMaps    bool   // Skip empty maps
	SkipNilPointers  bool   // Skip nil pointers (always true, included for completeness)
	TimeFormat       string // Format to use for time.Time values (empty string uses time.RFC3339)
}

MapOptions defines options for structure to map conversion

func DefaultNoSkipMapOptions

func DefaultNoSkipMapOptions() MapOptions

Can also use this for non-skipping the zero and all, usually we want to set 0 to the integer like saving in DB, so usage would be:

resultMap := StructToMapDBWithOptions[StructName](struct_object, DefaultNoSkipMapOptions())

DefaultSkipMapOptions returns the default options for map conversion

func DefaultSkipMapOptions

func DefaultSkipMapOptions() MapOptions

DefaultSkipMapOptions returns the default options for map conversion

type Number

type Number interface {
	Integer | Float
}

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Define helper for number declaration/interface

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Jump to

Keyboard shortcuts

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