-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathformat.go
60 lines (48 loc) · 1.63 KB
/
format.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
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package wrp
import (
"io"
)
//go:generate go install golang.org/x/tools/cmd/stringer@latest
//go:generate stringer -type=Format
// Format indicates which format is desired.
// The zero value indicates Msgpack, which means by default other
// infrastructure can assume msgpack-formatted data.
type Format int
const (
Msgpack Format = iota
JSON
lastFormat
)
// AllFormats returns a distinct slice of all supported formats.
func AllFormats() []Format {
return []Format{Msgpack, JSON}
}
// Encoder returns an Encoder for the given format.
func (f Format) Encoder(output io.Writer) Encoder {
return NewEncoder(output, f)
}
// EncoderBytes returns an Encoder for the given format.
func (f Format) EncoderBytes(output *[]byte) Encoder {
return NewEncoderBytes(output, f)
}
// Decoder returns a Decoder for the given format.
func (f Format) Decoder(input io.Reader) Decoder {
return NewDecoder(input, f)
}
// DecoderBytes returns a Decoder for the given format.
func (f Format) DecoderBytes(input []byte) Decoder {
return NewDecoderBytes(input, f)
}
// TranscodeMessage converts a WRP message of any type from one format into another,
// e.g. from JSON into Msgpack. The intermediate, generic Message used to hold decoded
// values is returned in addition to any error. If a decode error occurs, this function
// will not perform the encoding step.
func TranscodeMessage(target Encoder, source Decoder) (msg *Message, err error) {
msg = new(Message)
if err = source.Decode(msg); err == nil {
err = target.Encode(msg)
}
return
}