-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_util_fs_test.go
86 lines (73 loc) · 1.49 KB
/
example_util_fs_test.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
package temple_test
import (
"io"
"io/fs"
"time"
)
type staticFS map[string]string
// Open opens the named file.
// When Open returns an error, it should be of type *PathError
// with the Op field set to "open", the Path field set to name,
// and the Err field describing the problem.
//
// Open should reject attempts to open names that do not satisfy
// ValidPath(name), returning a *PathError with Err set to
// ErrInvalid or ErrNotExist.
func (s staticFS) Open(name string) (fs.File, error) {
val, ok := s[name]
if !ok {
return nil, &fs.PathError{
Op: "open",
Path: name,
Err: fs.ErrNotExist,
}
}
return &staticFile{
name: name,
contents: []byte(val),
}, nil
}
type staticFile struct {
name string
contents []byte
offset int
}
func (s *staticFile) Stat() (fs.FileInfo, error) {
return s, nil
}
func (s *staticFile) Read(buf []byte) (int, error) {
if s.offset >= len(s.contents) {
return 0, io.EOF
}
if s.offset < 0 {
return 0, &fs.PathError{
Op: "read",
Path: s.name,
Err: fs.ErrInvalid,
}
}
n := copy(buf, s.contents[s.offset:])
s.offset += n
return n, nil
}
func (*staticFile) Close() error {
return nil
}
func (s *staticFile) Name() string {
return s.name
}
func (s *staticFile) Size() int64 {
return int64(len(s.contents))
}
func (*staticFile) Mode() fs.FileMode {
return 0400
}
func (*staticFile) ModTime() time.Time {
return time.Now()
}
func (*staticFile) IsDir() bool {
return false
}
func (*staticFile) Sys() any {
return nil
}