-
Notifications
You must be signed in to change notification settings - Fork 14
/
marshalHelpers.go
61 lines (53 loc) · 1.52 KB
/
marshalHelpers.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
package refmt
import (
"io"
"github.com/polydawn/refmt/cbor"
"github.com/polydawn/refmt/json"
"github.com/polydawn/refmt/obj/atlas"
)
type EncodeOptions interface {
IsEncodeOptions() // marker method.
}
func Marshal(opts EncodeOptions, v interface{}) ([]byte, error) {
switch o2 := opts.(type) {
case json.EncodeOptions:
return json.MarshalAtlased(o2, v, atlas.MustBuild())
case cbor.EncodeOptions:
return cbor.MarshalAtlased(v, atlas.MustBuild())
default:
panic("incorrect usage: unknown EncodeOptions type")
}
}
func MarshalAtlased(opts EncodeOptions, v interface{}, atl atlas.Atlas) ([]byte, error) {
switch o2 := opts.(type) {
case json.EncodeOptions:
return json.MarshalAtlased(o2, v, atl)
case cbor.EncodeOptions:
return cbor.MarshalAtlased(v, atl)
default:
panic("incorrect usage: unknown EncodeOptions type")
}
}
type Marshaller interface {
Marshal(v interface{}) error
}
func NewMarshaller(opts EncodeOptions, wr io.Writer) Marshaller {
switch o2 := opts.(type) {
case json.EncodeOptions:
return json.NewMarshallerAtlased(wr, o2, atlas.MustBuild())
case cbor.EncodeOptions:
return cbor.NewMarshaller(wr)
default:
panic("incorrect usage: unknown EncodeOptions type")
}
}
func NewMarshallerAtlased(opts EncodeOptions, wr io.Writer, atl atlas.Atlas) Marshaller {
switch o2 := opts.(type) {
case json.EncodeOptions:
return json.NewMarshallerAtlased(wr, o2, atl)
case cbor.EncodeOptions:
return cbor.NewMarshallerAtlased(wr, atl)
default:
panic("incorrect usage: unknown EncodeOptions type")
}
}