convert

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2021 License: MIT Imports: 19 Imported by: 2

README

Convert Utils

Cast

support

int, int8, int16, int32, int64, float32, float64, bool, string

package main
import (
    "github.com/optim-kazuhiro-seida/go-advance-type/convert"
)

func main() {
	a := convert.MustStr(120)  // Same SafeStr(120, "")
	b := convert.MustInt("120") // Same SafeInt("120", 0)
	c := convert.MustInt64([]byte("120"))  // Same SafeInt64([]byte("120"), 0)
        // var a string = "120" 
        // var b int = 120      
        // var c int64 = 120    


	d := convert.SafeInt32(struct{}{}, 21)
	e := convert.SafeFloat32([]byte(""), 1.2)
        // var d int32  = 21
        // var e float32 = 1.2


	f, err := convert.Float64("hogehoge") 
	g, err := convert.Bool("gegege")      
        // error
        // error

}

Json

Powered by json

package main
import (
    "github.com/optim-kazuhiro-seida/go-advance-type/convert"
)

func main() {
    type T1 struct {
        Test   string
        Sample string
    }
    t1 := T1{Test: "test", Sample: "sample"}
    
    jsonStr, err := IndentJson(t1) 
    //  jsonStr = "{\n  \"Test\": \"test\",\n  \"Sample\": \"sample\"\n}"
    
    jsonStr, err := CompactJson(t1)
    //  jsonStr = "{\"Test\":\"test\",\"Sample\":\"sample\"}"  
    
    err := UnMarshalJson(t1}, &hogehoge)
    err := MarshalJson(t1) 


}

Convert

package main
import (
    "github.com/optim-kazuhiro-seida/go-advance-type/convert"
)

func main() {
    var b Hoge
    var c Foga
    a := Hoge {
        a: "a",
    }

    convert.Struct2Map(a) 
    // map[string]interface{}{"jsontag_a": "a"}
    
    convert.Map2Struct(map[string]interface{}{"a": "a"}, &b) 
    // map[string]interface{}{"a": "a"}
    
    convert.StructTag2Map(a, "json")
    //  Hoge {a: "a"}
    
    convert.StructJsonTag2Map(a) 
    //  map[string]interface{}{"jsontag_a": "a"}
   

    convert.GetObjectValues(a)
    convert.GetObjectKeys(a)
    
    convert.DeepCopy(a, &b)
    convert.CopyFields(a, &c)
    convert.CopyCastFields(a, &c)
}

Exchange

package main
import (
	"github.com/optim-kazuhiro-seida/go-advance-type/convert"
)

func main() {
	ptrString := convert.StringPtr("test") // &"test"
	ptrInt := convert.IntPtr(2)            // &2
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(in interface{}) (bool, error)

func BoolPtr added in v1.0.1

func BoolPtr(b bool) *bool

func ByteSlicePtr added in v1.0.1

func ByteSlicePtr(b []byte) *[]byte

func Bytes2Float

func Bytes2Float(b []byte) float32

func Bytes2Long

func Bytes2Long(buf []byte) int64

func BytesAsULong

func BytesAsULong(b []byte) uint64

func CompactJson added in v1.1.12

func CompactJson(data interface{}) (result string, err error)

func CopyCastFields added in v1.1.3

func CopyCastFields(source, target interface{}) (err error)

func CopyFields added in v1.1.1

func CopyFields(source, target interface{}) (err error)

func DecodeBase64

func DecodeBase64(str string) []byte

func DeepClone added in v1.4.0

func DeepClone(source interface{}) (target interface{})

func DeepCopy

func DeepCopy(source, target interface{}) error

func Double2Bytes

func Double2Bytes(f float64) []byte

func Duration added in v1.1.0

func Duration(in interface{}) (time.Duration, error)

func EncodeBase64

func EncodeBase64(byts []byte) string

func Float2Bytes

func Float2Bytes(f float32) []byte

func Float32

func Float32(in interface{}) (float32, error)

func Float32Ptr added in v1.0.1

func Float32Ptr(i float32) *float32

func Float64

func Float64(in interface{}) (float64, error)

func Float64Ptr added in v1.0.1

func Float64Ptr(i float64) *float64

func GetObjectKeys added in v1.1.13

func GetObjectKeys(m interface{}) (result []string)

func GetObjectValues added in v1.1.13

func GetObjectValues(m interface{}) (result []interface{})

func IndentJson

func IndentJson(data interface{}) (result string, err error)

func Int

func Int(in interface{}) (int, error)

func Int16

func Int16(in interface{}) (int16, error)

func Int16AsUint16

func Int16AsUint16(i int16) uint16

func Int32

func Int32(in interface{}) (int32, error)

func Int32AsUint32

func Int32AsUint32(i int32) uint32

func Int32Ptr added in v1.0.1

func Int32Ptr(i int32) *int32

func Int64

func Int64(in interface{}) (int64, error)

func Int64AsUint64

func Int64AsUint64(i int64) uint64

func Int64Ptr added in v1.0.1

func Int64Ptr(i int64) *int64

func Int8

func Int8(in interface{}) (int8, error)

func IntPtr added in v1.0.1

func IntPtr(i int) *int

func Json added in v1.1.12

func Json(data interface{}) (string, error)

func Long2Bytes

func Long2Bytes(i int64) []byte

func Map2Struct added in v1.1.0

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

func MarshalJson

func MarshalJson(data interface{}) ([]byte, error)

func Milli2Nano

func Milli2Nano(milliTimestamp interface{}) string

func Milli2NanoN added in v1.4.0

func Milli2NanoN(milliTimestamp interface{}) int64

func MustBool

func MustBool(in interface{}) (result bool)

func MustCompactJson added in v1.1.12

func MustCompactJson(data interface{}) (result string)

func MustFloat32

func MustFloat32(in interface{}) (result float32)

func MustFloat64

func MustFloat64(in interface{}) (result float64)

func MustIndentJson added in v1.1.8

func MustIndentJson(data interface{}) (result string)

func MustInt

func MustInt(in interface{}) (result int)

func MustInt16 added in v1.0.1

func MustInt16(in interface{}) (result int16)

func MustInt32

func MustInt32(in interface{}) (result int32)

func MustInt64

func MustInt64(in interface{}) (result int64)

func MustInt8 added in v1.0.1

func MustInt8(in interface{}) (result int8)

func MustJson added in v1.1.12

func MustJson(data interface{}) (result string)

func MustMarshalJson added in v1.1.8

func MustMarshalJson(data interface{}) (result []byte)

func MustStr

func MustStr(in interface{}) (result string)

func MustUint added in v1.1.0

func MustUint(in interface{}) (result uint)

func MustUint16 added in v1.1.0

func MustUint16(in interface{}) (result uint16)

func MustUint32 added in v1.1.0

func MustUint32(in interface{}) (result uint32)

func MustUint64 added in v1.1.0

func MustUint64(in interface{}) (result uint64)

func MustUint8 added in v1.1.0

func MustUint8(in interface{}) (result uint8)

func Nano2Milli

func Nano2Milli(nanoTimeStamp interface{}) string

func Nano2MilliN added in v1.4.0

func Nano2MilliN(nanoTimeStamp interface{}) int64

func SafeBool added in v1.1.3

func SafeBool(in interface{}, defaultVal bool) bool

func SafeFloat32

func SafeFloat32(in interface{}, defaultNumber float32) float32

func SafeFloat64

func SafeFloat64(in interface{}, defaultNumber float64) float64

func SafeInt

func SafeInt(in interface{}, defaultNumber int) int

func SafeInt16 added in v1.0.1

func SafeInt16(in interface{}, defaultNumber int16) int16

func SafeInt32

func SafeInt32(in interface{}, defaultNumber int32) int32

func SafeInt64 added in v1.0.1

func SafeInt64(in interface{}, defaultNumber int64) int64

func SafeInt8 added in v1.0.1

func SafeInt8(in interface{}, defaultNumber int8) int8

func SafeStr

func SafeStr(in interface{}, defaultStr string) string

func SafeUint added in v1.1.0

func SafeUint(in interface{}, defaultNumber uint) uint

func SafeUint16 added in v1.1.0

func SafeUint16(in interface{}, defaultNumber uint16) uint16

func SafeUint32 added in v1.1.0

func SafeUint32(in interface{}, defaultNumber uint32) uint32

func SafeUint64 added in v1.1.0

func SafeUint64(in interface{}, defaultNumber uint64) uint64

func SafeUint8 added in v1.1.0

func SafeUint8(in interface{}, defaultNumber uint8) uint8

func Str

func Str(in interface{}) (string, error)

func Str2Bytes

func Str2Bytes(s string) []byte

func StringPtr added in v1.0.1

func StringPtr(s string) *string

func Struct2JsonMap added in v1.0.1

func Struct2JsonMap(data interface{}) (result map[string]interface{}, err error)

func Struct2Map added in v1.0.1

func Struct2Map(data interface{}) (result map[string]interface{})

func StructDbTag2Map added in v1.1.2

func StructDbTag2Map(data interface{}) map[string]interface{}

func StructJsonTag2Map added in v1.1.2

func StructJsonTag2Map(data interface{}) map[string]interface{}

func StructTag2Map added in v1.0.1

func StructTag2Map(data interface{}, tag string) (result map[string]interface{})

func StructYamlTag2Map added in v1.1.2

func StructYamlTag2Map(data interface{}) map[string]interface{}

func StructYmlTag2Map added in v1.1.2

func StructYmlTag2Map(data interface{}) map[string]interface{}

func Time2Str

func Time2Str(x time.Time) string

func TimePtr added in v1.1.0

func TimePtr(v time.Time) *time.Time

func ToTimestampRange added in v1.4.0

func ToTimestampRange(start, end interface{}, sep string) string

func Uint added in v1.1.0

func Uint(in interface{}) (uint, error)

func Uint16 added in v1.1.0

func Uint16(in interface{}) (uint16, error)

func Uint16AsInt16

func Uint16AsInt16(ui uint16) int16

func Uint32 added in v1.1.0

func Uint32(in interface{}) (uint32, error)

func Uint32AsInt32

func Uint32AsInt32(ui uint32) int32

func Uint64 added in v1.1.0

func Uint64(in interface{}) (uint64, error)

func Uint64AsInt64

func Uint64AsInt64(ui uint64) int64

func Uint8 added in v1.1.0

func Uint8(in interface{}) (uint8, error)

func UnMarshalJson

func UnMarshalJson(data interface{}, target interface{}) error

Types

This section is empty.

Jump to

Keyboard shortcuts

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