forked from bazil/bazil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makechunk.go
50 lines (42 loc) · 928 Bytes
/
makechunk.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
package chunkutil
import (
"bazil.org/bazil/cas"
"bazil.org/bazil/cas/chunks"
)
// MakeChunk makes a new Chunk of the given description, filling it
// with content from data.
func MakeChunk(typ string, level uint8, data []byte) *chunks.Chunk {
chunk := &chunks.Chunk{
Type: typ,
Level: level,
Buf: data,
}
return chunk
}
type Handler func(key cas.Key, typ string, level uint8) ([]byte, error)
func HandleGet(fn Handler, key cas.Key, typ string, level uint8) (*chunks.Chunk, error) {
if key.IsSpecial() {
if key == cas.Empty {
chunk := MakeChunk(typ, level, nil)
return chunk, nil
}
return nil, cas.NotFound{
Type: typ,
Level: level,
Key: key,
}
}
data, err := fn(key, typ, level)
if err != nil {
return nil, err
}
if data == nil {
return nil, cas.NotFound{
Type: typ,
Level: level,
Key: key,
}
}
chunk := MakeChunk(typ, level, data)
return chunk, nil
}