优化自动生成json #3

Merged
zhangdeman merged 24 commits from feature/tree into master 2023-05-05 16:10:39 +08:00
2 changed files with 71 additions and 30 deletions
Showing only changes of commit 07f0e77b96 - Show all commits

44
const.go Normal file
View File

@ -0,0 +1,44 @@
// 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 = "float64"
DataTypeFloat = "float"
DataTypeIntSlice = "[]int"
DataTypeUntSlice = "[]uint"
DataTypeNumberSlice = "[]number"
DataTypeFloatSlice = "[]float"
DataTypeBoolSlice = "[]bool"
DataTypeAnySlice = "[]interface"
DataTypeObject = "map[string]interface"
)

View File

@ -64,17 +64,7 @@ func (f *filter) Deal() error {
continue
}
sourceResult := gjson.Get(f.sourceData, rule.SourcePath)
var (
sourceVal string
)
if !sourceResult.Exists() {
// 不存在, 使用默认值
sourceVal = rule.DefaultValue
} else {
sourceVal = sourceResult.String()
}
if formatVal, err = f.getValue(rule.DataType, sourceVal); nil != err {
if formatVal, err = f.getValue(rule.DataType, sourceResult, rule.DefaultValue); nil != err {
return fmt.Errorf("%s = %v can not convert to %s : %s", rule.SourcePath, sourceResult.Value(), rule.DataType, err.Error())
}
if f.formatResult, err = sjson.Set(f.formatResult, rule.MapPath, formatVal); nil != err {
@ -183,56 +173,63 @@ func (f *filter) Parse(receiver interface{}) error {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:25 2022/7/4
func (f *filter) getValue(dataType string, defaultValue string) (interface{}, error) {
func (f *filter) getValue(dataType string, sourceValue gjson.Result, defaultValue string) (interface{}, error) {
sourceValueStr := defaultValue
if sourceValue.Exists() {
sourceValueStr = sourceValue.String()
}
switch dataType {
case "int8":
case DataTypeInt8:
fallthrough
case "int16":
case DataTypeInt16:
fallthrough
case "int32":
case DataTypeInt32:
fallthrough
case "int64":
case DataTypeInt64:
fallthrough
case "int":
case DataTypeInt:
var (
err error
val int64
)
err = util.ConvertAssign(&val, defaultValue)
err = util.ConvertAssign(&val, sourceValueStr)
return val, err
case "uint8":
case DataTypeUint8:
fallthrough
case "uint16":
case DataTypeUint16:
fallthrough
case "uint32":
case DataTypeUint32:
fallthrough
case "uint64":
case DataTypeUint64:
fallthrough
case "uint":
case DataTypeUint:
var (
err error
val uint64
)
err = util.ConvertAssign(&val, defaultValue)
err = util.ConvertAssign(&val, sourceValueStr)
return val, err
case "bool":
case DataTypeBool:
var (
err error
val bool
)
err = util.ConvertAssign(&val, defaultValue)
err = util.ConvertAssign(&val, sourceValueStr)
return val, err
case "float32":
case DataTypeFloat32:
fallthrough
case "float64":
case DataTypeFloat64:
var (
err error
val float64
)
err = util.ConvertAssign(&val, defaultValue)
err = util.ConvertAssign(&val, sourceValueStr)
return val, err
case "string":
case DataTypeString:
return defaultValue, nil
case DataTypeAny:
return sourceValue.Value(), nil
default:
return nil, errors.New(dataType + " is not support!")
}