// Package wrapper ... // // Description : wrapper ... // // Author : go_developer@163.com<白茶清欢> // // Date : 2023-05-05 11:59 package wrapper import ( "crypto/md5" "errors" "git.zhangdeman.cn/zhangdeman/serialize" "git.zhangdeman.cn/zhangdeman/util" "io" "strings" ) // String 字符串类型包装 // // Author : go_developer@163.com<白茶清欢> // // Date : 12:07 2023/5/5 type String string // ToFloat32 ... // // Author : go_developer@163.com<白茶清欢> // // Date : 18:26 2023/5/4 func (str String) ToFloat32() Float32Result { var ( res Float32Result ) res.Err = util.ConvertAssign(&res.Value, str) return res } // ToFloat32Ptr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 11:58 2023/5/16 func (str String) ToFloat32Ptr() Float32PtrResult { res := str.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 : 18:26 2023/5/4 func (str String) ToFloat64() Float64Result { var ( res Float64Result ) res.Err = util.ConvertAssign(&res.Value, str) return res } // ToFloat64Ptr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 11:59 2023/5/16 func (str String) ToFloat64Ptr() Float64PtrResult { res := str.ToFloat64() if nil != res.Err { return Float64PtrResult{ Value: nil, Err: res.Err, } } return Float64PtrResult{ Value: &res.Value, Err: nil, } } // ToDouble 转double // // Author : go_developer@163.com<白茶清欢> // // Date : 18:33 2023/5/4 func (str String) ToDouble() Float64Result { return str.ToFloat64() } // ToDoublePtr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 11:59 2023/5/16 func (str String) ToDoublePtr() Float64PtrResult { return str.ToFloat64Ptr() } // ToNumber 转数字, 使用最高精度的float64 // // Author : go_developer@163.com<白茶清欢> // // Date : 18:33 2023/5/4 func (str String) ToNumber() Float64Result { return str.ToFloat64() } // ToNumberPtr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:00 2023/5/16 func (str String) ToNumberPtr() Float64PtrResult { return str.ToFloat64Ptr() } // ToInt8 ... // // Author : go_developer@163.com<白茶清欢> // // Date : 18:29 2023/5/4 func (str String) ToInt8() Int8Result { var ( res Int8Result ) res.Err = util.ConvertAssign(&res.Value, str) return res } // ToInt8Ptr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:01 2023/5/16 func (str String) ToInt8Ptr() Int8PtrResult { res := str.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 : 18:29 2023/5/4 func (str String) ToInt16() Int16Result { var ( res Int16Result ) res.Err = util.ConvertAssign(&res.Value, str) return res } // ToInt16Ptr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:02 2023/5/16 func (str String) ToInt16Ptr() Int16PtrResult { res := str.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 : 18:29 2023/5/4 func (str String) ToInt32() Int32Result { var ( res Int32Result ) res.Err = util.ConvertAssign(&res.Value, str) return res } // ToInt32Ptr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:03 2023/5/16 func (str String) ToInt32Ptr() Int32PtrResult { res := str.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 : 18:29 2023/5/4 func (str String) ToInt64() Int64Result { var ( res Int64Result ) res.Err = util.ConvertAssign(&res.Value, str) return res } // ToInt64Ptr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:03 2023/5/16 func (str String) ToInt64Ptr() Int64PtrResult { res := str.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 : 10:47 2023/5/5 func (str String) ToInt() IntResult { var ( res IntResult ) res.Err = util.ConvertAssign(&res.Value, str) return res } // ToIntPtr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:04 2023/5/16 func (str String) ToIntPtr() IntPtrResult { res := str.ToInt() if nil != res.Err { return IntPtrResult{ Value: nil, Err: res.Err, } } return IntPtrResult{ Value: &res.Value, Err: nil, } } // ToUint8 ... // // Author : go_developer@163.com<白茶清欢> // // Date : 18:30 2023/5/4 func (str String) ToUint8() Uint8Result { var ( res Uint8Result ) res.Err = util.ConvertAssign(&res.Value, str) return res } // ToUint8Ptr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:05 2023/5/16 func (str String) ToUint8Ptr() Uint8PtrResult { res := str.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 : 18:29 2023/5/4 func (str String) ToUint16() Uint16Result { var ( res Uint16Result ) res.Err = util.ConvertAssign(&res.Value, str) return res } // ToUint16Ptr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:05 2023/5/16 func (str String) ToUint16Ptr() Uint16PtrResult { res := str.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 : 18:29 2023/5/4 func (str String) ToUint32() Uint32Result { var ( res Uint32Result ) res.Err = util.ConvertAssign(&res.Value, str) return res } // ToUint32Ptr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:05 2023/5/16 func (str String) ToUint32Ptr() Uint32PtrResult { res := str.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 : 18:29 2023/5/4 func (str String) ToUint64() Uint64Result { var ( res Uint64Result ) res.Err = util.ConvertAssign(&res.Value, str) return res } // ToUint64Ptr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:06 2023/5/16 func (str String) ToUint64Ptr() Uint64PtrResult { res := str.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 : 18:29 2023/5/4 func (str String) ToUint() UintResult { var ( res UintResult ) res.Err = util.ConvertAssign(&res.Value, str) return res } // ToUintPtr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:06 2023/5/16 func (str String) ToUintPtr() UintPtrResult { res := str.ToUint() if nil != res.Err { return UintPtrResult{ Value: nil, Err: res.Err, } } return UintPtrResult{ Value: &res.Value, Err: nil, } } // ToBool ... // // Author : go_developer@163.com<白茶清欢> // // Date : 18:34 2023/5/4 func (str String) ToBool() BoolResult { var ( res BoolResult ) res.Err = util.ConvertAssign(&res.Value, str) return res } // ToBoolPtr ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:07 2023/5/16 func (str String) ToBoolPtr() BoolPtrResult { res := str.ToBool() if nil != res.Err { return BoolPtrResult{ Value: nil, Err: res.Err, } } return BoolPtrResult{ Value: &res.Value, Err: nil, } } // ToStringPtr 转换成字符串指针 // // Author : go_developer@163.com<白茶清欢> // // Date : 15:57 2023/7/3 func (str String) ToStringPtr() StringPtrResult { val := str.Value() return StringPtrResult{ Value: &val, Err: nil, } } // ToObject ... // // Author : go_developer@163.com<白茶清欢> // // Date : 18:35 2023/5/4 func (str String) ToObject() ObjectResult { var ( res = ObjectResult{ Value: map[string]interface{}{}, Err: nil, } ) res.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &res.Value) return res } // ToStruct ... // // Author : go_developer@163.com<白茶清欢> // // Date : 18:38 2023/5/4 func (str String) ToStruct(receiver interface{}) error { if nil == receiver { return errors.New("receiver is nil") } return serialize.JSON.UnmarshalWithNumber([]byte(str), receiver) } // ToInt8Slice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 18:55 2023/5/4 func (str String) ToInt8Slice(splitChar ...string) Int8SliceResult { result := Int8SliceResult{ Value: []int8{}, Err: nil, } isSplit := len(splitChar) > 0 if !isSplit { result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } var ( res = make([]int8, 0) ) arr := strings.Split(string(str), splitChar[0]) for _, item := range arr { if itemVal := String(item).ToInt8(); nil != itemVal.Err { result.Err = itemVal.Err return result } else { res = append(res, itemVal.Value) } } result.Value = res return result } // ToInt16Slice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 19:01 2023/5/4 func (str String) ToInt16Slice(splitChar ...string) Int16SliceResult { result := Int16SliceResult{ Value: []int16{}, Err: nil, } isSplit := len(splitChar) > 0 if !isSplit { result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } res := make([]int16, 0) arr := strings.Split(string(str), splitChar[0]) for _, item := range arr { if itemVal := String(item).ToInt16(); nil != itemVal.Err { result.Err = itemVal.Err return result } else { res = append(res, itemVal.Value) } } result.Value = res return result } // ToInt32Slice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 19:03 2023/5/4 func (str String) ToInt32Slice(splitChar ...string) Int32SliceResult { result := Int32SliceResult{ Value: []int32{}, Err: nil, } isSplit := len(splitChar) > 0 if !isSplit { result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } res := make([]int32, 0) arr := strings.Split(string(str), splitChar[0]) for _, item := range arr { if itemVal := String(item).ToInt32(); nil != itemVal.Err { result.Err = itemVal.Err return result } else { res = append(res, itemVal.Value) } } result.Value = res return result } // ToInt64Slice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 19:04 2023/5/4 func (str String) ToInt64Slice(splitChar ...string) Int64SliceResult { result := Int64SliceResult{ Value: []int64{}, Err: nil, } isSplit := len(splitChar) > 0 if !isSplit { result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } res := make([]int64, 0) arr := strings.Split(string(str), splitChar[0]) for _, item := range arr { if itemVal := String(item).ToInt64(); nil != itemVal.Err { return result } else { res = append(res, itemVal.Value) } } result.Value = res return result } // ToIntSlice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 19:04 2023/5/4 func (str String) ToIntSlice(splitChar ...string) IntSliceResult { result := IntSliceResult{ Value: []int{}, Err: nil, } isSplit := len(splitChar) > 0 if !isSplit { result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } res := make([]int, 0) arr := strings.Split(string(str), splitChar[0]) for _, item := range arr { if itemVal := String(item).ToInt(); nil != itemVal.Err { result.Err = itemVal.Err return result } else { res = append(res, itemVal.Value) } } result.Value = res return result } // ToUint8Slice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 10:50 2023/5/5 func (str String) ToUint8Slice(splitChar ...string) Uint8SliceResult { result := Uint8SliceResult{ Value: []uint8{}, Err: nil, } isSplit := len(splitChar) > 0 if !isSplit { result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } res := make([]uint8, 0) arr := strings.Split(string(str), splitChar[0]) for _, item := range arr { if itemVal := String(item).ToUint8(); nil != itemVal.Err { result.Err = itemVal.Err return result } else { res = append(res, itemVal.Value) } } result.Value = res return result } // ToUint16Slice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 10:52 2023/5/5 func (str String) ToUint16Slice(splitChar ...string) Uint16SliceResult { result := Uint16SliceResult{ Value: []uint16{}, Err: nil, } isSplit := len(splitChar) > 0 if !isSplit { result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } res := make([]uint16, 0) arr := strings.Split(string(str), splitChar[0]) for _, item := range arr { if itemVal := String(item).ToUint16(); nil != itemVal.Err { result.Err = itemVal.Err return result } else { res = append(res, itemVal.Value) } } result.Value = res return result } // ToUint32Slice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 10:53 2023/5/5 func (str String) ToUint32Slice(splitChar ...string) Uint32SliceResult { result := Uint32SliceResult{ Value: []uint32{}, Err: nil, } isSplit := len(splitChar) > 0 if !isSplit { result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } res := make([]uint32, 0) arr := strings.Split(string(str), splitChar[0]) for _, item := range arr { if itemVal := String(item).ToUint32(); nil != itemVal.Err { result.Err = itemVal.Err return result } else { res = append(res, itemVal.Value) } } result.Value = res return result } // ToUint64Slice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 10:54 2023/5/5 func (str String) ToUint64Slice(splitChar ...string) Uint64SliceResult { result := Uint64SliceResult{ Value: []uint64{}, Err: nil, } isSplit := len(splitChar) > 0 if !isSplit { result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } res := make([]uint64, 0) arr := strings.Split(string(str), splitChar[0]) for _, item := range arr { if itemVal := String(item).ToUint64(); nil != itemVal.Err { result.Err = itemVal.Err return result } else { res = append(res, itemVal.Value) } } return result } // ToUintSlice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 10:54 2023/5/5 func (str String) ToUintSlice(splitChar ...string) UintSliceResult { result := UintSliceResult{ Value: []uint{}, Err: nil, } isSplit := len(splitChar) > 0 if !isSplit { result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } res := make([]uint, 0) arr := strings.Split(string(str), splitChar[0]) for _, item := range arr { if itemVal := String(item).ToUint(); nil != itemVal.Err { result.Err = itemVal.Err return result } else { res = append(res, itemVal.Value) } } result.Value = res return result } // ToBoolSlice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 10:55 2023/5/5 func (str String) ToBoolSlice(splitChar ...string) BoolSliceResult { result := BoolSliceResult{ Value: []bool{}, Err: nil, } isSplit := len(splitChar) > 0 if !isSplit { result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } res := make([]bool, 0) arr := strings.Split(string(str), splitChar[0]) for _, item := range arr { if itemVal := String(item).ToBool(); nil != itemVal.Err { result.Err = itemVal.Err return result } else { res = append(res, itemVal.Value) } } result.Value = res return result } // ToFloat32Slice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 10:56 2023/5/5 func (str String) ToFloat32Slice(splitChar ...string) Float32SliceResult { result := Float32SliceResult{ Value: []float32{}, Err: nil, } isSplit := len(splitChar) > 0 if !isSplit { result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } res := make([]float32, 0) arr := strings.Split(string(str), splitChar[0]) for _, item := range arr { if itemVal := String(item).ToFloat32(); nil != itemVal.Err { result.Err = itemVal.Err return result } else { res = append(res, itemVal.Value) } } return result } // ToFloat64Slice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 10:56 2023/5/5 func (str String) ToFloat64Slice(splitChar ...string) Float64SliceResult { result := Float64SliceResult{ Value: []float64{}, Err: nil, } isSplit := len(splitChar) > 0 if !isSplit { result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } res := make([]float64, 0) arr := strings.Split(string(str), splitChar[0]) for _, item := range arr { if itemVal := String(item).ToFloat64(); nil != itemVal.Err { result.Err = itemVal.Err return result } else { res = append(res, itemVal.Value) } } result.Value = res return result } // ToDoubleSlice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 10:57 2023/5/5 func (str String) ToDoubleSlice(splitChar ...string) Float64SliceResult { return str.ToFloat64Slice(splitChar...) } // ToNumberSlice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 10:57 2023/5/5 func (str String) ToNumberSlice(splitChar ...string) Float64SliceResult { return str.ToFloat64Slice(splitChar...) } // ToStringSlice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 15:11 2023/8/6 func (str String) ToStringSlice(splitChar ...string) StringSliceResult { result := StringSliceResult{ Value: []string{}, Err: nil, } isSplit := len(splitChar) > 0 if !isSplit { result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } result.Value = strings.Split(string(str), splitChar[0]) return result } // ToAnySlice ... // // Author : go_developer@163.com<白茶清欢> // // Date : 15:01 2023/5/5 func (str String) ToAnySlice() AnySliceResult { result := AnySliceResult{ Value: []interface{}{}, Err: nil, } result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value) return result } // Md5 计算Md5值 // // Author : go_developer@163.com<白茶清欢> // // Date : 12:08 2023/5/16 func (str String) Md5() StringResult { h := md5.New() _, err := io.WriteString(h, str.Value()) if nil != err { return StringResult{ Value: "", Err: err, } } return StringResult{ Value: string(h.Sum(nil)), Err: nil, } } // Value ... // // Author : go_developer@163.com<白茶清欢> // // Date : 12:11 2023/5/16 func (str String) Value() string { return string(str) }