forked from aws/aws-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
166 lines (147 loc) · 4.5 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package protocol
import (
"io"
"time"
)
// A FieldMarshaler interface is used to marshal struct fields when encoding.
type FieldMarshaler interface {
MarshalFields(FieldEncoder) error
}
// 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)
}
// A ListEncoder provides the interface for encoders that will encode List elements.
type ListEncoder interface {
ListAddValue(v ValueMarshaler)
ListAddList(fn func(ListEncoder))
ListAddMap(fn func(MapEncoder))
ListAddFields(m FieldMarshaler)
}
// A MapEncoder provides the interface for encoders that will encode map elements.
type MapEncoder interface {
MapSetValue(k string, v ValueMarshaler)
MapSetList(k string, fn func(ListEncoder))
MapSetMap(k string, fn func(MapEncoder))
MapSetFields(k string, m FieldMarshaler)
}
// A FieldEncoder provides the interface for encoding struct field members.
type FieldEncoder interface {
SetValue(t Target, k string, v ValueMarshaler, meta Metadata)
SetStream(t Target, k string, v StreamMarshaler, meta Metadata)
SetList(t Target, k string, fn func(ListEncoder), meta Metadata)
SetMap(t Target, k string, fn func(MapEncoder), meta Metadata)
SetFields(t Target, k string, m FieldMarshaler, meta Metadata)
}
// EncodeStringList returns a function that will add the slice's values to
// a list Encoder.
func EncodeStringList(vs []*string) func(ListEncoder) {
return func(le ListEncoder) {
for _, v := range vs {
le.ListAddValue(StringValue(*v))
}
}
}
// EncodeStringMap returns a function that will add the map's values to
// a map Encoder.
func EncodeStringMap(vs map[string]*string) func(MapEncoder) {
return func(me MapEncoder) {
for k, v := range vs {
me.MapSetValue(k, StringValue(*v))
}
}
}
// EncodeInt64List returns a function that will add the slice's values to
// a list Encoder.
func EncodeInt64List(vs []*int64) func(ListEncoder) {
return func(le ListEncoder) {
for _, v := range vs {
le.ListAddValue(Int64Value(*v))
}
}
}
// EncodeInt64Map returns a function that will add the map's values to
// a map Encoder.
func EncodeInt64Map(vs map[string]*int64) func(MapEncoder) {
return func(me MapEncoder) {
for k, v := range vs {
me.MapSetValue(k, Int64Value(*v))
}
}
}
// EncodeFloat64List returns a function that will add the slice's values to
// a list Encoder.
func EncodeFloat64List(vs []*float64) func(ListEncoder) {
return func(le ListEncoder) {
for _, v := range vs {
le.ListAddValue(Float64Value(*v))
}
}
}
// EncodeFloat64Map returns a function that will add the map's values to
// a map Encoder.
func EncodeFloat64Map(vs map[string]*float64) func(MapEncoder) {
return func(me MapEncoder) {
for k, v := range vs {
me.MapSetValue(k, Float64Value(*v))
}
}
}
// EncodeBoolList returns a function that will add the slice's values to
// a list Encoder.
func EncodeBoolList(vs []*bool) func(ListEncoder) {
return func(le ListEncoder) {
for _, v := range vs {
le.ListAddValue(BoolValue(*v))
}
}
}
// EncodeBoolMap returns a function that will add the map's values to
// a map Encoder.
func EncodeBoolMap(vs map[string]*bool) func(MapEncoder) {
return func(me MapEncoder) {
for k, v := range vs {
me.MapSetValue(k, BoolValue(*v))
}
}
}
// EncodeTimeList returns a function that will add the slice's values to
// a list Encoder.
func EncodeTimeList(vs []*time.Time) func(ListEncoder) {
return func(le ListEncoder) {
for _, v := range vs {
le.ListAddValue(TimeValue{V: *v})
}
}
}
// EncodeTimeMap returns a function that will add the map's values to
// a map Encoder.
func EncodeTimeMap(vs map[string]*time.Time) func(MapEncoder) {
return func(me MapEncoder) {
for k, v := range vs {
me.MapSetValue(k, TimeValue{V: *v})
}
}
}
// 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)
b.buf = v
b.buf = b.buf[0:0]
return v, err
}