MCPcopy Index your code
hub / github.com/segmentio/encoding / constructMapCodec

Function constructMapCodec

json/codec.go:351–451  ·  view source on GitHub ↗
(t reflect.Type, seen map[reflect.Type]*structType)

Source from the content-addressed store, hash-verified

349}
350
351func constructMapCodec(t reflect.Type, seen map[reflect.Type]*structType) codec {
352 var sortKeys sortFunc
353 k := t.Key()
354 v := t.Elem()
355
356 // Faster implementations for some common cases.
357 switch {
358 case k == stringType && v == interfaceType:
359 return codec{
360 encode: encoder.encodeMapStringInterface,
361 decode: decoder.decodeMapStringInterface,
362 }
363
364 case k == stringType && v == rawMessageType:
365 return codec{
366 encode: encoder.encodeMapStringRawMessage,
367 decode: decoder.decodeMapStringRawMessage,
368 }
369
370 case k == stringType && v == stringType:
371 return codec{
372 encode: encoder.encodeMapStringString,
373 decode: decoder.decodeMapStringString,
374 }
375
376 case k == stringType && v == stringsType:
377 return codec{
378 encode: encoder.encodeMapStringStringSlice,
379 decode: decoder.decodeMapStringStringSlice,
380 }
381
382 case k == stringType && v == boolType:
383 return codec{
384 encode: encoder.encodeMapStringBool,
385 decode: decoder.decodeMapStringBool,
386 }
387 }
388
389 kc := codec{}
390 vc := constructCodec(v, seen, false)
391
392 if k.Implements(textMarshalerType) || reflect.PointerTo(k).Implements(textUnmarshalerType) {
393 kc.encode = constructTextMarshalerEncodeFunc(k, false)
394 kc.decode = constructTextUnmarshalerDecodeFunc(k, true)
395
396 sortKeys = func(keys []reflect.Value) {
397 sort.Slice(keys, func(i, j int) bool {
398 // This is a performance abomination but the use case is rare
399 // enough that it shouldn't be a problem in practice.
400 k1, _ := keys[i].Interface().(encoding.TextMarshaler).MarshalText()
401 k2, _ := keys[j].Interface().(encoding.TextMarshaler).MarshalText()
402 return string(k1) < string(k2)
403 })
404 }
405 } else {
406 switch k.Kind() {
407 case reflect.String:
408 kc.encode = encoder.encodeString

Callers 1

constructCodecFunction · 0.85

Calls 15

constructCodecFunction · 0.85
constructStringCodecFunction · 0.85
intStringsAreSortedFunction · 0.85
uintStringsAreSortedFunction · 0.85
constructMapEncodeFuncFunction · 0.85
constructMapDecodeFuncFunction · 0.85
inlinedFunction · 0.70
KeyMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…