-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmarshal.go
51 lines (48 loc) · 968 Bytes
/
marshal.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
package ipldgit
import (
"fmt"
"io"
"github.com/ipld/go-ipld-prime"
)
// Encode serializes a git node to a raw binary form.
func Encode(n ipld.Node, w io.Writer) error {
switch n.Prototype() {
case Type.Blob, Type.Blob__Repr:
return encodeBlob(n, w)
case Type.Commit, Type.Commit__Repr:
return encodeCommit(n, w)
case Type.Tree, Type.Tree__Repr:
return encodeTree(n, w)
case Type.Tag, Type.Tag__Repr:
return encodeTag(n, w)
default:
}
switch n.Kind() {
case ipld.Kind_Bytes:
return encodeBlob(n, w)
case ipld.Kind_List:
return encodeTree(n, w)
case ipld.Kind_Map:
k, _, err := n.MapIterator().Next()
if err != nil {
return err
}
repKey, err := k.AsString()
if err != nil {
return err
}
switch repKey {
case
"object",
"type",
"tag",
"tagger",
"text":
return encodeTag(n, w)
default:
return encodeCommit(n, w)
}
default:
return fmt.Errorf("unrecognized object type: %T", n.Prototype())
}
}