convertor

package
v0.0.0-...-e50112c Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: BSD-3-Clause Imports: 10 Imported by: 0

README

类型转换

  • 用于将 interface 对象转化为 bool|int|float|string 等类型。
  • 在对象与字符串的转换过程中会判断对象上是否实现了 StringMarshal、fmt.Stringer 接口,如果都没有则使用 JSON 格式进行序列化和反序列化。
  • 判断字符串值的类型 GetStrValueType
    • "123" 会判断出这是个 Int
    • "123.456" 会判断是个浮点数
    • "abc" 会判断是个字符串
    • "{"a":123}" 会判断是个 Map
    • "[1,2,3]"、"[{"a":123}]" 会判断是个数组

Documentation

Overview

------------------------------------------------------------------------------ 类型转换函数集 ------------------------------------------------------------------------------

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolToFloat

func BoolToFloat(v bool) float64

func BoolToInt

func BoolToInt(v bool) int

func IsEmpty

func IsEmpty(a interface{}) bool

判断变量是否为空(nil、0、""、长度为 0 的 Slice 或 Map)

func IsEmpty2

func IsEmpty2(a interface{}, falseAsEmpty ...bool) bool

判断变量是否为空(nil、0、""、长度为 0 的 Slice 或 Map)

falseAsEmpty: 是否把 false 当作空值。默认为 true。

func MustStrToStringMap

func MustStrToStringMap(str string) map[string]string

func SetDefaultJsonApi

func SetDefaultJsonApi(api JsonAPI)

func StrToBool

func StrToBool(s string) (bool, error)

func StrToBoolNoError

func StrToBoolNoError(s string) bool

func StrToFloat

func StrToFloat(s string) (float64, error)

func StrToFloatNoError

func StrToFloatNoError(s string) float64

func StrToInt

func StrToInt(s string) (int, error)

func StrToInt32

func StrToInt32(s string) (int32, error)

func StrToInt32NoError

func StrToInt32NoError(s string) int32

func StrToInt64

func StrToInt64(s string) (int64, error)

func StrToInt64NoError

func StrToInt64NoError(s string) int64

func StrToIntNoError

func StrToIntNoError(s string) int

func StrToStringMap

func StrToStringMap(str string) (map[string]string, error)

func StrToStringObjectMap

func StrToStringObjectMap(str string) (map[string]interface{}, error)

func StrToUint

func StrToUint(s string) (uint, error)

func StrToUint32

func StrToUint32(s string) (uint32, error)

func StrToUint32NoError

func StrToUint32NoError(s string) uint32

func StrToUint64

func StrToUint64(s string) (uint64, error)

func StrToUint64NoError

func StrToUint64NoError(s string) uint64

func StrToUintNoError

func StrToUintNoError(s string) uint

func ToBool

func ToBool(a interface{}) (bool, error)

func ToBoolNoError

func ToBoolNoError(a interface{}) bool

func ToError

func ToError(a interface{}) (err error)

func ToFloat

func ToFloat(a interface{}) (float64, error)

func ToFloat32

func ToFloat32(a interface{}) (float32, error)

func ToFloat32NoError

func ToFloat32NoError(a interface{}) float32

func ToFloatNoError

func ToFloatNoError(a interface{}) float64

func ToInt

func ToInt(a interface{}) (int, error)

func ToInt32

func ToInt32(a interface{}) (int32, error)

func ToInt32NoError

func ToInt32NoError(a interface{}) int32

func ToInt64

func ToInt64(a interface{}) (int64, error)

func ToInt64NoError

func ToInt64NoError(a interface{}) int64

func ToIntNoError

func ToIntNoError(a interface{}) int

func ToString

func ToString(a interface{}, api ...JsonAPI) (string, error)

func ToStringMap

func ToStringMap(a interface{}, api ...JsonAPI) (map[string]string, error)

func ToStringMapNoError

func ToStringMapNoError(a interface{}, api ...JsonAPI) map[string]string

func ToStringNoError

func ToStringNoError(a interface{}, api ...JsonAPI) string

func ToStringObjectMap

func ToStringObjectMap(a interface{}, api ...JsonAPI) (map[string]interface{}, error)

func ToStringObjectMapNoError

func ToStringObjectMapNoError(a interface{}, api ...JsonAPI) map[string]interface{}

func ToUint

func ToUint(a interface{}) (uint, error)

func ToUint32

func ToUint32(a interface{}) (uint32, error)

func ToUint32NoError

func ToUint32NoError(a interface{}) uint32

func ToUint64

func ToUint64(a interface{}) (uint64, error)

func ToUint64NoError

func ToUint64NoError(a interface{}) uint64

func ToUintNoError

func ToUintNoError(a interface{}) uint

Types

type BasicType

type BasicType int

BasicType 简化版的反射类型

const (
	BasicType_Others  BasicType = -1
	BasicType_Invalid BasicType = 0
	BasicType_Nil     BasicType = 1
	BasicType_Bool    BasicType = 2
	BasicType_Int     BasicType = 3
	BasicType_Uint    BasicType = 4
	BasicType_Float   BasicType = 5
	BasicType_String  BasicType = 6
	BasicType_Slice   BasicType = 7
	BasicType_Map     BasicType = 8
	BasicType_Struct  BasicType = 9
)

func GetBasicType

func GetBasicType(a interface{}, parseStrValue ...bool) (BasicType, reflect.Type, reflect.Value)

获取变量的基本类型

parseStrValue: 如果 a 是字符串,是否继续解析字符串内容的值类型(使用了 GetStrValueType 方法)。例:
               GetBasicType("123", true) 将返回 BasicType_Int、GetBasicType("true", true) 将返回 BasicType_Bool。

func GetBasicType0

func GetBasicType0(a interface{}, parseStrValue ...bool) BasicType

func GetStrValueType

func GetStrValueType(str string) (BasicType, reflect.Type, reflect.Value, interface{})

获取字符串值的格式。

BasicType: 返回字符串可能的基础类型。可能是以下值:
   String:  普通字符串
   Float:   带有小数点的数字
   Int:     负整数、或小于等于 MaxInt64 的非负整数
   Uint:    非负且大于 MaxInt64 的整数
   Bool:    "bool" 或者 "false"
   Nil:     "null"
   Slice:   JSON 数组
   Map:     JSON 对象

func ReflectTypeToBasicType

func ReflectTypeToBasicType(refType reflect.Type) BasicType

func (BasicType) GetValue

func (this BasicType) GetValue(val reflect.Value) interface{}

func (BasicType) String

func (this BasicType) String() string

type ConvertToString

type ConvertToString interface {
	ToString() (str string, err error)
}

type JsonAPI

type JsonAPI interface {
	MarshalToString(v interface{}) (string, error)
	UnmarshalFromString(str string, v interface{}) error
}

Jump to

Keyboard shortcuts

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