-
Notifications
You must be signed in to change notification settings - Fork 62
/
dagtransl.go
86 lines (67 loc) · 2.23 KB
/
dagtransl.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
76
77
78
79
80
81
82
83
84
85
86
package coredag
import (
"fmt"
"io"
ipld "github.com/ipfs/go-ipld-format"
)
// DagParser is function used for parsing stream into Node
type DagParser func(r io.Reader, mhType uint64, mhLen int) ([]ipld.Node, error)
// FormatParsers is used for mapping format descriptors to DagParsers
type FormatParsers map[string]DagParser
// InputEncParsers is used for mapping input encodings to FormatParsers
type InputEncParsers map[string]FormatParsers
// DefaultInputEncParsers is InputEncParser that is used everywhere
var DefaultInputEncParsers = InputEncParsers{
"json": defaultJSONParsers,
"raw": defaultRawParsers,
"cbor": defaultCborParsers,
"protobuf": defaultProtobufParsers,
}
var defaultJSONParsers = FormatParsers{
"cbor": cborJSONParser,
"dag-cbor": cborJSONParser,
"protobuf": dagpbJSONParser,
"dag-pb": dagpbJSONParser,
}
var defaultRawParsers = FormatParsers{
"cbor": cborRawParser,
"dag-cbor": cborRawParser,
"protobuf": dagpbRawParser,
"dag-pb": dagpbRawParser,
"raw": rawRawParser,
}
var defaultCborParsers = FormatParsers{
"cbor": cborRawParser,
"dag-cbor": cborRawParser,
}
var defaultProtobufParsers = FormatParsers{
"protobuf": dagpbRawParser,
"dag-pb": dagpbRawParser,
}
// ParseInputs uses DefaultInputEncParsers to parse io.Reader described by
// input encoding and format to an instance of ipld Node
func ParseInputs(ienc, format string, r io.Reader, mhType uint64, mhLen int) ([]ipld.Node, error) {
return DefaultInputEncParsers.ParseInputs(ienc, format, r, mhType, mhLen)
}
// AddParser adds DagParser under give input encoding and format
func (iep InputEncParsers) AddParser(ienc, format string, f DagParser) {
m, ok := iep[ienc]
if !ok {
m = make(FormatParsers)
iep[ienc] = m
}
m[format] = f
}
// ParseInputs parses io.Reader described by input encoding and format to
// an instance of ipld Node
func (iep InputEncParsers) ParseInputs(ienc, format string, r io.Reader, mhType uint64, mhLen int) ([]ipld.Node, error) {
parsers, ok := iep[ienc]
if !ok {
return nil, fmt.Errorf("no input parser for %q", ienc)
}
parser, ok := parsers[format]
if !ok {
return nil, fmt.Errorf("no parser for format %q using input type %q", format, ienc)
}
return parser(r, mhType, mhLen)
}