-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
274 lines (241 loc) · 9.06 KB
/
repository.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package repository
import (
"fmt"
"strings"
"github.com/danielbaenabird/fyne/v2"
)
// repositoryTable stores the mapping of schemes to Repository implementations.
// It should only ever be used by ForURI() and Register().
var repositoryTable map[string]Repository = map[string]Repository{}
// Repository represents a storage repository, which is a set of methods which
// implement specific functions on a URI. Repositories are registered to handle
// specific URI schemes, and the higher-level functions that operate on URIs
// internally look up an appropriate method from the relevant Repository.
//
// The repository interface includes only methods which must be implemented at
// a minimum. Without implementing all of the methods in this interface, a URI
// would not be usable in a useful way. Additional functionality can be exposed
// by using interfaces which extend Repository.
//
// Repositories are registered to handle a specific URI scheme (or schemes)
// using the Register() method. When a higher-level URI function such as
// storage.Copy() is called, the storage package will internally look up
// the repository associated with the scheme of the URI, then it will use
// a type assertion to check if the repository implements CopyableRepository.
// If so, the Copy() function will be run from the repository, otherwise
// storage.Copy() will return NotSupportedError. This works similarly for
// all other methods in repository-related interfaces.
//
// Note that a repository can be registered for multiple URI schemes. In such
// cases, the repository must internally select and implement the correct
// behavior for each URI scheme.
//
// A repository will only ever need to handle URIs with schemes for which it
// was registered, with the exception that functions with more than 1 operand
// such as Copy() and Move(), in which cases only the first operand is
// guaranteed to match a scheme for which the repository is registered.
//
// NOTE: most developers who use Fyne should *not* generally attempt to
// call repository methods directly. You should use the methods in the storage
// package, which will automatically detect the scheme of a URI and call into
// the appropriate repository.
//
// Since: 2.0
type Repository interface {
// Exists will be used to implement calls to storage.Exists() for the
// registered scheme of this repository.
//
// Since: 2.0
Exists(u fyne.URI) (bool, error)
// Reader will be used to implement calls to storage.Reader()
// for the registered scheme of this repository.
//
// Since: 2.0
Reader(u fyne.URI) (fyne.URIReadCloser, error)
// CanRead will be used to implement calls to storage.CanRead() for the
// registered scheme of this repository.
//
// Since: 2.0
CanRead(u fyne.URI) (bool, error)
// Destroy is called when the repository is un-registered from a given
// URI scheme.
//
// The string parameter will be the URI scheme that the repository was
// registered for. This may be useful for repositories that need to
// handle more than one URI scheme internally.
//
// Since: 2.0
Destroy(string)
}
// CustomURIRepository is an extension of the repository interface which
// allows the behavior of storage.ParseURI to be overridden. This is only
// needed if you wish to generate custom URI types, rather than using Fyne's
// URI implementation and net/url based parsing.
//
// NOTE: even for URIs with non-RFC3986-compliant encoding, the URI MUST begin
// with 'scheme:', or storage.ParseURI() will not be able to determine which
// storage repository to delegate to for parsing.
//
// Since: 2.0
type CustomURIRepository interface {
Repository
// ParseURI will be used to implement calls to storage.ParseURI()
// for the registered scheme of this repository.
ParseURI(string) (fyne.URI, error)
}
// WritableRepository is an extension of the Repository interface which also
// supports obtaining a writer for URIs of the scheme it is registered to.
//
// Since: 2.0
type WritableRepository interface {
Repository
// Writer will be used to implement calls to storage.WriterTo() for
// the registered scheme of this repository.
//
// Since: 2.0
Writer(u fyne.URI) (fyne.URIWriteCloser, error)
// CanWrite will be used to implement calls to storage.CanWrite() for
// the registered scheme of this repository.
//
// Since: 2.0
CanWrite(u fyne.URI) (bool, error)
// Delete will be used to implement calls to storage.Delete() for the
// registered scheme of this repository.
//
// Since: 2.0
Delete(u fyne.URI) error
}
// ListableRepository is an extension of the Repository interface which also
// supports obtaining directory listings (generally analogous to a directory
// listing) for URIs of the scheme it is registered to.
//
// Since: 2.0
type ListableRepository interface {
Repository
// CanList will be used to implement calls to storage.Listable() for
// the registered scheme of this repository.
//
// Since: 2.0
CanList(u fyne.URI) (bool, error)
// List will be used to implement calls to storage.List() for the
// registered scheme of this repository.
//
// Since: 2.0
List(u fyne.URI) ([]fyne.URI, error)
// CreateListable will be used to implement calls to
// storage.CreateListable() for the registered scheme of this
// repository.
//
// Since: 2.0
CreateListable(u fyne.URI) error
}
// HierarchicalRepository is an extension of the Repository interface which
// also supports determining the parent and child items of a URI.
//
// Since: 2.0
type HierarchicalRepository interface {
Repository
// Parent will be used to implement calls to storage.Parent() for the
// registered scheme of this repository.
//
// A generic implementation is provided in GenericParent(), which
// is based on the RFC3986 definition of a URI parent.
//
// Since: 2.0
Parent(fyne.URI) (fyne.URI, error)
// Child will be used to implement calls to storage.Child() for
// the registered scheme of this repository.
//
// A generic implementation is provided in GenericParent(), which
// is based on RFC3986.
//
// Since: 2.0
Child(fyne.URI, string) (fyne.URI, error)
}
// CopyableRepository is an extension of the Repository interface which also
// supports copying referenced resources from one URI to another.
//
// Since: 2.0
type CopyableRepository interface {
Repository
// Copy will be used to implement calls to storage.Copy() for the
// registered scheme of this repository.
//
// A generic implementation is provided by GenericCopy().
//
// NOTE: the first parameter is the source, the second is the
// destination.
//
// NOTE: if storage.Copy() is given two URIs of different schemes, it
// is possible that only the source URI will be of the type this
// repository is registered to handle. In such cases, implementations
// are suggested to fail-over to GenericCopy().
//
// Since: 2.0
Copy(fyne.URI, fyne.URI) error
}
// MovableRepository is an extension of the Repository interface which also
// supports moving referenced resources from one URI to another.
//
// Note: both Moveable and Movable are correct spellings, but Movable is newer
// and more accepted. Source: https://grammarist.com/spelling/movable-moveable/
//
// Since: 2.0
type MovableRepository interface {
Repository
// Move will be used to implement calls to storage.Move() for the
// registered scheme of this repository.
//
// A generic implementation is provided by GenericMove().
//
// NOTE: the first parameter is the source, the second is the
// destination.
//
// NOTE: if storage.Move() is given two URIs of different schemes, it
// is possible that only the source URI will be of the type this
// repository is registered to handle. In such cases, implementations
// are suggested to fail-over to GenericMove().
//
// Since: 2.0
Move(fyne.URI, fyne.URI) error
}
// Register registers a storage repository so that operations on URIs of the
// registered scheme will use methods implemented by the relevant repository
// implementation.
//
// Since: 2.0
func Register(scheme string, repository Repository) {
scheme = strings.ToLower(scheme)
prev, ok := repositoryTable[scheme]
if ok {
prev.Destroy(scheme)
}
repositoryTable[scheme] = repository
}
// ForURI returns the Repository instance which is registered to handle URIs of
// the given scheme. This is a helper method that calls ForScheme() on the
// scheme of the given URI.
//
// NOTE: this function is intended to be used specifically by the storage
// package. It generally should not be used outside of the fyne package -
// instead you should use the methods in the storage package.
//
// Since: 2.0
func ForURI(u fyne.URI) (Repository, error) {
return ForScheme(u.Scheme())
}
// ForScheme returns the Repository instance which is registered to handle URIs
// of the given scheme.
//
// NOTE: this function is intended to be used specifically by the storage
// package. It generally should not be used outside of the fyne package -
// instead you should use the methods in the storage package.
//
// Since: 2.0
func ForScheme(scheme string) (Repository, error) {
repo, ok := repositoryTable[scheme]
if !ok {
return nil, fmt.Errorf("no repository registered for scheme '%s'", scheme)
}
return repo, nil
}