-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdata_test.go
More file actions
133 lines (112 loc) · 2.65 KB
/
data_test.go
File metadata and controls
133 lines (112 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package struct2_test
import (
"fmt"
"os"
"reflect"
"testing"
"time"
"github.com/worldline-go/struct2"
)
type timeCustom struct {
time.Time
}
var _ struct2.Hooker = timeCustom{}
// Struct2Hook hook function for decode struct2.
func (t timeCustom) Struct2Hook() any {
return t.Time
}
// ////////////////////////////////////////////////////////
type ColorGroup struct {
ID int `db:"id"`
Name string `db:"name"`
Colors []string `db:"colors"`
// custom type with implemented Hooker interface
// covertion result to time.Time
Date timeCustom `db:"time"`
// CustomValue will be pointer to int
CustomValue *int `db:"custom_value,ptr2"`
// Inner field will be flatten, so override ID from root
Inner Inner `db:"inner,flatten"`
// Coordinate field will be same struct, not touching
Coordinate Coordinate `db:"coordinate,omitnested"`
// Secret field will be ignored
Secret string `cfg:"-"`
}
type Inner struct {
ID int `db:"id"`
}
type Coordinate struct {
X int `db:"x"`
Y int `db:"y"`
}
type ColorGroupData struct {
Time time.Time
}
func (d ColorGroupData) Data() any {
v := 5
return ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
Date: timeCustom{Time: d.Time},
CustomValue: &v,
Inner: Inner{
ID: 2,
},
Coordinate: Coordinate{
X: 1,
Y: 2,
},
Secret: "secret",
}
}
func (d ColorGroupData) MapData() any {
return map[string]any{
"id": 2,
"name": "Reds",
"colors": []string{"Crimson", "Red", "Ruby", "Maroon"},
"time": d.Time,
"custom_value": 5,
"coordinate": Coordinate{
X: 1,
Y: 2,
},
}
}
// defination of example data
type exampleData interface {
Data() any
MapData() any
}
var mapExamples = map[string]exampleData{
"ColorGroup": ColorGroupData{
Time: time.Now().Truncate(time.Second),
},
}
// run specific DATA by name
func Test_Data(t *testing.T) {
decoder := struct2.Decoder{
TagName: "db",
BackupTagName: "cfg",
WeaklyDashUnderscore: true,
WeaklyIgnoreSeperator: true,
WeaklyTypedInput: true,
}
example := os.Getenv("DATA")
if example == "" {
t.Skip("DATA env variable is empty")
}
mapper, ok := mapExamples[example]
if !ok {
t.Fatalf("example %s not found", example)
}
inputData := mapper.Data()
outputData := decoder.Map(inputData)
correctData := mapper.MapData()
fmt.Printf("\n# input\n%#v\n", inputData)
fmt.Printf("\n# output\n%#v\n", outputData)
fmt.Printf("\n# correction\n%#v\n", correctData)
if !reflect.DeepEqual(outputData, correctData) {
t.Fatalf("output data not equal to correct data %v != %v", outputData, correctData)
}
}