remove code

This commit is contained in:
白茶清欢 2023-08-11 15:04:56 +08:00
parent 2e36e7b1ac
commit d6a489d586
6 changed files with 0 additions and 427 deletions

137
filter.go
View File

@ -1,137 +0,0 @@
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-07-04 11:45
package util
import (
"fmt"
"github.com/Jeffail/gabs"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
)
// MapRule 映射规则
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:21 2022/7/4
type MapRule struct {
SourcePath string `json:"source_path"` // 原路径
MapPath string `json:"map_path"` // 映射路径
Required bool `json:"required"` // 必须存在
DataType string `json:"data_type"` // 数据类型
DefaultValue string `json:"default_value"` // 默认值, 以字符串传入, 会转换成 DataType
}
// NewFilter 过滤器实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:54 2022/7/4
func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
return &filter{
sourceData: sourceData,
filterRuleList: filterRuleList,
jsonObj: &gabs.Container{},
}
}
// filter 数据过滤
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:58 2022/7/4
type filter struct {
sourceData string
filterRuleList []MapRule
jsonObj *gabs.Container // 生成的json对象实例
}
// Deal ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:59 2022/7/4
func (f *filter) Deal() ([]byte, error) {
for _, rule := range f.filterRuleList {
sourceResult := gjson.Get(f.sourceData, rule.SourcePath)
sourceVal := sourceResult.String()
if !sourceResult.Exists() {
// 不存在, 使用默认值
sourceVal = rule.DefaultValue
}
formatVal, err := f.getValue(rule.DataType, sourceVal)
if nil != err {
return nil, fmt.Errorf("%s = %v can not convert to %s : %s", rule.SourcePath, sourceResult.Value(), rule.DataType, err.Error())
}
if _, err := f.jsonObj.SetP(formatVal, rule.MapPath); nil != err {
return nil, fmt.Errorf("%s set val = %v fail : %s", rule.MapPath, formatVal, err.Error())
}
}
return f.jsonObj.EncodeJSON(), nil
}
// getValue 获取值
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:25 2022/7/4
func (f *filter) getValue(dataType string, defaultValue string) (interface{}, error) {
switch dataType {
case "int8":
fallthrough
case "int16":
fallthrough
case "int32":
fallthrough
case "int64":
fallthrough
case "int":
var (
err error
val int64
)
err = ConvertAssign(&val, defaultValue)
return val, err
case "uint8":
fallthrough
case "uint16":
fallthrough
case "uint32":
fallthrough
case "uint64":
fallthrough
case "uint":
var (
err error
val uint64
)
err = ConvertAssign(&val, defaultValue)
return val, err
case "bool":
var (
err error
val bool
)
err = ConvertAssign(&val, defaultValue)
return val, err
case "float32":
fallthrough
case "float64":
var (
err error
val float64
)
err = ConvertAssign(&val, defaultValue)
return val, err
case "string":
return defaultValue, nil
default:
return nil, errors.New(dataType + " is not support!")
}
}

View File

@ -1,57 +0,0 @@
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-07-04 12:44
package util
import (
"fmt"
"testing"
)
func Test_filter_Deal(t *testing.T) {
sourceData := `{
"name":"zhangsan",
"age":"18",
"extension":{
"sex":"man",
"height":"180"
}
}`
ruleList := []MapRule{
{
SourcePath: "name",
MapPath: "user_name",
Required: true,
DataType: "string",
DefaultValue: "lalala",
},
{
SourcePath: "age",
MapPath: "user_age",
Required: true,
DataType: "int",
DefaultValue: "280",
},
{
SourcePath: "extension.height",
MapPath: "user_height",
Required: true,
DataType: "int",
DefaultValue: "359",
},
{
SourcePath: "extension.sex",
MapPath: "user_sex",
Required: true,
DataType: "string",
DefaultValue: "lalala",
},
}
instance := NewFilter(sourceData, ruleList)
result, err := instance.Deal()
fmt.Println(string(result), err)
}

6
go.mod
View File

@ -3,22 +3,16 @@ module git.zhangdeman.cn/zhangdeman/util
go 1.20
require (
github.com/BurntSushi/toml v1.3.2
github.com/Jeffail/gabs v1.4.0
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
github.com/go-ini/ini v1.67.0
github.com/mitchellh/go-homedir v1.1.0
github.com/mozillazg/go-pinyin v0.20.0
github.com/pkg/errors v0.9.1
github.com/spaolacci/murmur3 v1.1.0
github.com/stretchr/testify v1.8.2
github.com/tidwall/gjson v1.14.4
gopkg.in/yaml.v3 v3.0.1
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
)

21
go.sum
View File

@ -1,31 +1,15 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
@ -33,8 +17,3 @@ github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JT
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -12,8 +12,6 @@ var (
Cli *cli
// Hash ...
Hash *hash
// String ...
String *stringOperate
// Calculate ...
Calculate *calculate
// Project ...
@ -27,7 +25,6 @@ var (
func init() {
Cli = &cli{}
Hash = &hash{}
String = &stringOperate{}
Calculate = &calculate{}
Project = &project{}
Console = &console{}

203
string.go
View File

@ -1,203 +0,0 @@
// Package util...
//
// Description : 字符串相关的工具
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-03-09 6:00 下午
package util
import (
"crypto/md5"
"encoding/hex"
"math/rand"
"strings"
"time"
"github.com/axgle/mahonia"
)
var (
// LETTER_LIST 字母列表
LETTER_LIST = []string{
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
}
)
// stringOperate ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:09 2022/5/14
type stringOperate struct {
}
// GenRandom 获取随机长度的字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 6:01 下午 2021/3/9
func (s *stringOperate) GenRandom(source string, length uint) string {
if length == 0 {
return ""
}
if len(source) == 0 {
//字符串为空,默认字符源为如下(去除易混淆的i/l):
source = "0123456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ"
}
strByte := []byte(source)
var genStrByte = make([]byte, 0)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < int(length); i++ {
genStrByte = append(genStrByte, strByte[r.Intn(len(strByte))])
}
return string(genStrByte)
}
// Md5 对字符串进行md5加密
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 6:01 下午 2021/3/9
func (s *stringOperate) Md5(str string) string {
h := md5.New()
_, _ = h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
// Md5FromByte 从字节数组计算签名
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022/10/21 14:12:16
func (s *stringOperate) Md5FromByte(data []byte) string {
h := md5.New()
_, _ = h.Write(data)
return hex.EncodeToString(h.Sum(nil))
}
// GenRandomMd5 生成随机md5
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:11 2022/7/12
func (s *stringOperate) GenRandomMd5() string {
return s.Md5(s.GenRandom("", 16))
}
// SnakeCaseToCamel 蛇形字符串转换为驼峰
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:58 下午 2021/10/25
func (s *stringOperate) SnakeCaseToCamel(str string) string {
if len(str) == 0 {
return ""
}
builder := strings.Builder{}
index := 0
if str[0] >= 'a' && str[0] <= 'z' {
builder.WriteByte(str[0] - ('a' - 'A'))
index = 1
}
for i := index; i < len(str); i++ {
if str[i] == '_' && i+1 < len(str) {
if str[i+1] >= 'a' && str[i+1] <= 'z' {
builder.WriteByte(str[i+1] - ('a' - 'A'))
i++
continue
}
}
// 将ID转为大写
if str[i] == 'd' && i-1 >= 0 && (str[i-1] == 'i' || str[i-1] == 'I') && (i+1 == len(str) || i+1 < len(str) && str[i+1] == '_') {
builder.WriteByte('d' - ('a' - 'A'))
continue
}
builder.WriteByte(str[i])
}
return builder.String()
}
// Convert 字符串编码转换
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:38 2022/7/9
func (s *stringOperate) Convert(str string, sourceCode string, targetCode string) string {
sourceCoder := mahonia.NewDecoder(sourceCode)
sourceResult := sourceCoder.ConvertString(str)
targetCoder := mahonia.NewDecoder(targetCode)
_, cdata, _ := targetCoder.Translate([]byte(sourceResult), true)
return string(cdata)
}
// RemoveDuplicates 对列表数据去重
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:12 2022/7/23
func (s *stringOperate) RemoveDuplicates(sourceList []string) []string {
result := make([]string, 0)
hasDeal := make(map[string]bool)
if len(sourceList) == 0 {
return result
}
for _, val := range sourceList {
if _, exist := hasDeal[val]; exist {
continue
}
result = append(result, val)
hasDeal[val] = true
}
return result
}
// Map2Query map参数转换为url query
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:51 2022/10/10
func (s *stringOperate) Map2Query(data map[string]string) string {
list := make([]string, 0)
for k, v := range data {
list = append(list, k+"="+v)
}
return strings.Join(list, "&")
}
// ClearChar 清理指定字符
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:53 2023/1/13
func (s *stringOperate) ClearChar(src string, charList ...string) string {
if len(charList) == 0 {
return src
}
for _, item := range charList {
src = strings.ReplaceAll(src, item, "")
}
return src
}
// ReplaceChineseChar 替换常见的中文符号
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:59 2023/4/4
func (s *stringOperate) ReplaceChineseChar(str string) string {
charTable := map[string]string{
"": "(",
"": ")",
"": ":",
"": ",",
"。": ".",
"【": "]",
"】": "]",
}
for k, v := range charTable {
str = strings.ReplaceAll(str, k, v)
}
return str
}