forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentryContent.go
71 lines (60 loc) · 1.77 KB
/
entryContent.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
package plugin
import (
"context"
"io"
)
// entryContent is the cached result of a Read invocation
type entryContent interface {
read(context.Context, int64, int64) ([]byte, error)
size() uint64
}
// entryContentImpl is the default implementation of entryContent,
// meant for Readable entries
type entryContentImpl struct {
content []byte
}
func newEntryContent(content []byte) *entryContentImpl {
return &entryContentImpl{
content: content,
}
}
func (c *entryContentImpl) read(_ context.Context, size int64, offset int64) (data []byte, err error) {
data = []byte{}
contentSize := int64(len(c.content))
if offset >= contentSize {
err = io.EOF
return
}
endIx := offset + size
if contentSize < endIx {
endIx = contentSize
err = io.EOF
}
data = c.content[offset:endIx]
return
}
func (c *entryContentImpl) size() uint64 {
return uint64(len(c.content))
}
type blockReadFunc = func(context.Context, int64, int64) ([]byte, error)
// blockReadableEntryContent is the implementation of entryContent that's
// meant for BlockReadable entries. For now, it doesn't cache any content.
type blockReadableEntryContent struct {
readFunc blockReadFunc
sz uint64
}
func newBlockReadableEntryContent(readFunc blockReadFunc) *blockReadableEntryContent {
return &blockReadableEntryContent{
readFunc: readFunc,
}
}
// Note that we don't need to check the offset/size because if the size attribute
// was set, then plugin.Read already did that validation. If the size attribute
// wasn't set, then it is the responsibility of the plugin's API to raise the error
// for us.
func (c *blockReadableEntryContent) read(ctx context.Context, size int64, offset int64) ([]byte, error) {
return c.readFunc(ctx, size, offset)
}
func (c *blockReadableEntryContent) size() uint64 {
return c.sz
}