-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.go
72 lines (63 loc) · 1.5 KB
/
source.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
package vegagoja
import (
"io/fs"
"os"
"strings"
)
// Source is a cascading [fs.FS] implementation.
type Source struct {
sources []fs.FS
}
// NewSource creates a source data source for the supplied file systems.
func NewSource(sources ...fs.FS) fs.FS {
if len(sources) == 1 {
return sources[0]
}
return &Source{
sources: sources,
}
}
// Open satisfies the [fs.FS] interface.
func (s *Source) Open(name string) (fs.File, error) {
for _, source := range s.sources {
if file, err := source.Open(name); err == nil {
return file, nil
}
}
return nil, os.ErrNotExist
}
// PrefixedSource is a prefixed source.
type PrefixedSource struct {
prefix string
source fs.FS
}
// NewPrefixedSource creates a prefixed source.
func NewPrefixedSource(prefix string, source fs.FS) *PrefixedSource {
return &PrefixedSource{
prefix: prefix,
source: source,
}
}
// NewPrefixedSourceDir creates a prefixed source for the specified directory.
func NewPrefixedSourceDir(prefix, dir string) *PrefixedSource {
return &PrefixedSource{
prefix: prefix,
source: os.DirFS(dir),
}
}
// Open satisfies the [fs.FS] interface.
func (s *PrefixedSource) Open(name string) (fs.File, error) {
if !strings.HasPrefix(name, s.prefix) {
return nil, os.ErrNotExist
}
return s.source.Open(strings.TrimPrefix(name, s.prefix))
}
// ResultSet is the shared interface for a result set.
type ResultSet interface {
Next() bool
Scan(...interface{}) error
Columns() ([]string, error)
Close() error
Err() error
NextResultSet() bool
}