tox

package module
v1.2.16 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 11 Imported by: 6

README

Go Report Card

tox

tox is a simple library for rapid (and potentially lossy) type conversions. It is very helpful when trying to do quick conversions of interfaces when confidence in the quality of the value is high.

Installation

Checkout the repository and run:

go get github.com/tartabit/tox

Usage

import "github.com/tartabit/tox"

var myint = 5
var mystring = tox.ToString(myint)

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Documentation

Index

Constants

View Source
const (
	BlankString = "(blank)"
	Null        = "(null)"
)

Variables

This section is empty.

Functions

func CapitalizeString added in v1.1.9

func CapitalizeString(s string) string

func Deepcopy added in v1.1.0

func Deepcopy[T any](x T) (T, error)

Deepcopy makes a deep copy of whatever gets passed in. It handles pretty much all known Go types (with the exception of channels, unsafe pointers, and functions). Note that this is a truly deep copy that will work it's way all the way to the leaves of the types--any pointer will be copied, any values in any slice or map will be deep copied, etc. Note: in order to avoid an infinite loop, we keep track of any pointers that we've run across. If we run into that pointer again, we don't make another deep copy of it; we just replace it with the copy we've already made. This also ensures that the cloned result is functionally equivalent to the original value.

func FlattenMap added in v1.0.17

func FlattenMap(m map[string]any, delim string) map[string]any

func IsArray added in v1.2.8

func IsArray(i any) bool

func IsNumber added in v1.1.5

func IsNumber(i any) bool

func IsString added in v1.1.5

func IsString(i any) bool

func MapKeysToArray added in v1.1.2

func MapKeysToArray(m any) []any

func ToBool

func ToBool(v interface{}) bool

ToBool converts any data type to a bool, if the conversion fails, it returns false.

func ToBoolPtr added in v1.0.8

func ToBoolPtr(v interface{}) *bool

func ToByte added in v1.1.4

func ToByte(v interface{}) byte

func ToByteArray

func ToByteArray(v interface{}) []byte

ToByteArray converts bool, string, or byte arrays, if the conversion fails, it returns false.

func ToFloat64

func ToFloat64(v interface{}) float64

ToFloat64 converts any data type to a float64, if the conversion fails, it returns NaN.

func ToFloat64Ptr added in v1.0.8

func ToFloat64Ptr(v interface{}) *float64

func ToInt

func ToInt(v interface{}) int

ToInt converts any data type to a bool, if the conversion fails, it returns 0.

func ToInt64 added in v1.0.10

func ToInt64(v interface{}) int64

ToInt converts any data type to a int64, if the conversion fails, it returns 0.

func ToInt64Ptr added in v1.0.10

func ToInt64Ptr(v interface{}) *int64

func ToIntArray added in v1.2.9

func ToIntArray(v interface{}) []int

func ToIntPtr added in v1.0.8

func ToIntPtr(v interface{}) *int

func ToJson

func ToJson(v interface{}) string

func ToMapStringInterface added in v1.0.14

func ToMapStringInterface(v interface{}) map[string]interface{}

func ToMapStringString

func ToMapStringString(v interface{}) map[string]string

ToMapStringString converts a generic map[string]interface{} to a map[string]string.

func ToPrettyDuration added in v1.1.10

func ToPrettyDuration(v time.Duration, format StringFormat) string

func ToPrettyJson added in v1.2.1

func ToPrettyJson(v interface{}) string

func ToPtr added in v1.2.12

func ToPtr[T any](a T) *T

func ToString

func ToString(v interface{}) string

ToString converts any data type to a string, it uses fmt.Sprintf() to convert unknown types.

func ToStringArray

func ToStringArray(v interface{}) []string

ToStringArray can convert a single string to an array, useful if interface could be a string or array of strings.

func ToStringOpts added in v1.1.10

func ToStringOpts(v interface{}, options *Options) string

func ToStringPtr added in v1.0.8

func ToStringPtr(v interface{}) *string

func ToStringPtrOpts added in v1.0.16

func ToStringPtrOpts(v interface{}, options *Options) *string

func ToTime

func ToTime(v interface{}) time.Time

ToTime converts data types to time.Time structures. 'int' or 'int64' are treated as unix time, strings are treated as RFC3330Nano timestamps. If the conversion fails, an empty time.Time{} is returned.

func ToTimePtr added in v1.0.13

func ToTimePtr(v interface{}) *time.Time

func TriBool added in v1.0.11

func TriBool(b *bool) bool

func TruncateByteArray added in v1.0.15

func TruncateByteArray(b []byte, length int) []byte

func TruncateString added in v1.0.15

func TruncateString(s string, length int) string

Types

type FieldDiff added in v1.2.2

type FieldDiff struct {
	Old any `json:"old" bson:"old"`
	New any `json:"new" bson:"new"`
}

type Object added in v1.1.0

type Object map[string]any

func NewObject added in v1.1.0

func NewObject(mi any) Object

func NewObjectFromDelimitedString added in v1.1.10

func NewObjectFromDelimitedString(source string, pattern string, delim string) (Object, error)

func (Object) Clone added in v1.1.0

func (o Object) Clone() Object

func (Object) ConvertStructs added in v1.2.4

func (o Object) ConvertStructs()

func (Object) Delete added in v1.1.0

func (o Object) Delete(key string)

func (Object) Diff added in v1.2.2

func (o Object) Diff(other Object) ObjectDiff

func (Object) Equals added in v1.2.2

func (o Object) Equals(other Object) bool

func (Object) FieldCount added in v1.1.0

func (o Object) FieldCount() int

func (Object) Flatten added in v1.2.2

func (o Object) Flatten(delim string) Object

func (Object) Get added in v1.1.0

func (o Object) Get(key string) any

func (Object) GetBool added in v1.1.0

func (o Object) GetBool(key string, def bool) bool

func (Object) GetBoolPtr added in v1.1.0

func (o Object) GetBoolPtr(key string, def bool) *bool

func (Object) GetBytes added in v1.1.0

func (o Object) GetBytes(key string, def []byte) []byte

func (Object) GetFloat64 added in v1.1.1

func (o Object) GetFloat64(key string, def float64) float64

func (Object) GetFloat64Ptr added in v1.1.1

func (o Object) GetFloat64Ptr(key string, def float64) *float64

func (Object) GetInt added in v1.1.0

func (o Object) GetInt(key string, def int) int

func (Object) GetIntPtr added in v1.1.0

func (o Object) GetIntPtr(key string, def int) *int

func (Object) GetInto added in v1.2.13

func (o Object) GetInto(key string, ret any) any

func (Object) GetObject added in v1.1.0

func (o Object) GetObject(key string) Object

func (Object) GetObjectArray added in v1.1.0

func (o Object) GetObjectArray(key string) []Object

func (Object) GetString added in v1.1.0

func (o Object) GetString(key string, def string) string

func (Object) GetStringArray added in v1.1.0

func (o Object) GetStringArray(key string, def []string) []string

func (Object) GetTime added in v1.1.0

func (o Object) GetTime(key string, def time.Time) time.Time

func (Object) GetTimePtr added in v1.1.0

func (o Object) GetTimePtr(key string, def time.Time) *time.Time

func (Object) JsonBytes added in v1.2.3

func (o Object) JsonBytes(pretty bool) []byte

func (Object) JsonString added in v1.2.3

func (o Object) JsonString(pretty bool) string

func (Object) Merge added in v1.1.0

func (o Object) Merge(other Object)

func (Object) MergeMissing added in v1.2.16

func (o Object) MergeMissing(other Object)

func (Object) Move added in v1.1.7

func (o Object) Move(from string, to string)

func (Object) RemoveNaN added in v1.2.3

func (o Object) RemoveNaN()

func (Object) Set added in v1.1.0

func (o Object) Set(key string, value any)

func (Object) SetIfNotExist added in v1.1.6

func (o Object) SetIfNotExist(key string, value any)

func (Object) SetNullIfNotExist added in v1.1.6

func (o Object) SetNullIfNotExist(key string)

func (Object) Unmarshal added in v1.1.0

func (o Object) Unmarshal(field string, raw any)

type ObjectDiff added in v1.2.2

type ObjectDiff struct {
	Same     bool                 `json:"same"               bson:"same"`
	Added    Object               `json:"added,omitempty"    bson:"added,omitempty"`
	Modified map[string]FieldDiff `json:"modified,omitempty" bson:"modified,omitempty"`
	Deleted  Object               `json:"deleted,omitempty"  bson:"deleted,omitempty"`
}

type Options added in v1.0.16

type Options struct {
	EmptyStringAsNull bool
	FloatPrecision    int
	FloatToInt        bool
}

type StringFormat added in v1.1.10

type StringFormat int
const (
	FormatShort  StringFormat = 1
	FormatMedium StringFormat = 2
	FormatLong   StringFormat = 3
)

Jump to

Keyboard shortcuts

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