util/filter.go

118 lines
2.3 KiB
Go
Raw Normal View History

2022-07-04 11:58:34 +08:00
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-07-04 11:45
package util
2022-07-04 12:30:53 +08:00
import (
"github.com/Jeffail/gabs"
"github.com/pkg/errors"
)
2022-07-04 12:26:02 +08:00
// 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
}
2022-07-04 11:58:34 +08:00
// NewFilter 过滤器实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:54 2022/7/4
2022-07-04 12:26:02 +08:00
func NewFilter(sourceData []byte, filterRuleList []MapRule) *filter {
2022-07-04 11:58:34 +08:00
return &filter{
2022-07-04 12:26:02 +08:00
sourceData: sourceData,
filterRuleList: filterRuleList,
jsonObj: &gabs.Container{},
2022-07-04 11:58:34 +08:00
}
}
// filter 数据过滤
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:58 2022/7/4
type filter struct {
2022-07-04 12:26:02 +08:00
sourceData []byte
filterRuleList []MapRule
jsonObj *gabs.Container // 生成的json对象实例
}
// Deal ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:59 2022/7/4
func (f *filter) Deal() ([]byte, error) {
return nil, nil
}
// getDefaultValue 获取默认值
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:25 2022/7/4
func (f *filter) getDefaultValue(dataType string, defaultValue string) (interface{}, error) {
2022-07-04 12:30:53 +08:00
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
default:
return nil, errors.New(dataType + " is not support!")
}
2022-07-04 11:58:34 +08:00
}