-
Notifications
You must be signed in to change notification settings - Fork 19
/
unmarshal.go
75 lines (66 loc) · 1.45 KB
/
unmarshal.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
package ipldgit
import (
"bufio"
"fmt"
"io"
"github.com/ipld/go-ipld-prime"
)
// Decode reads from a reader to fill a NodeAssembler
func Decode(na ipld.NodeAssembler, r io.Reader) error {
rd := bufio.NewReader(r)
typ, err := rd.ReadString(' ')
if err == io.EOF {
return io.ErrUnexpectedEOF
}
if err != nil {
return err
}
typ = typ[:len(typ)-1]
switch typ {
case "tree":
return DecodeTree(na, rd)
case "commit":
return DecodeCommit(na, rd)
case "blob":
return DecodeBlob(na, rd)
case "tag":
return DecodeTag(na, rd)
default:
return fmt.Errorf("unrecognized object type: %q", typ)
}
}
// ParseObject produces an ipld.Node from a stream / binary represnetation.
func ParseObject(r io.Reader) (ipld.Node, error) {
rd := bufio.NewReader(r)
typ, err := rd.ReadString(' ')
if err == io.EOF {
return nil, io.ErrUnexpectedEOF
}
if err != nil {
return nil, err
}
typ = typ[:len(typ)-1]
var na ipld.NodeBuilder
var decode func(ipld.NodeAssembler, *bufio.Reader) error
switch typ {
case "tree":
na = Type.Tree.NewBuilder()
decode = DecodeTree
case "commit":
na = Type.Commit.NewBuilder()
decode = DecodeCommit
case "blob":
na = Type.Blob.NewBuilder()
decode = DecodeBlob
case "tag":
na = Type.Tag.NewBuilder()
decode = DecodeTag
default:
return nil, fmt.Errorf("unrecognized object type: %q", typ)
}
// fmt.Printf("type %s\n", typ)
if err := decode(na, rd); err != nil {
return nil, err
}
return na.Build(), nil
}