-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.go
56 lines (45 loc) · 1.26 KB
/
regex.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
package main
import (
"fmt"
"os"
"github.com/0xrawsec/sod"
)
type Person struct {
sod.Item
FirstName string
LastName string
Age int
}
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 insertOrPanic(db *sod.DB, p *Person) {
if err := db.InsertOrUpdate(p); err != nil {
panic(err)
}
}
func main() {
dbpath := "./data/database"
os.RemoveAll(dbpath)
db := sod.Open(dbpath)
// We need to create a directory and a schema to store Person structures
if err := db.Create(&Person{}, sod.DefaultSchema); err != nil {
panic(err)
}
insertOrPanic(db, &Person{FirstName: "John", LastName: "Lennon", Age: 42})
insertOrPanic(db, &Person{FirstName: "Johnny", LastName: "Cash", Age: 71})
insertOrPanic(db, &Person{FirstName: "Joe", LastName: "Dassin", Age: 41})
fmt.Println("Printing Persons with a first name starting matching regex")
printSearchResult(db.Search(&Person{}, "FirstName", "~=", "^(?i:john.*)"))
fmt.Println("Printing Persons with a first name starting with J")
printSearchResult(db.Search(&Person{}, "FirstName", "~=", "^J"))
}