-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
44 lines (36 loc) · 829 Bytes
/
parse.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
package repr
import (
"bytes"
"strconv"
)
func ParseObject(compressed []byte) (Object, error) {
b, err := uncompress(compressed)
if err != nil {
return nil, err
}
parts := bytes.SplitN(b, []byte{0}, 2)
if len(parts) != 2 {
return nil, ErrorCorruptedObject
}
headerBytes, content := parts[0], parts[1]
headerParts := bytes.SplitN(headerBytes, []byte{' '}, 2)
if len(headerParts) != 2 {
return nil, ErrorCorruptedObjectHeader
}
objType, sizeStr := string(headerParts[0]), string(headerParts[1])
size, err := strconv.Atoi(sizeStr)
if err != nil {
return nil, err
}
if size != len(content) {
return nil, ErrorSizeNotMatch
}
r := bytes.NewReader(content)
switch objType {
case "blob":
return NewBlob(r)
case "tree":
return NewTree(r)
}
return nil, formatErrorUnknownObjectType(objType)
}