MCPcopy Create free account
hub / github.com/segmentio/encoding / encodeFuncMapOf

Function encodeFuncMapOf

thrift/encode.go:167–208  ·  view source on GitHub ↗
(t reflect.Type, seen encodeFuncCache)

Source from the content-addressed store, hash-verified

165}
166
167func encodeFuncMapOf(t reflect.Type, seen encodeFuncCache) encodeFunc {
168 key, elem := t.Key(), t.Elem()
169 if elem.Size() == 0 { // map[?]struct{}
170 return encodeFuncMapAsSetOf(t, seen)
171 }
172
173 keyType := TypeOf(key)
174 elemType := TypeOf(elem)
175 encodeKey := encodeFuncOf(key, seen)
176 encodeElem := encodeFuncOf(elem, seen)
177
178 return func(w Writer, v reflect.Value, flags flags) error {
179 n := v.Len()
180 if n > math.MaxInt32 {
181 return fmt.Errorf("map length is too large to be represented in thrift: %d > max(int32)", n)
182 }
183
184 err := w.WriteMap(Map{
185 Size: int32(n),
186 Key: keyType,
187 Value: elemType,
188 })
189 if err != nil {
190 return err
191 }
192 if n == 0 { // empty map
193 return nil
194 }
195
196 iter := v.MapRange()
197 for iter.Next() {
198 if err := encodeKey(w, iter.Key(), flags); err != nil {
199 return err
200 }
201 if err := encodeElem(w, iter.Value(), flags); err != nil {
202 return err
203 }
204 }
205
206 return nil
207 }
208}
209
210func encodeFuncMapAsSetOf(t reflect.Type, seen encodeFuncCache) encodeFunc {
211 key := t.Key()

Callers 1

encodeFuncOfFunction · 0.85

Calls 10

encodeFuncMapAsSetOfFunction · 0.85
encodeFuncOfFunction · 0.85
TypeOfFunction · 0.70
KeyMethod · 0.65
ElemMethod · 0.65
SizeMethod · 0.65
WriteMapMethod · 0.65
LenMethod · 0.45
NextMethod · 0.45
ValueMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…