-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (50 loc) · 1.4 KB
/
main.go
File metadata and controls
60 lines (50 loc) · 1.4 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
// Package main example on how to use gofieldselect.GetValue to transform a DAO object to a DTO with field selection.
package main
import (
"fmt"
"examples"
"github.com/golaxo/gofieldselect"
)
type (
userDao struct {
Name string `json:"name"`
Surname string `json:"surname"`
Age int `json:"age"`
Address addressDao `json:"address"`
}
addressDao struct {
Street string `json:"street"`
Number int `json:"number"`
}
)
func main() {
user := userDao{
Name: "John",
Surname: "Doe",
Age: 18,
Address: addressDao{
Street: "Example",
Number: 1,
},
}
fieldSelection, err := gofieldselect.Parse("name,address(street)")
if err != nil {
panic(err)
}
var address *examples.Address
addressFieldSelectionNode, ok := fieldSelection.SelectField("address")
if ok {
address = &examples.Address{
Street: gofieldselect.Get(addressFieldSelectionNode.Child, "street", user.Address.Street),
Number: gofieldselect.Get(addressFieldSelectionNode.Child, "number", user.Address.Number),
}
}
dto := examples.User{
Name: gofieldselect.Get(fieldSelection, "name", user.Name),
Surname: gofieldselect.Get(fieldSelection, "surname", user.Surname),
Age: gofieldselect.Get(fieldSelection, "age", user.Age),
Address: address,
}
fmt.Printf("Source: \n %+v\n", examples.ToIndentedJson(user))
fmt.Printf("Selected: \n%+v\n", examples.ToIndentedJson(dto))
}