-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob.go
45 lines (40 loc) · 1.03 KB
/
blob.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
package vesper
// Blob - create a new blob, using the specified byte slice as the data. The data is not copied.
func Blob(bytes []byte) *Object {
return &Object{
Type: BlobType,
Value: bytes,
}
}
// MakeBlob - create a new blob of the given size. It will be initialized to all zeroes
func MakeBlob(size int) *Object {
return Blob(make([]byte, size))
}
// EmptyBlob - a blob with no bytes
var EmptyBlob = MakeBlob(0)
// ToBlob - convert argument to a blob, if possible.
func ToBlob(obj *Object) (*Object, error) {
switch obj.Type {
case BlobType:
return obj, nil
case StringType:
return Blob([]byte(obj.text)), nil // this copies the data
case ArrayType:
return arrayToBlob(obj)
default:
return nil, Error(ArgumentErrorKey, "to-blob expected <blob> or <string>, got a ", obj.Type)
}
}
func arrayToBlob(obj *Object) (*Object, error) {
el := obj.elements
n := len(el)
b := make([]byte, n)
for i := 0; i < n; i++ {
val, err := AsByteValue(el[i])
if err != nil {
return nil, err
}
b[i] = val
}
return Blob(b), nil
}