json_filter/filter_test.go

172 lines
3.1 KiB
Go

// Package filter ...
//
// Description : filter ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-01-01 17:00
package filter
import (
"encoding/json"
"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",
TargetPath: "user_name",
Required: true,
DataType: "string",
DefaultValue: "",
},
{
SourcePath: "base.age",
TargetPath: "user_age",
Required: true,
DataType: "int",
DefaultValue: "",
},
{
SourcePath: "base.height",
TargetPath: "user_height",
Required: true,
DataType: "string",
DefaultValue: "",
},
{
SourcePath: "company.name",
TargetPath: "company_name",
Required: true,
DataType: "string",
DefaultValue: "",
},
{
SourcePath: "company.start",
TargetPath: "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",
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())
})
}
// 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.[]",
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)
}