161 lines
2.9 KiB
Go
161 lines
2.9 KiB
Go
// Package filter ...
|
|
//
|
|
// Description : filter ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2023-01-01 17:00
|
|
package filter
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/smartystreets/goconvey/convey"
|
|
)
|
|
|
|
// TestNewFilter ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 17:00 2023/1/1
|
|
func TestNewFilter(t *testing.T) {
|
|
var (
|
|
err error
|
|
)
|
|
testData := `{
|
|
"base":{
|
|
"name":"go_developer",
|
|
"age":18,
|
|
"height":180
|
|
},
|
|
"company":{
|
|
"name":"wajijiwa",
|
|
"start":"2023-01-01"
|
|
}
|
|
}`
|
|
filterRuleList := []MapRule{
|
|
{
|
|
SourcePath: "base.name",
|
|
MapPath: "user_name",
|
|
Required: true,
|
|
DataType: "string",
|
|
DefaultValue: "",
|
|
},
|
|
{
|
|
SourcePath: "base.age",
|
|
MapPath: "user_age",
|
|
Required: true,
|
|
DataType: "int",
|
|
DefaultValue: "",
|
|
},
|
|
{
|
|
SourcePath: "base.height",
|
|
MapPath: "user_height",
|
|
Required: true,
|
|
DataType: "string",
|
|
DefaultValue: "",
|
|
},
|
|
{
|
|
SourcePath: "company.name",
|
|
MapPath: "company_name",
|
|
Required: true,
|
|
DataType: "string",
|
|
DefaultValue: "",
|
|
},
|
|
{
|
|
SourcePath: "company.start",
|
|
MapPath: "company_start",
|
|
Required: true,
|
|
DataType: "string",
|
|
DefaultValue: "",
|
|
},
|
|
}
|
|
f := NewFilter(testData, filterRuleList)
|
|
convey.Convey("基础对象重写", t, func() {
|
|
err = f.Deal()
|
|
convey.So(err, convey.ShouldEqual, nil)
|
|
fmt.Println(f.String())
|
|
})
|
|
}
|
|
|
|
// TestNewFilterForArrayOne ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 21:43 2023/1/2
|
|
func TestNewFilterForArrayOne(t *testing.T) {
|
|
var (
|
|
err error
|
|
)
|
|
testData := `[
|
|
{
|
|
"name":"zhangdeman"
|
|
},
|
|
{
|
|
"name":"zhang"
|
|
},
|
|
{
|
|
"name":"de"
|
|
},
|
|
{
|
|
"name":"man"
|
|
}
|
|
]`
|
|
filterRuleList := []MapRule{
|
|
{
|
|
SourcePath: "[].name",
|
|
MapPath: "user_name.[]",
|
|
Required: true,
|
|
DataType: "string",
|
|
DefaultValue: "",
|
|
},
|
|
}
|
|
f := NewFilter(testData, filterRuleList)
|
|
convey.Convey("提取列表字段,构成新的列表", t, func() {
|
|
err = f.Deal()
|
|
convey.So(err, convey.ShouldEqual, nil)
|
|
fmt.Println(f.String())
|
|
})
|
|
}
|
|
|
|
// TestNewFilterForArrayTwo ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 22:22 2023/1/2
|
|
func TestNewFilterForArrayTwo(t *testing.T) {
|
|
var (
|
|
err error
|
|
)
|
|
testData := `{"user_list":[
|
|
{
|
|
"name":"zhangdeman1"
|
|
},
|
|
{
|
|
"name":"zhang1"
|
|
},
|
|
{
|
|
"name":"de1"
|
|
},
|
|
{
|
|
"name":"man1"
|
|
}
|
|
]}`
|
|
filterRuleList := []MapRule{
|
|
{
|
|
SourcePath: "user_list.[].name",
|
|
MapPath: "user.name_list.[]",
|
|
Required: true,
|
|
DataType: "string",
|
|
DefaultValue: "",
|
|
},
|
|
}
|
|
f := NewFilter(testData, filterRuleList)
|
|
convey.Convey("提取列表字段,构成新的列表", t, func() {
|
|
err = f.Deal()
|
|
convey.So(err, convey.ShouldEqual, nil)
|
|
fmt.Println(f.String())
|
|
})
|
|
}
|