feature/upgrade_t #11

Merged
zhangdeman merged 3 commits from feature/upgrade_t into master 2025-10-14 11:49:48 +08:00
14 changed files with 114 additions and 1567 deletions

View File

@ -1,18 +0,0 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-05-06 下午2:48
package wrapper
import (
"fmt"
"testing"
)
func TestArray_Unique(t *testing.T) {
fmt.Println(ArrayType([]any{"1", 1, 1, "1", 2, 3}).Unique())
fmt.Println(ArrayType([]int{1, 1, 2, 3}).Unique())
}

View File

@ -1,11 +1,11 @@
// Package wrapper ...
// Package bigint ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-11-19 16:33
package wrapper
package bigint
import (
"database/sql/driver"

View File

@ -1,394 +0,0 @@
// Package wrapper ...
//
// Description : 任意类型之间的相互转换
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-02-23 10:23 下午
package wrapper
/*
Desc : 在处理一些参数的时候,可能需要将参数转换为各种类型,这里实现一个通用的转换函数,实现各种类型之间的相互转换。
当然如果源数据格式和目标数据类型不一致是会返回错误的。例如将字符串“一二三”转换为数值类型则会报错而将字符串“123”转换为数值类型则OK。
这段代码实际抄自go自带的“database/sql”库只是源代码作为内部函数无法在外面调用可以单独把需要的功能拎出来使用
代码中有一个Scaner接口可以自行实现然后通过"convertAssign()"函数作为dst参数传入。
Author : zhangdeman001@ke.com<白茶清欢>
*/
import (
"errors"
"fmt"
"reflect"
"strconv"
"time"
)
// RawBytes is a byte slice that holds a reference to memory owned by
// the database itself. After a Scan into a RawBytes, the slice is only
// valid until the next call to Next, Scan, or Close.
type RawBytes []byte
var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error
// ConvertAssign ...
// convertAssign copies to dest the value in src, converting it if possible.
// An error is returned if the copy would result in loss of information.
// dest should be a pointer type.
func ConvertAssign(dest, src any) error {
// Common cases, without reflect.
switch s := src.(type) {
case string:
switch d := dest.(type) {
case *string:
if d == nil {
return errNilPtr
}
*d = s
return nil
case *[]byte:
if d == nil {
return errNilPtr
}
*d = []byte(s)
return nil
case *RawBytes:
if d == nil {
return errNilPtr
}
*d = append((*d)[:0], s...)
return nil
}
case []byte:
switch d := dest.(type) {
case *string:
if d == nil {
return errNilPtr
}
*d = string(s)
return nil
case *any:
if d == nil {
return errNilPtr
}
*d = cloneBytes(s)
return nil
case *[]byte:
if d == nil {
return errNilPtr
}
*d = cloneBytes(s)
return nil
case *RawBytes:
if d == nil {
return errNilPtr
}
*d = s
return nil
}
case time.Time:
switch d := dest.(type) {
case *time.Time:
*d = s
return nil
case *string:
*d = s.Format(time.RFC3339Nano)
return nil
case *[]byte:
if d == nil {
return errNilPtr
}
*d = []byte(s.Format(time.RFC3339Nano))
return nil
case *RawBytes:
if d == nil {
return errNilPtr
}
*d = s.AppendFormat((*d)[:0], time.RFC3339Nano)
return nil
}
case nil:
switch d := dest.(type) {
case *any:
if d == nil {
return errNilPtr
}
*d = nil
return nil
case *[]byte:
if d == nil {
return errNilPtr
}
*d = nil
return nil
case *RawBytes:
if d == nil {
return errNilPtr
}
*d = nil
return nil
}
}
var sv reflect.Value
switch d := dest.(type) {
case *string:
sv = reflect.ValueOf(src)
switch sv.Kind() {
case reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
*d = asString(src)
return nil
}
case *[]byte:
sv = reflect.ValueOf(src)
if b, ok := asBytes(nil, sv); ok {
*d = b
return nil
}
case *RawBytes:
sv = reflect.ValueOf(src)
if b, ok := asBytes([]byte(*d)[:0], sv); ok {
*d = RawBytes(b)
return nil
}
case *bool:
bv, err := Bool.ConvertValue(src)
if err == nil {
*d = bv.(bool)
}
return err
case *any:
*d = src
return nil
}
if scanner, ok := dest.(Scanner); ok {
return scanner.Scan(src)
}
dpv := reflect.ValueOf(dest)
if dpv.Kind() != reflect.Ptr {
return errors.New("destination not a pointer")
}
if dpv.IsNil() {
return errNilPtr
}
if !sv.IsValid() {
sv = reflect.ValueOf(src)
}
dv := reflect.Indirect(dpv)
if sv.IsValid() && sv.Type().AssignableTo(dv.Type()) {
switch b := src.(type) {
case []byte:
dv.Set(reflect.ValueOf(cloneBytes(b)))
default:
dv.Set(sv)
}
return nil
}
if dv.Kind() == sv.Kind() && sv.Type().ConvertibleTo(dv.Type()) {
dv.Set(sv.Convert(dv.Type()))
return nil
}
// The following conversions use a string value as an intermediate representation
// to convert between various numeric types.
//
// This also allows scanning into user defined types such as "type Int int64".
// For symmetry, also check for string destination types.
switch dv.Kind() {
case reflect.Ptr:
if src == nil {
dv.Set(reflect.Zero(dv.Type()))
return nil
}
dv.Set(reflect.New(dv.Type().Elem()))
return ConvertAssign(dv.Interface(), src)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s := asString(src)
i64, err := strconv.ParseInt(s, 10, dv.Type().Bits())
if err != nil {
err = strconvErr(err)
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
}
dv.SetInt(i64)
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
s := asString(src)
u64, err := strconv.ParseUint(s, 10, dv.Type().Bits())
if err != nil {
err = strconvErr(err)
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
}
dv.SetUint(u64)
return nil
case reflect.Float32, reflect.Float64:
s := asString(src)
f64, err := strconv.ParseFloat(s, dv.Type().Bits())
if err != nil {
err = strconvErr(err)
return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err)
}
dv.SetFloat(f64)
return nil
case reflect.String:
switch v := src.(type) {
case string:
dv.SetString(v)
return nil
case []byte:
dv.SetString(string(v))
return nil
}
}
return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, dest)
}
func strconvErr(err error) error {
if ne, ok := err.(*strconv.NumError); ok {
return ne.Err
}
return err
}
func cloneBytes(b []byte) []byte {
if b == nil {
return nil
}
c := make([]byte, len(b))
copy(c, b)
return c
}
func ToString(src any) string {
return asString(src)
}
func asString(src any) string {
switch v := src.(type) {
case string:
return v
case []byte:
return string(v)
}
rv := reflect.ValueOf(src)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(rv.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(rv.Uint(), 10)
case reflect.Float64:
return strconv.FormatFloat(rv.Float(), 'g', -1, 64)
case reflect.Float32:
return strconv.FormatFloat(rv.Float(), 'g', -1, 32)
case reflect.Bool:
return strconv.FormatBool(rv.Bool())
}
return fmt.Sprintf("%v", src)
}
func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) {
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.AppendInt(buf, rv.Int(), 10), true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.AppendUint(buf, rv.Uint(), 10), true
case reflect.Float32:
return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 32), true
case reflect.Float64:
return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 64), true
case reflect.Bool:
return strconv.AppendBool(buf, rv.Bool()), true
case reflect.String:
s := rv.String()
return append(buf, s...), true
}
return
}
// Value is a value that drivers must be able to handle.
// It is either nil, a type handled by a database driver's NamedValueChecker
// interface, or an instance of one of these types:
//
// int64
// float64
// bool
// []byte
// string
// time.Time
type Value any
type boolType struct{}
var Bool boolType
func (boolType) String() string { return "Bool" }
func (boolType) ConvertValue(src any) (Value, error) {
switch s := src.(type) {
case bool:
return s, nil
case string:
b, err := strconv.ParseBool(s)
if err != nil {
return nil, fmt.Errorf("sql/driver: couldn't convert %q into type bool", s)
}
return b, nil
case []byte:
b, err := strconv.ParseBool(string(s))
if err != nil {
return nil, fmt.Errorf("sql/driver: couldn't convert %q into type bool", s)
}
return b, nil
}
sv := reflect.ValueOf(src)
switch sv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
iv := sv.Int()
if iv == 1 || iv == 0 {
return iv == 1, nil
}
return nil, fmt.Errorf("sql/driver: couldn't convert %d into type bool", iv)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
uv := sv.Uint()
if uv == 1 || uv == 0 {
return uv == 1, nil
}
return nil, fmt.Errorf("sql/driver: couldn't convert %d into type bool", uv)
}
return nil, fmt.Errorf("sql/driver: couldn't convert %v (%T) into type bool", src, src)
}
type Scanner interface {
// Scan assigns a value from a database driver.
//
// The src value will be of one of the following types:
//
// int64
// float64
// bool
// []byte
// string
// time.Time
// nil - for NULL values
//
// An error should be returned if the value cannot be stored
// without loss of information.
//
// Reference types such as []byte are only valid until the next call to Scan
// and should not be retained. Their underlying memory is owned by the driver.
// If retention is necessary, copy their values before the next call to Scan.
Scan(src any) error
}

302
define.go
View File

@ -1,302 +0,0 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-05 14:44
package wrapper
import (
"time"
"git.zhangdeman.cn/zhangdeman/op_type"
)
// BaseTypeValueResult 基础类型结果
type BaseTypeValueResult[BaseType op_type.BaseType] struct {
Value BaseType `json:"value"`
Err error `json:"err"`
}
// Int8Result ...
type Int8Result struct {
Value int8 `json:"value"`
Err error `json:"err"`
}
// Int8PtrResult ...
type Int8PtrResult struct {
Value *int8 `json:"value"`
Err error `json:"err"`
}
// Int16Result ...
type Int16Result struct {
Value int16 `json:"value"`
Err error `json:"err"`
}
// Int16PtrResult ...
type Int16PtrResult struct {
Value *int16 `json:"value"`
Err error `json:"err"`
}
// Int32Result ...
type Int32Result struct {
Value int32 `json:"value"`
Err error `json:"err"`
}
// Int32PtrResult ...
type Int32PtrResult struct {
Value *int32 `json:"value"`
Err error `json:"err"`
}
// Int64Result ...
type Int64Result struct {
Value int64 `json:"value"`
Err error `json:"err"`
}
// Int64PtrResult ...
type Int64PtrResult struct {
Value *int64 `json:"value"`
Err error `json:"err"`
}
// IntResult ...
type IntResult struct {
Value int `json:"value"`
Err error `json:"err"`
}
// IntPtrResult ...
type IntPtrResult struct {
Value *int `json:"value"`
Err error `json:"err"`
}
// Uint8Result ...
type Uint8Result struct {
Value uint8 `json:"value"`
Err error `json:"err"`
}
// Uint8PtrResult ...
type Uint8PtrResult struct {
Value *uint8 `json:"value"`
Err error `json:"err"`
}
// Uint16Result ...
type Uint16Result struct {
Value uint16 `json:"value"`
Err error `json:"err"`
}
// Uint16PtrResult ...
type Uint16PtrResult struct {
Value *uint16 `json:"value"`
Err error `json:"err"`
}
// Uint32Result ...
type Uint32Result struct {
Value uint32 `json:"value"`
Err error `json:"err"`
}
// Uint32PtrResult ...
type Uint32PtrResult struct {
Value *uint32 `json:"value"`
Err error `json:"err"`
}
// Uint64Result ...
type Uint64Result struct {
Value uint64 `json:"value"`
Err error `json:"err"`
}
// Uint64PtrResult ...
type Uint64PtrResult struct {
Value *uint64 `json:"value"`
Err error `json:"err"`
}
// UintResult ...
type UintResult struct {
Value uint `json:"value"`
Err error `json:"err"`
}
// UintPtrResult ...
type UintPtrResult struct {
Value *uint `json:"value"`
Err error `json:"err"`
}
// Float32Result ...
type Float32Result struct {
Value float32 `json:"value"`
Err error `json:"err"`
}
// Float32PtrResult ...
type Float32PtrResult struct {
Value *float32 `json:"value"`
Err error `json:"err"`
}
// Float64Result ...
type Float64Result struct {
Value float64 `json:"value"`
Err error `json:"err"`
}
// Float64PtrResult ...
type Float64PtrResult struct {
Value *float64 `json:"value"`
Err error `json:"err"`
}
// Any ...
type Any struct {
Value any `json:"value"`
Err error `json:"err"`
}
// BoolResult ...
type BoolResult struct {
Value bool `json:"value"`
Err error `json:"err"`
}
// BoolPtrResult ...
type BoolPtrResult struct {
Value *bool `json:"value"`
Err error `json:"err"`
}
// ObjectResult ...
type ObjectResult struct {
Value map[string]any `json:"value"`
Err error `json:"err"`
}
// StringResult ...
type StringResult struct {
Value string `json:"value"`
Err error `json:"err"`
}
// StringPtrResult 字符串指针
type StringPtrResult struct {
Value *string `json:"value"`
Err error `json:"err"`
}
// Int8SliceResult ...
type Int8SliceResult struct {
Value []int8 `json:"value"`
Err error `json:"err"`
}
// Int16SliceResult ...
type Int16SliceResult struct {
Value []int16 `json:"value"`
Err error `json:"err"`
}
// Int32SliceResult ...
type Int32SliceResult struct {
Value []int32 `json:"value"`
Err error `json:"err"`
}
// Int64SliceResult ...
type Int64SliceResult struct {
Value []int64 `json:"value"`
Err error `json:"err"`
}
// IntSliceResult ...
type IntSliceResult struct {
Value []int `json:"value"`
Err error `json:"err"`
}
// Uint8SliceResult ...
type Uint8SliceResult struct {
Value []uint8 `json:"value"`
Err error `json:"err"`
}
// Uint16SliceResult ...
type Uint16SliceResult struct {
Value []uint16 `json:"value"`
Err error `json:"err"`
}
// Uint32SliceResult ...
type Uint32SliceResult struct {
Value []uint32 `json:"value"`
Err error `json:"err"`
}
// Uint64SliceResult ...
type Uint64SliceResult struct {
Value []uint64 `json:"value"`
Err error `json:"err"`
}
// UintSliceResult ...
type UintSliceResult struct {
Value []uint `json:"value"`
Err error `json:"err"`
}
// BoolSliceResult ...
type BoolSliceResult struct {
Value []bool `json:"value"`
Err error `json:"err"`
}
// Float32SliceResult ...
type Float32SliceResult struct {
Value []float32 `json:"value"`
Err error `json:"err"`
}
// Float64SliceResult ...
type Float64SliceResult struct {
Value []float64 `json:"value"`
Err error `json:"err"`
}
// DurationResult 时间转换结果
type DurationResult struct {
Value time.Duration `json:"value"`
Err error `json:"err"`
}
// StringSliceResult ...
type StringSliceResult struct {
Value []string `json:"value"`
Err error `json:"err"`
}
// MapResult 转map的结果
type MapResult struct {
Value any `json:"value"`
Err error `json:"err"`
}
// AnySliceResult ...
type AnySliceResult struct {
Value []any `json:"value"`
Err error `json:"err"`
}

View File

@ -52,7 +52,19 @@ type StructValueSliceResult[Value any] struct {
}
// StringResult ...
type StringResult struct {
/*type StringResult struct {
Value string `json:"value"`
Err error `json:"err"`
}*/
// NumberResult 数字转换结果
type NumberResult[ResultType op_type.Number] struct {
Value ResultType `json:"value"`
Err error `json:"err"`
}
// NumberPtrResult 数字指针转换结果
type NumberPtrResult[ResultType op_type.Number] struct {
Value *ResultType `json:"value"`
Err error `json:"err"`
}

132
float.go
View File

@ -1,132 +0,0 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-05 14:33
package wrapper
import (
"fmt"
"math"
)
// Float ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:33 2023/5/5
type Float float64
// ToFloat32 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:36 2023/5/5
func (f Float) ToFloat32() Float32Result {
res := Float32Result{
Value: 0,
Err: nil,
}
if f > math.MaxFloat32 || f < math.SmallestNonzeroFloat32 {
res.Err = fmt.Errorf("float32 should between %v and %v", float32(math.SmallestNonzeroFloat32), float32(math.MaxFloat32))
return res
}
res.Value = float32(f)
return res
}
// ToFloat32Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:10 2023/5/16
func (f Float) ToFloat32Ptr() Float32PtrResult {
res := f.ToFloat32()
if nil != res.Err {
return Float32PtrResult{
Value: nil,
Err: res.Err,
}
}
return Float32PtrResult{
Value: &res.Value,
Err: nil,
}
}
// ToFloat64 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:36 2023/5/5
func (f Float) ToFloat64() Float64Result {
res := Float64Result{
Value: 0,
Err: nil,
}
if f > math.MaxFloat64 || f < math.SmallestNonzeroFloat64 {
res.Err = fmt.Errorf("float32 should between %v and %v", float64(math.SmallestNonzeroFloat64), float64(math.MaxFloat64))
return res
}
res.Value = float64(f)
return res
}
// ToFloat64Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:11 2023/5/16
func (f Float) ToFloat64Ptr() Float64PtrResult {
res := f.ToFloat64()
if nil != res.Err {
return Float64PtrResult{
Value: nil,
Err: res.Err,
}
}
return Float64PtrResult{
Value: &res.Value,
Err: nil,
}
}
// ToString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:38 2023/5/5
func (f Float) ToString() StringResult {
floatVal := f.ToFloat64()
if nil != floatVal.Err {
return StringResult{
Value: "",
Err: floatVal.Err,
}
}
return StringResult{
Value: fmt.Sprintf("%v", floatVal.Value),
Err: nil,
}
}
// ToStringPtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:56 2023/5/16
func (f Float) ToStringPtr() StringPtrResult {
res := f.ToString()
if nil != res.Err {
return StringPtrResult{
Value: nil,
Err: res.Err,
}
}
return StringPtrResult{
Value: &res.Value,
Err: nil,
}
}

255
int.go
View File

@ -1,255 +0,0 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-05 13:56
package wrapper
import (
"fmt"
"math"
"time"
)
// Int int类型
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 13:57 2023/5/5
type Int int64
// ToDuration ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:33 2023/9/4
func (i Int) ToDuration(timeUnit time.Duration) DurationResult {
return DurationResult{
Value: time.Duration(i.ToInt64().Value) * timeUnit,
Err: nil,
}
}
// ToInt8 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 13:57 2023/5/5
func (i Int) ToInt8() Int8Result {
res := Int8Result{
Value: 0,
Err: nil,
}
if math.MaxInt8 < int64(i) || math.MinInt8 > int64(i) {
res.Err = fmt.Errorf("int8 value should between %v and %v ", int8(math.MinInt8), int8(math.MaxInt8))
return res
}
res.Value = int8(i)
return res
}
// ToInt8Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:08 2023/5/15
func (i Int) ToInt8Ptr() Int8PtrResult {
res := i.ToInt8()
if nil != res.Err {
return Int8PtrResult{
Value: nil,
Err: res.Err,
}
}
return Int8PtrResult{
Value: &res.Value,
Err: nil,
}
}
// ToInt16 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:05 2023/5/5
func (i Int) ToInt16() Int16Result {
res := Int16Result{
Value: 0,
Err: nil,
}
if math.MaxInt16 < int64(i) || math.MinInt16 > int64(i) {
res.Err = fmt.Errorf("int16 value should between %v and %v ", int16(math.MinInt16), int16(math.MaxInt16))
return res
}
res.Value = int16(i)
return res
}
// ToInt16Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:07 2023/5/15
func (i Int) ToInt16Ptr() Int16PtrResult {
res := i.ToInt16()
if nil != res.Err {
return Int16PtrResult{
Value: nil,
Err: res.Err,
}
}
return Int16PtrResult{
Value: &res.Value,
Err: nil,
}
}
// ToInt32 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:05 2023/5/5
func (i Int) ToInt32() Int32Result {
res := Int32Result{
Value: 0,
Err: nil,
}
if math.MaxInt32 < int64(i) || math.MinInt32 > int64(i) {
res.Err = fmt.Errorf("int32 value should between %v and %v ", int32(math.MinInt32), int32(math.MaxInt32))
return res
}
res.Value = int32(i)
return res
}
// ToInt32Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:07 2023/5/15
func (i Int) ToInt32Ptr() Int32PtrResult {
res := i.ToInt32()
if nil != res.Err {
return Int32PtrResult{
Value: nil,
Err: res.Err,
}
}
return Int32PtrResult{
Value: &res.Value,
Err: nil,
}
}
// ToInt64 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:06 2023/5/5
func (i Int) ToInt64() Int64Result {
res := Int64Result{
Value: 0,
Err: nil,
}
if math.MaxInt64 < i || math.MinInt64 > i {
res.Err = fmt.Errorf("int64 value should between %v and %v ", int64(math.MinInt64), int64(math.MaxInt64))
return res
}
res.Value = int64(i)
return res
}
// ToInt64Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:05 2023/5/15
func (i Int) ToInt64Ptr() Int64PtrResult {
res := i.ToInt64()
if nil != res.Err {
return Int64PtrResult{
Value: nil,
Err: res.Err,
}
}
return Int64PtrResult{
Value: &res.Value,
Err: nil,
}
}
// ToInt ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:07 2023/5/5
func (i Int) ToInt() IntResult {
res := IntResult{
Value: 0,
Err: nil,
}
if math.MaxInt < i || math.MinInt > i {
res.Err = fmt.Errorf("int value should between %v and %v ", int(math.MinInt), int(math.MaxInt))
}
res.Value = int(i)
return res
}
// ToIntPtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:52 2023/5/15
func (i Int) ToIntPtr() IntPtrResult {
intRes := i.ToInt()
if nil != intRes.Err {
return IntPtrResult{
Value: nil,
Err: intRes.Err,
}
}
return IntPtrResult{
Value: &intRes.Value,
Err: nil,
}
}
// ToString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:07 2023/5/5
func (i Int) ToString() StringResult {
result := i.ToInt64()
if nil != result.Err {
return StringResult{
Value: "",
Err: result.Err,
}
}
return StringResult{
Value: fmt.Sprintf("%v", result.Value),
Err: nil,
}
}
// ToStringPtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:02 2023/5/15
func (i Int) ToStringPtr() StringPtrResult {
result := i.ToString()
if nil != result.Err {
return StringPtrResult{
Value: nil,
Err: result.Err,
}
}
return StringPtrResult{
Value: &result.Value,
Err: nil,
}
}

150
object.go
View File

@ -1,150 +0,0 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-06-01 18:33
package wrapper
import (
"encoding/json"
"errors"
"git.zhangdeman.cn/zhangdeman/serialize"
"reflect"
)
// ObjectData 对象类型, 支持 nil / Map / Struct
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:36 2023/6/1
func ObjectData(data any) *ObjectType {
ot := &ObjectType{
source: data,
data: map[any]any{},
byteData: []byte{},
isValid: true,
invalidErr: errors.New("data is invalid"),
}
if nil == ot {
return ot
}
reflectType := reflect.TypeOf(data)
switch reflectType.Kind() {
case reflect.Map:
fallthrough
case reflect.Struct:
ot.byteData, _ = json.Marshal(ot.source)
default:
// 数据类型不是 nil / map / struct 之一
ot.isValid = false
}
return ot
}
// ObjectType ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:38 2023/6/1
type ObjectType struct {
source any
data map[any]any
byteData []byte
isValid bool
invalidErr error
}
// IsValid 是否有效对象数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:49 2023/6/1
func (ot *ObjectType) IsValid() bool {
return ot.isValid
}
// IsNil 是否为nil
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:50 2023/6/1
func (ot *ObjectType) IsNil() bool {
if ot.source == nil {
return true
}
return reflect.ValueOf(ot.source).IsNil()
}
// ToString 转字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:51 2023/6/1
func (ot *ObjectType) ToString() StringResult {
if ot.IsNil() {
return StringResult{
Value: "nil",
Err: nil,
}
}
if !ot.IsValid() {
// 非法对象数据
return StringResult{
Value: "",
Err: errors.New("data is invalid"),
}
}
return StringResult{
Value: string(ot.byteData),
Err: nil,
}
}
// ToMapStringAny ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:17 2023/6/2
func (ot *ObjectType) ToMapStringAny() ObjectResult {
res := ObjectResult{
Value: map[string]any{},
Err: nil,
}
if ot.IsNil() {
return res
}
res.Err = serialize.JSON.UnmarshalWithNumber(ot.byteData, &res.Value)
return res
}
// ToStruct ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:41 2023/6/2
func (ot *ObjectType) ToStruct(receiver any) error {
if nil == receiver {
return errors.New("receiver is nil")
}
if ot.IsNil() {
return errors.New("data is nil")
}
return serialize.JSON.UnmarshalWithNumber(ot.byteData, receiver)
}
// ToStructIgnoreErr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:42 2023/6/2
func (ot *ObjectType) ToStructIgnoreErr(receiver any) {
if nil == receiver {
return
}
if ot.IsNil() {
return
}
_ = serialize.JSON.UnmarshalWithNumber(ot.byteData, receiver)
}

View File

@ -49,11 +49,11 @@ func (a *Array[Bt]) ToStringSlice() []string {
}
// Unique 对数据结果进行去重
func (a *Array[Bt]) Unique(arr Array[Bt]) []Bt {
func (a *Array[Bt]) Unique() []Bt {
result := make([]Bt, 0)
dataTable := make(map[string]bool)
for _, item := range arr.value {
for _, item := range a.value {
byteData, _ := json.Marshal(item)
k := string(byteData)
if strings.HasPrefix(k, "\"\"") && strings.HasSuffix(k, "\"\"") {
@ -102,7 +102,7 @@ func (a *Array[Bt]) ToString() define.BaseValueResult[string] {
}
// ToStringWithSplit 数组按照指定分隔符转为字符串
func (a *Array[Bt]) ToStringWithSplit(arr Array[Bt], split string) define.BaseValueResult[string] {
func (a *Array[Bt]) ToStringWithSplit(split string) define.BaseValueResult[string] {
if a.IsNil() {
return define.BaseValueResult[string]{
Value: "",

86
op_number/number.go Normal file
View File

@ -0,0 +1,86 @@
// Package op_number ...
//
// Description : op_number ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2025-10-14 10:14
package op_number
import (
"git.zhangdeman.cn/zhangdeman/op_type"
"git.zhangdeman.cn/zhangdeman/wrapper/convert"
"git.zhangdeman.cn/zhangdeman/wrapper/define"
)
// ToNumber ...
func ToNumber[InputType op_type.BaseType, ResultType op_type.Number](in InputType) define.NumberResult[ResultType] {
var (
res ResultType
err error
)
if err = convert.ConvertAssign(&res, in); err != nil {
return define.NumberResult[ResultType]{
Err: err,
Value: res,
}
}
return define.NumberResult[ResultType]{
Err: nil,
Value: res,
}
}
// ToNumberPtr 数字指针
func ToNumberPtr[InputType op_type.BaseType, ResultType op_type.Number](in InputType) define.NumberPtrResult[ResultType] {
var (
res define.NumberResult[ResultType]
)
res = ToNumber[InputType, ResultType](in)
if nil != res.Err {
return define.NumberPtrResult[ResultType]{
Err: res.Err,
Value: nil,
}
}
return define.NumberPtrResult[ResultType]{
Err: nil,
Value: &res.Value,
}
}
// ToString 转换为字符串
func ToString[InputType op_type.Number](in InputType) define.BaseValueResult[string] {
var (
err error
res string
)
if err = convert.ConvertAssign(&res, in); err != nil {
return define.BaseValueResult[string]{
Value: res,
Err: err,
}
}
return define.BaseValueResult[string]{
Value: res,
Err: nil,
}
}
// ToStringPtr 字符串指针
func ToStringPtr[InputType op_type.Number](in InputType) define.BaseValuePtrResult[string] {
var (
res define.BaseValueResult[string]
)
res = ToString[InputType](in)
if nil != res.Err {
return define.BaseValuePtrResult[string]{
Err: res.Err,
Value: nil,
}
}
return define.BaseValuePtrResult[string]{
Err: nil,
Value: &res.Value,
}
}

View File

@ -57,23 +57,23 @@ func SnakeCaseToCamel(str string) string {
}
// Md5 计算Md5值
func Md5(str string) define.StringResult {
func Md5(str string) define.BaseValueResult[string] {
h := md5.New()
_, err := io.WriteString(h, str)
if nil != err {
return define.StringResult{
return define.BaseValueResult[string]{
Value: "",
Err: err,
}
}
return define.StringResult{
return define.BaseValueResult[string]{
Value: hex.EncodeToString(h.Sum(nil)),
Err: nil,
}
}
// RandomMd5 生成随机字符串MD%值
func RandomMd5() define.StringResult {
func RandomMd5() define.BaseValueResult[string] {
str := Random(64, "")
return Md5(str)
}

View File

@ -41,6 +41,6 @@ type Struct struct {
}
// ToMap 转为Map
func ToMap(s *Struct) op_map.Map {
func (s *Struct) ToMap() op_map.Map {
return op_map.EasyMap(s.data)
}

View File

@ -1,11 +1,11 @@
// Package wrapper ...
// Package op_time ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-08-09 18:22
package wrapper
package op_time
import (
"fmt"
@ -13,17 +13,11 @@ import (
)
const (
// StandTimeFormat 标准时间格式
StandTimeFormat = "2006-01-02 15:04:05"
// StandISO8601Time 标准ISO时间格式
StandISO8601Time = "2006-01-02T15:04:05+08:00"
)
// OwnTime ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:28 2023/8/9
func OwnTime(inputTime time.Time) *Time {
instance := &Time{}
instance.Time = inputTime
@ -31,59 +25,35 @@ func OwnTime(inputTime time.Time) *Time {
}
// OwnTimeFromNow 从当前时间获取实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:44 2023/8/10
func OwnTimeFromNow() *Time {
return OwnTime(time.Now())
}
// OwnTimeFromSecond 从秒获取实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:47 2023/8/9
func OwnTimeFromSecond(second int64) *Time {
t := time.UnixMilli(second * 1000)
return OwnTime(t)
}
// OwnTimeFromMilli 从ms获取实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:56 2023/8/9
func OwnTimeFromMilli(milli int64) *Time {
t := time.UnixMilli(milli)
return OwnTime(t)
}
// OwnTimeFromMicro 从微秒获取实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:00 2023/8/9
func OwnTimeFromMicro(micro int64) *Time {
t := time.UnixMicro(micro)
return OwnTime(t)
}
// OwnTimeFromNano 从纳秒获取实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:00 2023/8/9
func OwnTimeFromNano(nano int64) *Time {
t := time.Unix(nano/1e9, nano%1e9)
return OwnTime(t)
}
// OwnTimeFromISO8601Time ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:02 2023/8/10
func OwnTimeFromISO8601Time(parseTime string, layout ...string) *Time {
var (
err error
@ -98,66 +68,38 @@ func OwnTimeFromISO8601Time(parseTime string, layout ...string) *Time {
}
// Time 时间类型
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:23 2023/8/9
type Time struct {
time.Time
}
// GetCurrentFormatTime 获取当前时间的格式化时间(秒)
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 1:34 上午 2021/10/7
func (t *Time) GetCurrentFormatTime(layout ...string) string {
return time.Now().In(time.Local).Format(t.getTimeFormat(StandTimeFormat, layout...))
return time.Now().In(time.Local).Format(t.getTimeFormat(time.DateTime, layout...))
}
// FormatUnixNano 格式化纳秒时间戳
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:54 2022/7/14
func (t *Time) FormatUnixNano(layout ...string) string {
nano := t.UnixNano() % 1e6
return t.FormatUnixMilli(layout...) + fmt.Sprintf(" %v", nano)
}
// FormatUnixMilli 格式化毫秒时间戳
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:55 2022/7/14
func (t *Time) FormatUnixMilli(layout ...string) string {
return time.UnixMilli(t.UnixMilli()).In(time.Local).
Format(t.getTimeFormat(StandTimeFormat, layout...)) + fmt.Sprintf(".%v", t.UnixMilli()%1000)
Format(t.getTimeFormat(time.DateTime, layout...)) + fmt.Sprintf(".%v", t.UnixMilli()%1000)
}
// FormatUnixSec 格式化秒级时间戳
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:06 2022/7/14
func (t *Time) FormatUnixSec(layout ...string) string {
return time.Unix(t.Unix(), 0).In(time.Local).Format(t.getTimeFormat(StandTimeFormat, layout...))
return time.Unix(t.Unix(), 0).In(time.Local).Format(t.getTimeFormat(time.DateTime, layout...))
}
// ToString 转字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:16 2023/8/10
func (t *Time) ToString(layout ...string) string {
return t.FormatUnixSec(layout...)
}
// getTimeFormat 获取时间格式
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:37 2023/8/9
func (t *Time) getTimeFormat(defaultFormat string, layout ...string) string {
if len(layout) > 0 && len(layout[0]) > 0 {
return layout[0]

242
uint.go
View File

@ -1,242 +0,0 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-05 14:20
package wrapper
import (
"fmt"
"math"
)
// Uint ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:20 2023/5/5
type Uint uint64
// ToUint8 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:21 2023/5/5
func (ui Uint) ToUint8() Uint8Result {
res := Uint8Result{
Value: 0,
Err: nil,
}
if ui > math.MaxUint8 || ui < 0 {
res.Err = fmt.Errorf("uint8 should between 0 and %v", uint8(math.MaxUint8))
return res
}
res.Value = uint8(ui)
return res
}
// ToUint8Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:54 2023/5/16
func (ui Uint) ToUint8Ptr() Uint8PtrResult {
res := ui.ToUint8()
if nil != res.Err {
return Uint8PtrResult{
Value: nil,
Err: res.Err,
}
}
return Uint8PtrResult{
Value: &res.Value,
Err: nil,
}
}
// ToUint16 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:25 2023/5/5
func (ui Uint) ToUint16() Uint16Result {
res := Uint16Result{
Value: 0,
Err: nil,
}
if ui > math.MaxUint16 || ui < 0 {
res.Err = fmt.Errorf("uint16 should between 0 and %v", uint16(math.MaxUint16))
return res
}
res.Value = uint16(ui)
return res
}
// ToUint16Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:55 2023/5/16
func (ui Uint) ToUint16Ptr() Uint16PtrResult {
res := ui.ToUint16()
if nil != res.Err {
return Uint16PtrResult{
Value: nil,
Err: res.Err,
}
}
return Uint16PtrResult{
Value: &res.Value,
Err: nil,
}
}
// ToUint32 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:25 2023/5/5
func (ui Uint) ToUint32() Uint32Result {
res := Uint32Result{
Value: 0,
Err: nil,
}
if ui > math.MaxUint32 || ui < 0 {
res.Err = fmt.Errorf("uint32 should between 0 and %v", uint32(math.MaxUint32))
return res
}
res.Value = uint32(ui)
return res
}
// ToUint32Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:55 2023/5/16
func (ui Uint) ToUint32Ptr() Uint32PtrResult {
res := ui.ToUint32()
if nil != res.Err {
return Uint32PtrResult{
Value: nil,
Err: res.Err,
}
}
return Uint32PtrResult{
Value: &res.Value,
Err: nil,
}
}
// ToUint64 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:30 2023/5/5
func (ui Uint) ToUint64() Uint64Result {
res := Uint64Result{
Value: 0,
Err: nil,
}
if ui > math.MaxUint64 || ui < 0 {
res.Err = fmt.Errorf("uint64 should between 0 and %v", uint64(math.MaxUint64))
}
res.Value = uint64(ui)
return res
}
// ToUint64Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:57 2023/5/16
func (ui Uint) ToUint64Ptr() Uint64PtrResult {
res := ui.ToUint64()
if nil != res.Err {
return Uint64PtrResult{
Value: nil,
Err: res.Err,
}
}
return Uint64PtrResult{
Value: &res.Value,
Err: nil,
}
}
// ToUint ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:31 2023/5/5
func (ui Uint) ToUint() UintResult {
res := UintResult{
Value: 0,
Err: nil,
}
if ui > math.MaxUint || ui < 0 {
res.Err = fmt.Errorf("uint should between 0 and %v", uint(math.MaxUint))
return res
}
res.Value = uint(ui)
return res
}
// ToUintPtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:57 2023/5/16
func (ui Uint) ToUintPtr() UintPtrResult {
res := ui.ToUint()
if nil != res.Err {
return UintPtrResult{
Value: nil,
Err: res.Err,
}
}
return UintPtrResult{
Value: &res.Value,
Err: nil,
}
}
// ToString ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:32 2023/5/5
func (ui Uint) ToString() StringResult {
uint64Val := ui.ToUint64()
if nil != uint64Val.Err {
return StringResult{
Value: "",
Err: uint64Val.Err,
}
}
return StringResult{
Value: fmt.Sprintf("%v", uint64Val),
Err: nil,
}
}
// ToStringPtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:59 2023/5/16
func (ui Uint) ToStringPtr() StringPtrResult {
res := ui.ToString()
if nil != res.Err {
return StringPtrResult{
Value: nil,
Err: res.Err,
}
}
return StringPtrResult{
Value: &res.Value,
Err: nil,
}
}