-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
57 lines (46 loc) · 1.18 KB
/
example_test.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
package stom_test
import (
"fmt"
"github.com/elgris/stom"
)
type SomeGrandparentStruct struct {
GrandparentID int `db:"grand_parent_id"`
}
type SomeParentStruct struct {
SomeGrandparentStruct
ParentID int `db:"parent_id"`
}
// Yes, SToM supports embedded structs.
type SomeAwesomeStruct struct {
SomeParentStruct
ID int `db:"id" custom_tag:"id"`
Name string `db:"name"`
AbstractThing interface{} `db:"thing"`
Notes string
}
func Example() {
s := SomeAwesomeStruct{
ID: 123,
Name: "myname",
Notes: "mynote",
}
s.ParentID = 1123
s.GrandparentID = 11123
converter := stom.MustNewStom(s).
SetTag("db").
SetPolicy(stom.PolicyExclude).
SetDefault("DEFAULT")
/* you will get map:
"grand_parent_id": 11123,
"parent_id": 1123,
"id": 123,
"name": "myname",
"thing": "DEFAULT"
Field "Notes" is ignored as it has no tag.
Field "AbstractThing" is nil, so it replaced with default value.
Current policy demands to use default instead of nil values.
All embedded structs are flattened into flat map.
*/
m, err := converter.ToMap(s)
fmt.Printf("MAP: %+v\nERROR: %v", m, err)
}