This repository was archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.go
185 lines (159 loc) · 3.7 KB
/
link.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package core
import (
"crypto/sha256"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/datatogether/sql_datastore"
"github.com/datatogether/sqlutil"
"github.com/ipfs/go-datastore"
"github.com/multiformats/go-multihash"
"time"
)
// A link represents an <a> tag in an html document src who's href
// attribute points to the url that resolves to dst.
// both src & dst must be stored as urls
type Link struct {
// Calculated Hash for fixed ID purposes
Hash string
// created timestamp rounded to seconds in UTC
Created time.Time `json:"created"`
// updated timestamp rounded to seconds in UTC
Updated time.Time `json:"updated"`
// origin url of the linking document
Src *Url `json:"src"`
// absolute url of the <a> href property
Dst *Url `json:"dst"`
}
func (l *Link) DatastoreType() string {
return "Link"
}
func (l *Link) GetId() string {
if l.Hash == "" {
l.calcHash()
}
return l.Hash
}
func (l *Link) Key() datastore.Key {
return datastore.NewKey(fmt.Sprintf("%s:%s", l.DatastoreType(), l.GetId()))
}
func (l *Link) Read(store datastore.Datastore) (err error) {
var li interface{}
if l.Src == nil || l.Dst == nil {
return ErrNotFound
}
// TODO - can't use "store.Get" here b/c we aren't actually storing links by a cannonical ID
if sqlStore, ok := store.(*sql_datastore.Datastore); ok {
row := sqlStore.DB.QueryRow(qLinkRead, l.Src.Url, l.Dst.Url)
return l.UnmarshalSQL(row)
}
li, err = store.Get(l.Key())
if err != nil {
return err
}
got, ok := li.(*Link)
if !ok {
return ErrInvalidResponse
}
*l = *got
return nil
}
func (l *Link) Insert(store datastore.Datastore) error {
l.Created = time.Now().In(time.UTC).Round(time.Second)
l.Updated = l.Created
return store.Put(l.Key(), l)
}
func (l *Link) Update(store datastore.Datastore) error {
l.Updated = time.Now().Round(time.Second)
return store.Put(l.Key(), l)
}
func (l *Link) Delete(store datastore.Datastore) error {
return store.Delete(l.Key())
}
func (l *Link) calcHash() {
h := sha256.New()
data, err := json.Marshal(struct {
Src string `json:"src"`
Dst string `json:"dst"`
}{
Src: l.Src.Url,
Dst: l.Dst.Url,
})
if err != nil {
return
}
h.Write(data)
mhBuf, err := multihash.EncodeName(h.Sum(nil), "sha2-256")
if err != nil {
return
}
l.Hash = hex.EncodeToString(mhBuf)
}
func (l *Link) NewSQLModel(key datastore.Key) sql_datastore.Model {
return &Link{
Hash: key.Name(),
Src: l.Src,
Dst: l.Dst,
}
}
func (l *Link) SQLQuery(cmd sql_datastore.Cmd) string {
switch cmd {
case sql_datastore.CmdCreateTable:
return qLinkCreateTable
case sql_datastore.CmdSelectOne:
return qLinkRead
case sql_datastore.CmdExistsOne:
return qLinkExists
case sql_datastore.CmdInsertOne:
return qLinkInsert
case sql_datastore.CmdDeleteOne:
return qLinkDelete
case sql_datastore.CmdUpdateOne:
return qLinkUpdate
default:
return ""
}
}
func (l *Link) SQLParams(cmd sql_datastore.Cmd) []interface{} {
// TODO remove the need for these
if l.Src == nil {
l.Src = &Url{}
}
if l.Dst == nil {
l.Dst = &Url{}
}
switch cmd {
case sql_datastore.CmdSelectOne, sql_datastore.CmdExistsOne, sql_datastore.CmdDeleteOne:
return []interface{}{
l.Src.Url,
l.Dst.Url,
}
default:
return []interface{}{
l.Created.In(time.UTC),
l.Updated.In(time.UTC),
l.Src.Url,
l.Dst.Url,
}
}
}
func (l *Link) UnmarshalSQL(row sqlutil.Scannable) error {
var (
created, updated time.Time
src, dst string
)
if err := row.Scan(&created, &updated, &src, &dst); err != nil {
if err == sql.ErrNoRows {
return ErrNotFound
}
return err
}
*l = Link{
Created: created.In(time.UTC),
Updated: updated.In(time.UTC),
Src: &Url{Url: src},
Dst: &Url{Url: dst},
}
return nil
}