json_filter/filter_test.go

172 lines
3.1 KiB
Go
Raw Normal View History

2023-01-01 17:31:25 +08:00
// Package filter ...
//
// Description : filter ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-01-01 17:00
package filter
import (
"encoding/json"
2023-01-01 17:31:25 +08:00
"fmt"
"testing"
"github.com/smartystreets/goconvey/convey"
2023-01-01 17:31:25 +08:00
)
// 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",
TargetPath: "user_name",
2023-01-01 17:31:25 +08:00
Required: true,
DataType: "string",
DefaultValue: "",
},
{
SourcePath: "base.age",
TargetPath: "user_age",
2023-01-01 17:31:25 +08:00
Required: true,
DataType: "int",
DefaultValue: "",
},
{
SourcePath: "base.height",
TargetPath: "user_height",
2023-01-01 17:31:25 +08:00
Required: true,
DataType: "string",
DefaultValue: "",
},
{
SourcePath: "company.name",
TargetPath: "company_name",
2023-01-01 17:31:25 +08:00
Required: true,
DataType: "string",
DefaultValue: "",
},
{
SourcePath: "company.start",
TargetPath: "company_start",
2023-01-01 17:31:25 +08:00
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",
TargetPath: "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())
})
}
2023-01-07 23:24:55 +08:00
// 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",
TargetPath: "user.name_list.[]",
2023-01-07 23:24:55 +08:00
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())
})
}
func TestAntMap(t *testing.T) {
testMap := map[any]any{
"name": "zhangde",
1: 1,
1.234: 2.345,
}
byteData, err := json.Marshal(testMap)
fmt.Println(string(byteData), err)
}