converter

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolCopyPtr

func BoolCopyPtr(v bool) *bool

func BoolToInt

func BoolToInt(b bool) int

func BoolToString

func BoolToString(b bool) string

func Float32CopyPtr

func Float32CopyPtr(v float32) *float32

func Float32ToString

func Float32ToString(f float32) string

func Float64CopyPtr

func Float64CopyPtr(v float64) *float64

func Float64ToString

func Float64ToString(f float64) string

func GetOrDefault

func GetOrDefault(res interface{}, defaultValue interface{}) interface{}

func GetOrDefaultFloat64

func GetOrDefaultFloat64(i *float64) float64

func GetOrDefaultInt32

func GetOrDefaultInt32(i *int32) int32

func GetOrDefaultInt64

func GetOrDefaultInt64(i *int64) int64

func GetOrDefaultString

func GetOrDefaultString(str *string) string

func Int16CopyPtr

func Int16CopyPtr(v int16) *int16

func Int32CopyPtr

func Int32CopyPtr(v int32) *int32

func Int32ToString

func Int32ToString(i int32) string

func Int64CopyPtr

func Int64CopyPtr(v int64) *int64

func Int64PtrCopyPtr

func Int64PtrCopyPtr(v *int64) *int64

func Int64ToString

func Int64ToString(i int64) string

func Int8CopyPtr

func Int8CopyPtr(v int8) *int8

func IntCopyPtr

func IntCopyPtr(v int) *int

func IntMeanToString

func IntMeanToString(i int) string

IntMeanToString 特殊处理了0值的转换,主要是为了兼容某些下游的特殊需要,0值会转换为空字符串 其他逻辑,参考 IntToString

func IntToBool

func IntToBool(i int) bool

func IntToString

func IntToString(i int) string

IntToString int转换为string,比如1 => "1", 特别注意: 0 => "0"

func InterfaceToFloat64

func InterfaceToFloat64(inter interface{}) float64

func InterfaceToFloat64V2

func InterfaceToFloat64V2(inter interface{}) (float float64, err error)

InterfaceToFloat64V2 根据type assertion将interface转换成float

func InterfaceToInt

func InterfaceToInt(inter interface{}) int

func InterfaceToInt64

func InterfaceToInt64(inter interface{}) int64

func InterfaceToJsonNum

func InterfaceToJsonNum(inter interface{}) json.Number

func InterfaceToString

func InterfaceToString(inter interface{}) string

func JsonNumberPointerToFloat64

func JsonNumberPointerToFloat64(number *json.Number) float64

func JsonNumberPointerToInt64

func JsonNumberPointerToInt64(number *json.Number) int64

func JsonNumberPointerToString

func JsonNumberPointerToString(number *json.Number) string

func JsonNumberToInt

func JsonNumberToInt(num json.Number) (int, error)

JsonNumberToInt 将json.Number解析为int,优先用Int64解析,其次试图将float64转换为int,都失败则返回错误

func MapFieldFormatToString

func MapFieldFormatToString(value map[string]interface{}) map[string]interface{}

func MapStringToStruct

func MapStringToStruct(originalData map[string]string, result interface{}) error

MapStringToStruct 将map[string]string转换为结构体 注意result传递的必须是指针,数据将直接解析到result里,请确保result的json tag配置正确

Example :

 //原始数据
 originalData:= map[string]string{
    "name" : "bob",
    "age" : "100"
 }
//所需结构体
type Person struct{
   Name string `json:"name"`
   Age  int `json:"age,string"`
}

//调用本方法进行解析
bob := &Person{}
MapStringToStruct(originalData,bob)

//output ,bob解析完毕:
Bob(Person) { Name: "bob", Age : 100}

func PtrCopyBool

func PtrCopyBool(v *bool) bool

func PtrCopyFloat32

func PtrCopyFloat32(v *float32) float32

func PtrCopyFloat64

func PtrCopyFloat64(v *float64) float64

func PtrCopyInt

func PtrCopyInt(v *int) int

func PtrCopyInt16

func PtrCopyInt16(v *int16) int16

func PtrCopyInt32

func PtrCopyInt32(v *int32) int32

func PtrCopyInt64

func PtrCopyInt64(v *int64) int64

func PtrCopyInt8

func PtrCopyInt8(v *int8) int8

func PtrCopyString

func PtrCopyString(v *string) string

func PtrCopyUint32

func PtrCopyUint32(v *uint32) uint32

func PtrCopyUint64

func PtrCopyUint64(v *uint64) uint64

func SafeUnmarshal

func SafeUnmarshal(str []byte, ret interface{}) error

SafeUnmarshal 如果是数字,则会转化为jsonNumber

func StringCopyPtr

func StringCopyPtr(v string) *string

func StringInSlice

func StringInSlice(s string, slice []string) bool

func StringToBool

func StringToBool(s string) bool

func StringToFloat32

func StringToFloat32(s string) float32

func StringToFloat64

func StringToFloat64(s string) float64

func StringToInt

func StringToInt(s string) int

func StringToInt16

func StringToInt16(s string) int16

func StringToInt32

func StringToInt32(s string) int32

func StringToInt64

func StringToInt64(s string) int64

func StringToInt8

func StringToInt8(s string) int8

func StructToMap

func StructToMap(obj interface{}) map[string]interface{}

func StructToMapIgnoreEmpty

func StructToMapIgnoreEmpty(obj interface{}) map[string]interface{}

func StructToMapOmitEmpty

func StructToMapOmitEmpty(obj interface{}) (result map[string]interface{}, err error)

func StructToMapString

func StructToMapString(obj interface{}) (map[string]string, error)

StructToMapString 将结构体转换为map[string]string

func StructToMapStringOmitEmpty

func StructToMapStringOmitEmpty(obj interface{}) (map[string]string, error)

StructToMapStringOmitEmpty 将结构体转换为map[string]string(有omitempty的情况)

func StructToPHPTransform

func StructToPHPTransform(obj interface{}) interface{}

StructToPHPTransform 主要用途:按照php格式的结构体转换函数,主要用于json格式转换的兼容。 如果传入的是**结构体指针**不为空,那么返回结构体 如果传入的**结构体指针**是nil,那么返回空数组,用于json encode的时候,变成 "[]"

注意!! 入参必须是结构体指针,其他类型目前不支持,都将返回原本数据

Example:

//创建一个结构体
testData = person{name: "bob", age: 20 }

//传入结构体指针
result := StructToPHPTransform(&testData)

//输出结果还是这个结构体指针
//output result = *person{name: "bob", age: 20}

//但是如果传入一个空指针
result := StructToPHPTransform(nil)

//返回值是一个空结构体数组,此时进行json encode,会变成[]
//result = []struct{}

func ToString

func ToString(obj interface{}) string

func Uint32CopyPtr

func Uint32CopyPtr(v uint32) *uint32

func Uint64CopyPtr

func Uint64CopyPtr(v uint64) *uint64

func Unescape

func Unescape(str string) (string, error)

Unescape 去除json字符串中的转义字符,尤其用于端上传来的某一个参数,是一个json字符串 Example:

//1.转义json,不带双引号,无法直接进行 json.Unmarshal, 所以需要进行反转义,返回值为:
input := `{\"name\":\"suitingwei\",\"age\":13}`
//进行反转义, output = {"name":"suitingwei","age":13}
output,_ :=converter.Unescape(input)

//2.转义json,带双引号
input := `"{\"name\":\"suitingwei\",\"age\":13}"`
//进行反转义, output = {"name":"suitingwei","age":13}
output,_ :=converter.Unescape(input)

//3.不是转义的json,直接返回,主要是给一些IDL的 UnmarshalJSON方法提供便利,避免合法的json在这个函数调用后包凑
input := `{"name":"suitingwei","age":13}"`
//进行反转义, output = {"name":"suitingwei","age":13}
output,_ :=converter.Unescape(input)

注意:如果传入的不是转义后的字符串,会返回空

Types

type BoolConverter

type BoolConverter interface {
	ToBool() bool
}

type Float32Converter

type Float32Converter interface {
	ToFloat32() float32
}

type Float64Converter

type Float64Converter interface {
	ToFloat64() float64
}

type Int16Converter

type Int16Converter interface {
	ToInt16() int16
}

type Int32Converter

type Int32Converter interface {
	ToInt32() int32
}

type Int64Converter

type Int64Converter interface {
	ToInt64() int64
}

type Int8Converter

type Int8Converter interface {
	ToInt8() int8
}

type IntConverter

type IntConverter interface {
	ToInt() int
}

type StringConverter

type StringConverter interface {
	ToString() string
}

Jump to

Keyboard shortcuts

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