完成数据过主干逻辑,细节健壮性有待继续完善 #4

Merged
zhangdeman merged 13 commits from feature/data_filter into master 2023-09-02 22:17:14 +08:00
Showing only changes of commit 8fd5b54a0c - Show all commits

View File

@ -11,6 +11,7 @@ import (
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"log/slog"
"strings"
)
@ -18,6 +19,15 @@ const (
virtualRoot = "__virtual__root"
)
// FilterOption 过滤选项
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:59 2023/9/1
type FilterOption struct {
DebugModel bool // 调试模式
}
// FilterDataRule 参数过滤规则
//
// Author : go_developer@163.com<白茶清欢>
@ -35,12 +45,15 @@ type FilterDataRule struct {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022/1/22 9:50 PM
func NewDataFilter(source string, filterRule []*FilterDataRule, debugMode bool) *DataFilter {
func NewDataFilter(source string, filterRule []*FilterDataRule, filterOption *FilterOption) *DataFilter {
if nil == filterOption {
filterOption = &FilterOption{}
}
return &DataFilter{
source: source,
filterRule: filterRule,
rewriteResult: "{}",
debugMode: debugMode,
filterOption: filterOption,
}
}
@ -52,8 +65,8 @@ func NewDataFilter(source string, filterRule []*FilterDataRule, debugMode bool)
type DataFilter struct {
source string
filterRule []*FilterDataRule
rewriteResult string // json数据重写结果
debugMode bool // 调试模式
rewriteResult string // json数据重写结果
filterOption *FilterOption // 过滤选项
}
// Filter 数据过滤
@ -109,3 +122,34 @@ func (df *DataFilter) setKV(rule *FilterDataRule) error {
df.rewriteResult, err = sjson.Set(df.rewriteResult, rule.MapKey, rule.DefaultValue)
return err
}
// logPrint 打印日志
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:00 2023/9/1
func (df *DataFilter) logPrint(level string, msg string, other ...interface{}) {
if !df.filterOption.DebugModel {
// 未开启调试模式
return
}
switch level {
case logLevelFatal:
slog.Error(msg, other...)
case logLevelWarn:
slog.Warn(msg, other...)
case logLevelInfo:
slog.Info(msg, other...)
case logLevelDebug:
slog.Debug(msg, other...)
}
}
// 日志等级定义
const (
logLevelFatal = "FATAL"
logLevelWarn = "WARN"
logLevelInfo = "INFO"
logLevelDebug = "DEBUG"
)