cvt

package module
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: MIT Imports: 9 Imported by: 5

README

cvt

PkgGoDev Go Report Card Build Status codecov GitHub Mentioned in Awesome Go

Simple, safe conversion of any type, including indirect/custom types.

Documents

https://cvt.shockerli.net

Install

Go >= 1.13

go get -u github.com/shockerli/cvt

Usage

English | 中文

with error

Method __E(): expect handle error, while unable to convert

cvt.IntE("12")          // 12, nil
cvt.Float64E("12.34")   // 12.34, nil
cvt.StringE(12.34)      // "12.34", nil
cvt.BoolE("false")      // false, nil
custom type and pointers

dereferencing pointer and reach the original type

type Name string

var name Name = "jioby"

cvt.StringE(name)       // jioby, nil
cvt.StringE(&name)      // jioby, nil
ignore error

Method __(): ignore error, while convert failed, will return the zero value of type

cvt.Int("12")           // 12(success)
cvt.Int(struct{}{})     // 0(failed)
with default

return the default value, while convert failed

cvt.Int(struct{}{}, 12)     // 12
cvt.Float("hello", 12.34)   // 12.34
more

1000+ unit test cases, for more examples, see *_test.go

License

This project is under the terms of the MIT license.

Thanks

JetBrains Logo (Main) logo

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TimeFormats = []string{
	time.RFC3339,
	time.RFC1123Z,
	time.RFC1123,
	time.RFC822Z,
	time.RFC822,
	time.RFC850,
	time.ANSIC,
	time.UnixDate,
	time.RubyDate,
	time.Kitchen,
	time.Stamp,
	time.StampMilli,
	time.StampMicro,
	time.StampNano,
	"2006-01-02T15:04:05",
	"Mon Jan 2 15:04:05 2006 -0700",
	"2006-01-02 15:04:05.999999999 -0700 MST",
	"2006-01-02",
	"02 Jan 2006",
	"20060102",
	"20060102150405",
	"2006-01-02T15:04:05-0700",
	"2006-01-02 15:04:05 -07:00",
	"2006-01-02 15:04:05 -0700",
	"2006-01-02 15:04:05Z07:00",
	"2006-01-02 15:04:05Z0700",
	"2006-01-02 15:04:05",
	"2006-01-02 15:04:05.000",
	"2006-01-02 15:04:05.000000",
	"2006-01-02 15:04:05.000000000",
	"2006.01.02",
	"2006.01.02 15:04:05",
	"2006.01.02 15:04:05.000",
	"2006.01.02 15:04:05.000000",
	"2006.01.02 15:04:05.000000000",
	"2006/01/02",
	"2006/01/02 15:04:05",
	"2006/01/02 15:04:05.000",
	"2006/01/02 15:04:05.000000",
	"2006/01/02 15:04:05.000000000",
	"2006年01月02日",
	"2006年01月02日 15:04:05",
	"2006年01月02日 15:04:05.000",
	"2006年01月02日 15:04:05.000000",
	"2006年01月02日 15:04:05.000000000",
	"2006年01月02日 15时04分05秒",
}

TimeFormats all supported time formats you can add your custom time format

View Source
var TimeLocation = time.UTC

TimeLocation default time location you can change this for global location if cvt < v0.2.7, Time() and TimeE() use time.Parse() to parse the time string, its default time.UTC since cvt >= v0.2.7, add TimeInLocation() and TimeInLocationE() support, add this variable to setting default time.Location

Functions

func Bool

func Bool(v interface{}, def ...bool) bool

Bool convert an interface to a bool type, with default value

func BoolE

func BoolE(val interface{}) (bool, error)

BoolE convert an interface to a bool type

func BoolP added in v0.2.3

func BoolP(v interface{}, def ...bool) *bool

BoolP convert and store in a new bool value, and returns a pointer to it

func ColumnsE

func ColumnsE(val interface{}, field interface{}) (sl []interface{}, err error)

ColumnsE return the values from a single column in the input array/slice/map of struct/map

func Field added in v0.2.4

func Field(v interface{}, field interface{}, def ...interface{}) interface{}

Field return the field value from map/struct, with default value

func FieldE

func FieldE(val interface{}, field interface{}) (interface{}, error)

FieldE return the field value from map/struct, ignore the field type

func Float32

func Float32(v interface{}, def ...float32) float32

Float32 convert an interface to a float32 type, with default value

func Float32E

func Float32E(val interface{}) (float32, error)

Float32E convert an interface to a float32 type

func Float32P added in v0.2.3

func Float32P(v interface{}, def ...float32) *float32

Float32P convert and store in a new float32 value, and returns a pointer to it

func Float64

func Float64(v interface{}, def ...float64) float64

Float64 convert an interface to a float64 type, with default value

func Float64E

func Float64E(val interface{}) (float64, error)

Float64E convert an interface to a float64 type

func Float64P added in v0.2.3

func Float64P(v interface{}, def ...float64) *float64

Float64P convert and store in a new float64 value, and returns a pointer to it

func Indirect

func Indirect(a interface{}) (val interface{}, rv reflect.Value)

Indirect returns the value with base type

func Int

func Int(v interface{}, def ...int) int

Int convert an interface to an int type, with default value

func Int16

func Int16(v interface{}, def ...int16) int16

Int16 convert an interface to an int16 type, with default value

func Int16E

func Int16E(val interface{}) (int16, error)

Int16E convert an interface to an int16 type

func Int16P added in v0.2.3

func Int16P(v interface{}, def ...int16) *int16

Int16P convert and store in a new int16 value, and returns a pointer to it

func Int32

func Int32(v interface{}, def ...int32) int32

Int32 convert an interface to an int32 type, with default value

func Int32E

func Int32E(val interface{}) (int32, error)

Int32E convert an interface to an int32 type

func Int32P added in v0.2.3

func Int32P(v interface{}, def ...int32) *int32

Int32P convert and store in a new int32 value, and returns a pointer to it

func Int64

func Int64(v interface{}, def ...int64) int64

Int64 convert an interface to an int64 type, with default value

func Int64E

func Int64E(val interface{}) (int64, error)

Int64E convert an interface to an int64 type

func Int64P added in v0.2.3

func Int64P(v interface{}, def ...int64) *int64

Int64P convert and store in a new int64 value, and returns a pointer to it

func Int8

func Int8(v interface{}, def ...int8) int8

Int8 convert an interface to an int8 type, with default value

func Int8E

func Int8E(val interface{}) (int8, error)

Int8E convert an interface to an int8 type

func Int8P added in v0.2.3

func Int8P(v interface{}, def ...int8) *int8

Int8P convert and store in a new int8 value, and returns a pointer to it

func IntE

func IntE(val interface{}) (int, error)

IntE convert an interface to an int type

func IntMapE added in v0.2.8

func IntMapE(val interface{}) (m map[int]interface{}, err error)

IntMapE convert an interface to `map[int]interface{}` * Support JSON string of map * Support any `map` type

func IntP added in v0.2.3

func IntP(v interface{}, def ...int) *int

IntP convert and store in a new int value, and returns a pointer to it

func IsEmpty added in v0.2.8

func IsEmpty(v interface{}) bool

IsEmpty checks value for empty state

func KeysE added in v0.1.2

func KeysE(val interface{}) (sl []interface{}, err error)

KeysE return the keys of map, sorted by asc; or fields of struct

func Len added in v0.2.8

func Len(v interface{}) int

Len return size of string, slice, array or map

func Slice

func Slice(v interface{}, def ...[]interface{}) []interface{}

Slice convert an interface to a []interface{} type, with default value

func SliceE

func SliceE(val interface{}) (sl []interface{}, err error)

SliceE convert an interface to a []interface{} type

func SliceFloat64 added in v0.2.4

func SliceFloat64(v interface{}, def ...[]float64) []float64

SliceFloat64 convert an interface to a []float64 type, with default value

func SliceFloat64E added in v0.1.2

func SliceFloat64E(val interface{}) (sl []float64, err error)

SliceFloat64E convert an interface to a []float64 type

func SliceInt added in v0.2.4

func SliceInt(v interface{}, def ...[]int) []int

SliceInt convert an interface to a []int type, with default value

func SliceInt64 added in v0.2.4

func SliceInt64(v interface{}, def ...[]int64) []int64

SliceInt64 convert an interface to a []int64 type, with default value

func SliceInt64E added in v0.1.2

func SliceInt64E(val interface{}) (sl []int64, err error)

SliceInt64E convert an interface to a []int64 type

func SliceIntE added in v0.1.2

func SliceIntE(val interface{}) (sl []int, err error)

SliceIntE convert an interface to a []int type

func SliceString added in v0.2.4

func SliceString(v interface{}, def ...[]string) []string

SliceString convert an interface to a []string type, with default value

func SliceStringE added in v0.1.2

func SliceStringE(val interface{}) (sl []string, err error)

SliceStringE convert an interface to a []string type

func String

func String(v interface{}, def ...string) string

String convert an interface to a string type, with default value

func StringE

func StringE(val interface{}) (string, error)

StringE convert an interface to a string type

func StringMapE added in v0.2.1

func StringMapE(val interface{}) (m map[string]interface{}, err error)

StringMapE convert an interface to `map[string]interface{}` * Support JSON string of map * Support any `map` type * Support any `struct` type

func StringP added in v0.2.3

func StringP(v interface{}, def ...string) *string

StringP convert and store in a new string value, and returns a pointer to it

func Time added in v0.1.1

func Time(v interface{}, def ...time.Time) time.Time

Time convert an interface to a time.Time type, with default value

func TimeE added in v0.1.1

func TimeE(val interface{}) (t time.Time, err error)

TimeE convert an interface to a time.Time type

func TimeInLocation added in v0.2.7

func TimeInLocation(v interface{}, loc *time.Location, def ...time.Time) time.Time

TimeInLocation convert an interface to a time.Time type, with time.Location, with default

func TimeInLocationE added in v0.2.7

func TimeInLocationE(val interface{}, loc *time.Location) (t time.Time, err error)

TimeInLocationE convert an interface to a time.Time type, with time.Location, with error

func Uint

func Uint(v interface{}, def ...uint) uint

Uint convert an interface to an uint type, with default value

func Uint16

func Uint16(v interface{}, def ...uint16) uint16

Uint16 convert an interface to an uint16 type, with default value

func Uint16E

func Uint16E(val interface{}) (uint16, error)

Uint16E convert an interface to an uint16 type

func Uint16P added in v0.2.3

func Uint16P(v interface{}, def ...uint16) *uint16

Uint16P convert and store in a new uint16 value, and returns a pointer to it

func Uint32

func Uint32(v interface{}, def ...uint32) uint32

Uint32 convert an interface to an uint32 type, with default value

func Uint32E

func Uint32E(val interface{}) (uint32, error)

Uint32E convert an interface to an uint32 type

func Uint32P added in v0.2.3

func Uint32P(v interface{}, def ...uint32) *uint32

Uint32P convert and store in a new uint32 value, and returns a pointer to it

func Uint64

func Uint64(v interface{}, def ...uint64) uint64

Uint64 convert an interface to an uint64 type, with default value

func Uint64E

func Uint64E(val interface{}) (uint64, error)

Uint64E convert an interface to an uint64 type

func Uint64P added in v0.2.3

func Uint64P(v interface{}, def ...uint64) *uint64

Uint64P convert and store in a new uint64 value, and returns a pointer to it

func Uint8

func Uint8(v interface{}, def ...uint8) uint8

Uint8 convert an interface to an uint8 type, with default value

func Uint8E

func Uint8E(val interface{}) (uint8, error)

Uint8E convert an interface to an uint8 type

func Uint8P added in v0.2.3

func Uint8P(v interface{}, def ...uint8) *uint8

Uint8P convert and store in a new uint8 value, and returns a pointer to it

func UintE

func UintE(val interface{}) (uint, error)

UintE convert an interface to an uint type

func UintP added in v0.2.3

func UintP(v interface{}, def ...uint) *uint

UintP convert and store in a new uint value, and returns a pointer to it

Types

This section is empty.

Jump to

Keyboard shortcuts

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