tc

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 10, 2022 License: MIT Imports: 4 Imported by: 1

README

TypeConverter


go commit license

关于

这不过就是一个类型转换器. 因为太多的时候无法分出到底interface{}过来的数据的类型是哪个, 所以就想着弄了个这玩意.


用法

首先你得依靠自己的力量在你的项目上安装好Golang环境, 并且确认你即将导入包的Golang项目是可用的, 然后:

  1. 在需要使用的脚本中引入包:
import tc "github.com/kisschou/TypeConverter"

  1. 在需要使用的地方使用:
var t interface{} // 假定一个不知道类型的数据

tcImpl := tc.New(t) // 将未知类型的数据初始化脚本

str := tcImpl.String // 获取string类型的数据
i64 := tcImpl.Int64 // 获取int64的数据
// 可选的类型:
// .String => string
// .Byte => byte
// .Bytes => []byte
// .Bool => bool
// .Int => int
// .Int8 => int8
// .Int16 => int16
// .Int32 => int32
// .Int64 => int64
// .Uint => uint
// .Uint8 => uint8
// .Uint16 => uint16
// .Uint32 => uint32
// .Uint64 => uint64
// .Float32 => float32
// .Float64 => float64
// .Complex64 => complex64
// .Complex128 => complex128
// InterfaceSlice => []interface{}
// StringMapInterface => map[string]interface{}

tcImpl.Equal("hello") // 判断是否相等
tcImpl.Compare("EGT", 13) // 判断是否大于等于13

通用函数

总结一些我觉得可用的上的函数:

1. (*TypeConverter) Equal(compareData interface{}) bool

比对新传入的对象与当前的对象是否相等.

Example:

import tc "github.com/kisschou/TypeConverter"

// 待比较对象
var d1, d2 interface{}

// 比较
if tc.New(d1).Equal(d2) {
    // true...
} else {
    // false...
}

2. (*TypeConverter) Compare(operator string, compareData interface{}) bool

传入新对象和运算符以与当前对象进行比较.

Example:

import tc "github.com/kisschou/TypeConverter"

// 待比较对象
var d1, d2 interface{}

// 比较
if tc.New(d1).Compare("GT", d2) {
    // true...
} else {
    // false...
}

可选运算符: EQ(=), NEQ(!=), EGT(>=), GT(>), LT(<), ELT(<=)


3. SliceEqual(d1, d2 []interface{}) bool

传入两个切片, 比对他们是否相等.

Example:

import tc "github.com/kisschou/TypeConverter"

// 待比较对象
var d1, d2 []interface{}

// 比较
if tc.SliceEqual(d1, d2) {
    // true...
} else {
    // false...
}

4. MapEqual(d1, d2 map[string]interface{}) bool

传入两个map, 比对他们是否相等.

Example:

import tc "github.com/kisschou/TypeConverter"

// 待比较对象
var d1, d2 map[string]interface{}

// 比较
if tc.MapEqual(d1, d2) {
    // true...
} else {
    // false...
}

Licence

Copyright (c) 2022-present Kisschou.

Documentation

Overview

Package TypeConverter implements type conversion. Used to safely obtain data of the specified type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolToComplex

func BoolToComplex(input bool, bitSize int) interface{}

BoolToComplex Convert bool to complex.

func BoolToFloat

func BoolToFloat(input bool, bitSize int) interface{}

BoolToFloat Convert bool to float.

func BoolToInt

func BoolToInt(input bool, bitSize int) interface{}

BoolToInt Convert bool to int.

func BoolToMap

func BoolToMap(input bool) map[string]interface{}

BoolToMap Convert bool to map.

func BoolToSlice

func BoolToSlice(input bool) []interface{}

BoolToSlice Convert bool to slice.

func BoolToString

func BoolToString(input bool) string

BoolToString Convert bool to string.

func BoolToUint

func BoolToUint(input bool, bitSize int) interface{}

BoolToUint Convert bool to uint.

func ComplexToString

func ComplexToString(c complex128) string

ComplexToString Convert complex128 to string.

func FloatToString

func FloatToString(i float64) string

FloatToString Convert float64 to string.

func IntToString

func IntToString(i int64) string

IntToString Convert int to string.

func MapEqual

func MapEqual(d1, d2 map[string]interface{}) bool

MapEqual Compares two maps for equality.

func MapToBytes

func MapToBytes(input map[string]interface{}) []byte

MapToString Convert map[string]interface{} to []byte.

func MapToSlice

func MapToSlice(input map[string]interface{}) []interface{}

MapToSlice Convert map[string]interface{} to []interface{}.

func MapToStringMapInterface

func MapToStringMapInterface(input interface{}) map[string]interface{}

MapToStringMapInterface Convert map[string]interface{} to map[string]interface{}.

func SliceEqual

func SliceEqual(d1, d2 []interface{}) bool

SliceEqual Compares two slices for equality.

func SliceIsBytes

func SliceIsBytes(input []interface{}) bool

SliceIsBytes check is []uint8 or []byte.

func SliceToBytes

func SliceToBytes(input []interface{}) []byte

SliceToString Convert slice to string.

func SliceToInterfaceSlice

func SliceToInterfaceSlice(input interface{}) []interface{}

SliceToInterfaceSlice Convert slice to []interface{}.

func SliceToMap

func SliceToMap(input []interface{}) map[string]interface{}

SliceToMap Convert slice to map[string]interface{}

func SliceToString

func SliceToString(input []interface{}) string

SliceToString Convert slice to string.

func StringToBool

func StringToBool(input string) bool

StringToBool Convert string to bool.

func StringToBytes

func StringToBytes(input string) []byte

StringToBytes Convert string to bytes.

func StringToComplex

func StringToComplex(input string, bitSize int) interface{}

StringToComplex Convert string to complex. bitSize: 64/128.

func StringToFloat

func StringToFloat(input string, bitSize int) interface{}

StringToFloat Convert string to float.

func StringToInt

func StringToInt(input string, bitSize int) interface{}

StringToInt Convert string to int.

func StringToMap

func StringToMap(input string) map[string]interface{}

StringToMap Convert string to map.

func StringToSlice

func StringToSlice(input string) []interface{}

StringToSlice Convert string to slice.

func StringToUint

func StringToUint(input string, bitSize int) interface{}

StringToUint Convert string to uint.

func UintToString

func UintToString(i uint64) string

UintToString Convert uint to string.

Types

type TypeConverter

type TypeConverter struct {
	// String string
	String string

	// Byte byte
	Byte byte

	// Bytes []byte
	Bytes []byte

	// Bool bool
	Bool bool

	// Int int
	Int int

	// Int8 int8
	Int8 int8

	// Int16 int16
	Int16 int16

	// Int32 int32
	Int32 int32

	// Int64 int64
	Int64 int64

	// Uint unsigened int
	Uint uint

	// Uint8 unsigened int8
	Uint8 uint8

	// Uint16 unsigened int16
	Uint16 uint16

	// Uint32 unsigened int32
	Uint32 uint32

	// Uint64 unsigened int64
	Uint64 uint64

	// Float32 float32
	Float32 float32

	// Float64 float64
	Float64 float64

	// Complex64 complex64
	Complex64 complex64

	// Complex128 complex128
	Complex128 complex128

	// Slice []interface{}
	Slice []interface{}

	// Map map[string]interface{}
	Map map[string]interface{}
}

TypeConverter .

func BoolToStruct

func BoolToStruct(input bool) *TypeConverter

BoolToStruct Build *TypeConverter by input string.

func MapToStruct

func MapToStruct(input map[string]interface{}) *TypeConverter

MapToStruct Convert map[string]interface{} to struct.

func New

func New(input interface{}) *TypeConverter

New Init *TypeConverter.

func SliceToStruct

func SliceToStruct(input []interface{}) *TypeConverter

SliceToStruct Convert slice to struct.

func StringToStruct

func StringToStruct(input string) *TypeConverter

StringToStruct Build *TypeConverter by input string.

func (*TypeConverter) Compare

func (tc *TypeConverter) Compare(operator string, compareData interface{}) bool

Compare Pass in the new object and operator to compare against the current object. The optional operators are: EQ - = NEQ - != GT - > EGT - >= LT - < ELT - <=

func (*TypeConverter) Equal

func (tc *TypeConverter) Equal(compareData interface{}) bool

Equal Pass in a new object and compare it with the current object to check if the values are equal.

Jump to

Keyboard shortcuts

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