-
Notifications
You must be signed in to change notification settings - Fork 636
/
encode.go
95 lines (77 loc) · 2.6 KB
/
encode.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
87
88
89
90
91
92
93
94
95
package protocol
import (
"io"
)
// A FieldMarshaler interface is used to marshal struct fields when encoding.
type FieldMarshaler interface {
MarshalFields(FieldEncoder) error
}
// FieldMarshalerFunc is a helper utility that wrapps a function and allows
// that function to be called as a FieldMarshaler.
type FieldMarshalerFunc func(FieldEncoder) error
// MarshalFields will call the underlying function passing in the field encoder
// with the protocol field encoder.
func (fn FieldMarshalerFunc) MarshalFields(e FieldEncoder) error {
return fn(e)
}
// ValueMarshaler provides a generic type for all encoding field values to be
// passed into a encoder's methods with.
type ValueMarshaler interface {
MarshalValue() (string, error)
MarshalValueBuf([]byte) ([]byte, error)
}
// A StreamMarshaler interface is used to marshal a stream when encoding.
type StreamMarshaler interface {
MarshalStream() (io.ReadSeeker, error)
}
// MarshalListValues is a marshaler for list encoders.
type MarshalListValues interface {
MarshalValues(enc ListEncoder) error
}
// MapMarshaler is a marshaler for map encoders.
type MapMarshaler interface {
MarshalValues(enc MapEncoder) error
}
// A ListEncoder provides the interface for encoders that will encode List elements.
type ListEncoder interface {
Start()
End()
Map() MapEncoder
List() ListEncoder
ListAddValue(v ValueMarshaler)
ListAddFields(m FieldMarshaler)
}
// A MapEncoder provides the interface for encoders that will encode map elements.
type MapEncoder interface {
Start()
End()
Map(k string) MapEncoder
List(k string) ListEncoder
MapSetValue(k string, v ValueMarshaler)
MapSetFields(k string, m FieldMarshaler)
}
// A FieldEncoder provides the interface for encoding struct field members.
type FieldEncoder interface {
SetValue(t Target, k string, m ValueMarshaler, meta Metadata)
SetStream(t Target, k string, m StreamMarshaler, meta Metadata)
SetFields(t Target, k string, m FieldMarshaler, meta Metadata)
Map(t Target, k string, meta Metadata) MapEncoder
List(t Target, k string, meta Metadata) ListEncoder
}
// A FieldBuffer provides buffering of fields so the number of
// allocations are reduced by providng a persistent buffer that is
// used between fields.
type FieldBuffer struct {
buf []byte
}
// GetValue will retrieve the ValueMarshaler's value by appending the
// value to the buffer. Will return the buffer that was populated.
//
// This buffer is only valid until the next time GetValue is called.
func (b *FieldBuffer) GetValue(m ValueMarshaler) ([]byte, error) {
v, err := m.MarshalValueBuf(b.buf)
if len(v) > len(b.buf) {
b.buf = v
}
return v, err
}