6 Mapper is the main entry point in the package.
8 On init, you should call NewMapper() for each interface type you want
9 to support flexible de-serialization, and then
10 RegisterImplementation() in the init() function for each implementation of these
13 Note that unlike go-wire, you can call RegisterImplementation separately from
14 different locations with each implementation, not all in one place.
15 Just be careful not to use the same key or byte, of init will *panic*
22 // NewMapper creates a Mapper.
25 // type Foo interface {....}
26 // type FooS struct { Foo }
27 // then you should pass in FooS{} in NewMapper, and implementations of Foo
28 // in RegisterImplementation
29 func NewMapper(base interface{}) Mapper {
31 jsonMapper: newJsonMapper(base),
32 binaryMapper: newBinaryMapper(base),
36 // RegisterImplementation should be called once for each implementation of the
37 // interface that we wish to support.
39 // kind is the type string used in the json representation, while b is the
40 // type byte used in the go-wire representation. data is one instance of this
41 // concrete type, like Bar{}
42 func (m Mapper) RegisterImplementation(data interface{}, kind string, b byte) Mapper {
43 m.jsonMapper.registerImplementation(data, kind, b)
44 m.binaryMapper.registerImplementation(data, kind, b)
48 // ToText is a rather special-case serialization for cli, especially for []byte
51 // It tries to serialize as json, and the result looks like:
52 // { "type": "string", "data": "string" }
53 // Then it will return "<type>:<data>"
55 // Main usecase is serializing eg. crypto.PubKeyS as "ed25119:a1b2c3d4..."
56 // for displaying in cli tools.
58 // It also supports encoding data.Bytes to a string using the proper codec
59 // (or anything else that has a marshals to a string)
60 func ToText(o interface{}) (string, error) {
66 // try to recover as a string (data.Bytes case)
73 // if not, then try to recover as an interface (crypto.*S case)
75 err = FromJSON(d, &text)
79 res := fmt.Sprintf("%s:%s", text.Kind, text.Data)
83 // textEnv lets us parse an envelope if the data was output as string
84 // (either text, or a serialized []byte)
86 Kind string `json:"type"`
87 Data string `json:"data"`