优化自动生成json #3

Merged
zhangdeman merged 24 commits from feature/tree into master 2023-05-05 16:10:39 +08:00
4 changed files with 46 additions and 96 deletions
Showing only changes of commit 3487a87104 - Show all commits

View File

@ -1,44 +0,0 @@
// Package filter ...
//
// Description : filter ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-04 17:29
package filter
const (
DataTypeAny = "interface"
DataTypeString = "string"
DataTypeInt8 = "int8"
DataTypeInt16 = "int16"
DataTypeInt32 = "int32"
DataTypeInt64 = "int64"
DataTypeInt = "int"
DataTypeUint8 = "uint8"
DataTypeUint16 = "uint16"
DataTypeUint32 = "uint32"
DataTypeUint64 = "uint64"
DataTypeUint = "uint"
DataTypeBool = "bool"
DataTypeNumber = "number"
DataTypeFloat32 = "float32"
DataTypeFloat64 = "float64"
DataTypeDouble = "double"
DataTypeFloat = "float"
DataTypeIntSlice = "[]int"
DataTypeUntSlice = "[]uint"
DataTypeNumberSlice = "[]number"
DataTypeFloatSlice = "[]float"
DataTypeBoolSlice = "[]bool"
DataTypeAnySlice = "[]interface"
DataTypeObject = "map[string]interface"
)

View File

@ -10,14 +10,13 @@ package filter
import (
"encoding/json"
"fmt"
"git.zhangdeman.cn/zhangdeman/wrapper"
"strings"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"errors"
"git.zhangdeman.cn/zhangdeman/util"
)
// NewFilter 过滤器实例
@ -182,61 +181,51 @@ func (f *filter) getValue(dataType string, sourceValue gjson.Result, defaultValu
}
}
strVal := wrapper.String(sourceValueStr)
switch dataType {
case DataTypeInt8:
return util.StringConvert.ToInt8(sourceValueStr)
case DataTypeInt16:
return util.StringConvert.ToInt16(sourceValueStr)
case DataTypeInt32:
return util.StringConvert.ToInt32(sourceValueStr)
case DataTypeInt64:
return util.StringConvert.ToInt64(sourceValueStr)
case DataTypeInt:
return util.StringConvert.ToInt(sourceValueStr)
case DataTypeUint8:
return util.StringConvert.ToUint8(sourceValueStr)
case DataTypeUint16:
return util.StringConvert.ToUint16(sourceValueStr)
case DataTypeUint32:
return util.StringConvert.ToUint32(sourceValueStr)
case DataTypeUint64:
return util.StringConvert.ToUint64(sourceValueStr)
case DataTypeUint:
return util.StringConvert.ToUint(sourceValueStr)
case DataTypeBool:
return util.StringConvert.ToBool(sourceValueStr)
case DataTypeFloat32:
return util.StringConvert.ToFloat32(sourceValueStr)
case DataTypeFloat64:
return util.StringConvert.ToFloat64(sourceValueStr)
case DataTypeFloat:
return util.StringConvert.ToFloat64(sourceValueStr)
case DataTypeDouble:
return util.StringConvert.ToDouble(sourceValueStr)
case DataTypeNumber:
return util.StringConvert.ToNumber(sourceValueStr)
case DataTypeString:
case wrapper.DataTypeInt8:
return strVal.ToInt8()
case wrapper.DataTypeInt16:
return strVal.ToInt16()
case wrapper.DataTypeInt32:
return strVal.ToInt32()
case wrapper.DataTypeInt64:
return strVal.ToInt64()
case wrapper.DataTypeInt:
return strVal.ToInt()
case wrapper.DataTypeUint8:
return strVal.ToUint8()
case wrapper.DataTypeUint16:
return strVal.ToUint16()
case wrapper.DataTypeUint32:
return strVal.ToUint32()
case wrapper.DataTypeUint64:
return strVal.ToUint64()
case wrapper.DataTypeUint:
return strVal.ToUint()
case wrapper.DataTypeBool:
return strVal.ToBool()
case wrapper.DataTypeFloat32:
return strVal.ToFloat64()
case wrapper.DataTypeFloat64:
fallthrough
case wrapper.DataTypeFloat:
fallthrough
case wrapper.DataTypeDouble:
return strVal.ToFloat64()
case wrapper.DataTypeNumber:
return strVal.ToNumber()
case wrapper.DataTypeString:
return sourceValueStr, nil
case DataTypeAny:
case wrapper.DataTypeAny:
return sourceValue.Value(), nil
case DataTypeAnySlice:
case wrapper.DataTypeAnySlice:
// 任意类型的list
var (
result []interface{}
)
if err := util.JSON.UnmarshalWithNumber([]byte(sourceValueStr), &result); nil != err {
return nil, err
}
return result, nil
case DataTypeObject:
return strVal.ToAnySlice()
case wrapper.DataTypeObject:
// object
var (
result map[string]interface{}
)
if err := util.JSON.UnmarshalWithNumber([]byte(sourceValueStr), &result); nil != err {
return nil, err
}
return result, nil
return strVal.ToObject()
default:
return nil, errors.New(dataType + " is not support!")
}

1
go.mod
View File

@ -13,6 +13,7 @@ require (
)
require (
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230505070245-b182b21e039b // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect

4
go.sum
View File

@ -9,6 +9,10 @@ git.zhangdeman.cn/zhangdeman/util v0.0.0-20230330082619-662152cb682d h1:kMQZmkYB
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230330082619-662152cb682d/go.mod h1:qeVsrMae8ljqzcsmI+lWPU/4Rdjb9cOt4oaDUNEf1Ck=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230505025924-96532aff0019 h1:eJ/9rEj2iI8P9I1DfCmMUvsV+n2EiAWCXnI9yVVDHO0=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230505025924-96532aff0019/go.mod h1:z2bP5LIwRVpWSQV0/a3WIFaoarJUP8kA/0Clv0bP+8I=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230505064614-5e785171ed67 h1:DH9K3fNddpFxRGLkcLP5MHsAQVinpWpmGzbVBf8yrKM=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230505064614-5e785171ed67/go.mod h1:2jc48WuVoHxZjkvlBewzp+ey8khP1K4OOcibVD1yL2k=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230505070245-b182b21e039b h1:ZwAA10/+v3FFAq5/EzjXdXDodmKppXb5gBkCcgaYVBo=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230505070245-b182b21e039b/go.mod h1:2jc48WuVoHxZjkvlBewzp+ey8khP1K4OOcibVD1yL2k=
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=