增加简单的数据映射能力
This commit is contained in:
parent
d52af7e3f4
commit
ec9271c0f3
30
filter.go
30
filter.go
@ -8,8 +8,11 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Jeffail/gabs"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
// MapRule 映射规则
|
||||
@ -30,7 +33,7 @@ type MapRule struct {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:54 2022/7/4
|
||||
func NewFilter(sourceData []byte, filterRuleList []MapRule) *filter {
|
||||
func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
|
||||
return &filter{
|
||||
sourceData: sourceData,
|
||||
filterRuleList: filterRuleList,
|
||||
@ -44,7 +47,7 @@ func NewFilter(sourceData []byte, filterRuleList []MapRule) *filter {
|
||||
//
|
||||
// Date : 11:58 2022/7/4
|
||||
type filter struct {
|
||||
sourceData []byte
|
||||
sourceData string
|
||||
filterRuleList []MapRule
|
||||
jsonObj *gabs.Container // 生成的json对象实例
|
||||
}
|
||||
@ -55,15 +58,30 @@ type filter struct {
|
||||
//
|
||||
// Date : 11:59 2022/7/4
|
||||
func (f *filter) Deal() ([]byte, error) {
|
||||
return nil, nil
|
||||
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
|
||||
}
|
||||
|
||||
// getDefaultValue 获取默认值
|
||||
// getValue 获取值
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:25 2022/7/4
|
||||
func (f *filter) getDefaultValue(dataType string, defaultValue string) (interface{}, error) {
|
||||
func (f *filter) getValue(dataType string, defaultValue string) (interface{}, error) {
|
||||
switch dataType {
|
||||
case "int8":
|
||||
fallthrough
|
||||
@ -111,6 +129,8 @@ func (f *filter) getDefaultValue(dataType string, defaultValue string) (interfac
|
||||
)
|
||||
err = ConvertAssign(&val, defaultValue)
|
||||
return val, err
|
||||
case "string":
|
||||
return defaultValue, nil
|
||||
default:
|
||||
return nil, errors.New(dataType + " is not support!")
|
||||
}
|
||||
|
57
filter_test.go
Normal file
57
filter_test.go
Normal file
@ -0,0 +1,57 @@
|
||||
// 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)
|
||||
}
|
3
go.mod
3
go.mod
@ -11,4 +11,7 @@ require (
|
||||
require (
|
||||
github.com/Jeffail/gabs v1.4.0 // indirect
|
||||
github.com/go-ini/ini v1.66.6 // indirect
|
||||
github.com/tidwall/gjson v1.14.1 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
)
|
||||
|
6
go.sum
6
go.sum
@ -6,6 +6,12 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
|
||||
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
|
||||
github.com/tidwall/pretty v1.2.0/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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
Loading…
Reference in New Issue
Block a user