convert

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// DefaultLayout24h default 24h layout
	DefaultLayout24h = "yyyy-MM-dd HH:mm:ss"
	// DefaultLayout12h default 12h layout
	DefaultLayout12h = "yyyy-MM-dd hh:mm:ss"
)

Variables

This section is empty.

Functions

func AdjustToEndOfDay

func AdjustToEndOfDay(value any) (int64, error)

AdjustToEndOfDay adjusts the given time to the end of the day (23:59:59)

func ConvertValue

func ConvertValue(typeStr string, value string) (any, error)

ConvertValue converts a string value to the corresponding type based on the type string Supported types: string, number, boolean, object, array

func Float64ToString

func Float64ToString(n float64) string

Float64ToString converts a float64 to a string

func FormatTime

func FormatTime(t *time.Time, layout ...string) *string

FormatTime formats time to string with optional layout

func FromJSON

func FromJSON(s string, v any) error

FromJSON deserializes JSON string to specified object

func FromJSONMap

func FromJSONMap(m map[string]any, v any) error

FromJSONMap deserializes map[string]any to specified object

func FromStringArray

func FromStringArray(arr []string, targetType string) (any, error)

FromStringArray converts a string array to an array of a specific type The conversion is based on the target type

func FromSyncMap

func FromSyncMap[T any](sm *sync.Map) map[string]T

FromSyncMap takes a sync.Map and converts it into a regular map[string]T The function iterates over the sync.Map and retrieves each key-value pair, returning them as a standard Go map

func Int64ToInt

func Int64ToInt(n int64) (int, error)

Int64ToInt converts an int64 to an int

func Int64ToString

func Int64ToString(n int64) string

Int64ToString converts an int64 to a string

func IntToInt64

func IntToInt64(n int) int64

IntToInt64 converts an int to an int64

func IntToString

func IntToString(n int) string

IntToString converts an int to a string

func IsValidType

func IsValidType(typeStr string) bool

IsValidType checks if the provided type string is supported Returns true for valid types: string, number, boolean, object, array

func JSONUnmarshal

func JSONUnmarshal(s string, v any) bool

JSONUnmarshal unmarshals JSON string to specified object, returns false on error

func Max

func Max(a, b int) int

Max returns the larger of two integers

func Min

func Min(a, b int) int

Min returns the smaller of two integers

func MustFromJSON

func MustFromJSON(s string, v any)

MustFromJSON deserializes JSON string to specified object, panics on error

func MustToJSON

func MustToJSON(v any) string

MustToJSON serializes any object to JSON string, panics on error

func ParseJSON

func ParseJSON(s string, v any) bool

ParseJSON parses JSON string to specified object, returns false on error

func ParseLocalTime

func ParseLocalTime(str string) (t time.Time, err error)

ParseLocalTime parses string to local time by trying multiple formats

func ParseTimePtr

func ParseTimePtr(timeStr string) *time.Time

ParseTimePtr parses a time string into a *time.Time using multiple formats

func PrettyJSON

func PrettyJSON(v any) (string, error)

PrettyJSON formats object as indented JSON string

func PtrToPBTimestamp

func PtrToPBTimestamp(t *time.Time) *timestamppb.Timestamp

PtrToPBTimestamp converts *time.Time to *timestamppb.Timestamp

func StringToFloat64

func StringToFloat64(s string) (float64, error)

StringToFloat64 converts a string to a float64

func StringToInt

func StringToInt(s string) (int, error)

StringToInt converts a string to an int

func StringToInt32

func StringToInt32(s string) (int32, error)

StringToInt32 converts a string to an int32

func StringToInt64

func StringToInt64(s string) (int64, error)

StringToInt64 converts a string to an int64

func StringifyJSON

func StringifyJSON(v any) string

StringifyJSON converts object to JSON string, returns empty string on error

func ToArray

func ToArray(value any) ([]any, error)

ToArray attempts to convert a value to []interface{} Supports conversion from string (JSON) and existing arrays

func ToBool

func ToBool(value any) (bool, error)

ToBool attempts to convert a value to a boolean Supports conversion from string, number types, and bool For strings, accepts "true", "1", "yes", "on" as true values and "false", "0", "no", "off", "" as false values

func ToFloat

func ToFloat(value any) (float64, error)

ToFloat attempts to convert a value to a float64 Supports conversion from string, integer types, and bool

func ToInt

func ToInt(value any) (int64, error)

ToInt attempts to convert a value to an integer Supports conversion from string, float64, bool, and integer types

func ToJSON

func ToJSON(v any) (string, error)

ToJSON serializes any object to JSON string

func ToJSONMap

func ToJSONMap(v any) (map[string]any, error)

ToJSONMap serializes any object to map[string]any

func ToObject

func ToObject(value any) (map[string]any, error)

ToObject attempts to convert a value to a map[string]interface{} Supports conversion from string (JSON) and existing maps

func ToPBTimestamp

func ToPBTimestamp(t time.Time) *timestamppb.Timestamp

ToPBTimestamp converts time.Time to protobuf Timestamp

func ToPointer

func ToPointer[T any](v T) *T

ToPointer returns a pointer to a value of any type

func ToString

func ToString(value any) string

ToString converts any value to its string representation Returns empty string for nil values

func ToStringArray

func ToStringArray(arr any) []string

ToStringArray converts any type of array to a string array Each element is converted to its string representation

func ToSyncMap

func ToSyncMap[T any](m map[string]T) *sync.Map

ToSyncMap takes a regular map[string]T and returns a pointer to a sync.Map The function copies all the key-value pairs from the regular map into the sync.Map

func ToTypedArray

func ToTypedArray[T any](arr []string) ([]T, error)

ToTypedArray is a generic function that converts a string array to an array of type T It handles common types (string, int, int64, float64, bool)

func ToUnixMilli

func ToUnixMilli(v any) int64

ToUnixMilli converts various types to unix milliseconds timestamp

func ToValue

func ToValue[T any](v *T) T

ToValue returns the value pointed to by a pointer

func UnixMilliToString

func UnixMilliToString(t *int64, layout ...string) *string

UnixMilliToString converts unix milliseconds timestamp to string

func UnixMilliToTime

func UnixMilliToTime(i *int64) *time.Time

UnixMilliToTime converts unix milliseconds timestamp to time.Time

func UnixSecToTime

func UnixSecToTime(sec int64) time.Time

UnixSecToTime converts unix seconds to time.Time

Types

This section is empty.

Jump to

Keyboard shortcuts

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