-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddresses.go
79 lines (68 loc) · 1.66 KB
/
addresses.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
package entities
import (
"errors"
"fmt"
"regexp"
)
var _ = fmt.Println // remove after test
var r1 = regexp.MustCompile(`^(?P<Zip>[0-9]{5,5})$`)
type Address struct {
Key *Key
AddressLine string
Location *Location
PostalCode string
}
func NewAddress(a string, l *Location, c string) (*Address,error) {
p := new(Address)
p.Key = makeKey("Address")
p.AddressLine = a
p.Location = l
u := r1.FindStringSubmatch(c)
if len(u) == 2 {
p.PostalCode = c
return p,nil
} else {
err := errors.New("Invalid postal code: " + c)
return nil,err
}
}
func (a *Address) Triples () [][3]string {
var t [][3]string
t = append(t, makeTriple(Triple{(*a).Key,"hasType","Address",nil}))
t = append(t, makeTriple(Triple{(*a).Key,"hasAddressLine",(*a).AddressLine,nil}))
if (*a).Location != nil { t=append(t, makeTriple(Triple{(*a).Key,"hasLocation","",(*a.Location).Key}))}
if (*a).PostalCode != "" { t=append(t, makeTriple(Triple{(*a).Key,"hasPostalCode",(*a).PostalCode,nil}))}
return t
}
func (a *Address) Row() []string {
var t []string
t = append(t, a.Key.s)
t = append(t, a.AddressLine)
if (*a).Location != nil { t=append(t, a.Location.Key.s)}
if (*a).PostalCode != "" { t=append(t, a.PostalCode)}
return t
}
func FindAddressKey (kf *Key) int {
for i,a := range Addresses {
if kf.s == a.Key.s {
return i
}
}
return -1
}
func AddAddressFact(a []string) {
key := new(Key)
key.s = a[0]
i := FindAddressKey(key)
switch a[1] {
case "AddressLine":
Addresses[i].AddressLine = a[2]
case "Location":
key.s = a[2]
j := FindLocationKey(key)
Addresses[i].Location = Locations[j]
case "PostalCode":
Addresses[i].PostalCode = a[2]
}
}
var Addresses []*Address