-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
91 lines (77 loc) · 2.2 KB
/
main.go
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
package main
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/ifaceless/portal"
"github.com/ifaceless/portal/_examples/todo/model"
"github.com/ifaceless/portal/_examples/todo/schema"
)
func main() {
start := time.Now()
defer portal.CleanUp()
portal.SetMaxPoolSize(1024)
portal.SetDebug(true)
task := model.TaskModel{
ID: 1,
UserID: 1,
Title: "Finish your jobs.",
}
printFullFields(&task)
printWithOnlyFields(&task, "Unknown")
printWithOnlyFields(&task, "ID", "User[id,Notifications[ID],AnotherNotifications[Title]]", "simple_user[id]")
printMany()
printWithExcludeFields(&task, "Description", "ID", "User[Name,Notifications[ID,Content],AnotherNotifications], SimpleUser")
fmt.Printf("elapsed: %.1f ms\n", time.Since(start).Seconds()*1000)
}
func printFullFields(task *model.TaskModel) {
var taskSchema *schema.TaskSchema
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
err := portal.DumpWithContext(ctx, &taskSchema, task, portal.CustomFieldTagMap(map[string]string{
"TaskSchema.Title": "const:hello",
"UserSchema.Name": "const:xiaoming",
}))
if err != nil {
fmt.Printf("%++v", err)
return
}
data, _ := json.Marshal(taskSchema)
fmt.Println(string(data))
}
func printWithOnlyFields(task *model.TaskModel, fields ...string) {
var taskSchema schema.TaskSchema
err := portal.Dump(&taskSchema, task, portal.Only(fields...))
if err != nil {
panic(err)
}
data, _ := json.Marshal(taskSchema)
fmt.Println(string(data))
}
func printWithExcludeFields(task *model.TaskModel, fields ...string) {
var taskSchema schema.TaskSchema
err := portal.Dump(&taskSchema, task, portal.Exclude(fields...))
if err != nil {
panic(err)
}
data, _ := json.Marshal(taskSchema)
fmt.Println(string(data))
}
func printMany() {
var taskSchemas []schema.TaskSchema
tasks := make([]*model.TaskModel, 0)
for i := 0; i < 10; i++ {
tasks = append(tasks, &model.TaskModel{
ID: i,
UserID: i + 100,
Title: fmt.Sprintf("Task #%d", i+1),
})
}
err := portal.Dump(&taskSchemas, &tasks, portal.Only("ID", "Title", "User[Name]", "Description"))
if err != nil {
panic(err)
}
data, _ := json.Marshal(taskSchemas)
fmt.Println(string(data))
}