-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (84 loc) · 2.57 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
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"os"
"github.com/0xrawsec/sod"
)
// We make the assumption ForeignStruct is
// defined in an imported package. In this case
// we cannot edit the struct to make it a sod.Item
// and set the approriate sod tags
type ForeignStruct struct {
FirstName string
LastName string
Age int
}
// An embedded structure needs to be defined in the
// package you are actually writting
type Person struct {
sod.Item
ForeignStruct
// you can even add new fields if you like
Country string `sod:"index,upper"`
}
func NewLocalPerson(first, last string, age int, country string) Person {
return Person{ForeignStruct: ForeignStruct{FirstName: first, LastName: last, Age: age}, Country: country}
}
func printSearchResult(s *sod.Search) {
var persons []*Person
if err := s.Assign(&persons); err != nil {
panic(err)
} else {
fmt.Printf("Search brought %d results\n", len(persons))
for _, person := range persons {
fmt.Println(person)
}
fmt.Println()
}
}
func main() {
dbpath := "./data/database"
os.RemoveAll(dbpath)
db := sod.Open(dbpath)
defer db.Close()
// getting FieldDescritpors from Object we defined
descriptors := sod.FieldDescriptors(&Person{})
// we make a field of the foreign structure an indexed field
if err := descriptors.Constraint("ForeignStruct.FirstName", sod.Constraints{Index: true}); err != nil {
panic(err)
}
customSchema := sod.NewCustomSchema(descriptors, sod.DefaultExtension)
// We need to create a directory and a schema to store Person structures
if err := db.Create(&Person{}, customSchema); err != nil {
panic(err)
}
john := NewLocalPerson("John", "Doe", 42, "us")
// insert person in the db
if err := db.InsertOrUpdate(&john); err != nil {
panic(err)
}
connor := NewLocalPerson("John", "Connor", 10, "us")
// insert person in the db
if err := db.InsertOrUpdate(&connor); err != nil {
panic(err)
}
lennon := NewLocalPerson("John", "Lennon", 40, "uk")
// insert person in the db
if err := db.InsertOrUpdate(&lennon); err != nil {
panic(err)
}
fmt.Println("Showing indexed fields")
s, err := db.Schema(&Person{})
if err != nil {
panic(err)
}
for _, fd := range s.Indexed() {
fmt.Println(fd)
}
fmt.Println()
printSearchResult(db.Search(&Person{}, "Age", ">=", 40))
printSearchResult(db.Search(&Person{}, "FirstName", "=", "John").And("Age", "<", 42))
printSearchResult(db.Search(&Person{}, "LastName", "=", "Connor").Or("Age", "<", 128))
printSearchResult(db.Search(&Person{}, "LastName", "=", "Connor").Or("LastName", "=", "Doe"))
printSearchResult(db.Search(&Person{}, "Country", "=", "us"))
}