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

Function structEncodeFuncOf

proto/struct.go:214–286  ·  view source on GitHub ↗
(t reflect.Type, fields []structField)

Source from the content-addressed store, hash-verified

212}
213
214func structEncodeFuncOf(t reflect.Type, fields []structField) encodeFunc {
215 inlined := inlined(t)
216 var unique, repeated []*structField
217
218 for i := range fields {
219 f := &fields[i]
220 if f.repeated() {
221 repeated = append(repeated, f)
222 } else {
223 unique = append(unique, f)
224 }
225 }
226
227 return func(b []byte, p unsafe.Pointer, flags flags) (int, error) {
228 if p == nil {
229 return 0, nil
230 }
231
232 if !inlined {
233 flags = flags.without(inline | toplevel)
234 } else {
235 flags = flags.without(toplevel)
236 }
237 offset := 0
238
239 for _, f := range unique {
240 fieldFlags := f.makeFlags(flags)
241 elem := f.pointer(p)
242 size := f.codec.size(elem, fieldFlags)
243
244 if size > 0 {
245 n, err := encodeTag(b[offset:], f.fieldNumber(), f.wireType())
246 offset += n
247 if err != nil {
248 return offset, err
249 }
250
251 if f.embedded() {
252 n, err := encodeVarint(b[offset:], uint64(size))
253 offset += n
254 if err != nil {
255 return offset, err
256 }
257 }
258
259 if (len(b) - offset) < size {
260 return len(b), io.ErrShortBuffer
261 }
262
263 n, err = f.codec.encode(b[offset:offset+size], elem, fieldFlags)
264 offset += n
265 if err != nil {
266 return offset, err
267 }
268
269 flags = flags.without(wantzero)
270 }
271 }

Callers 1

structCodecOfFunction · 0.85

Calls 11

encodeTagFunction · 0.85
encodeVarintFunction · 0.85
repeatedMethod · 0.80
makeFlagsMethod · 0.80
pointerMethod · 0.80
fieldNumberMethod · 0.80
wireTypeMethod · 0.80
embeddedMethod · 0.80
encodeMethod · 0.80
inlinedFunction · 0.70
withoutMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…