MCPcopy Create free account
hub / github.com/brimdata/super / encodeArray

Method encodeArray

sup/marshal.go:464–502  ·  view source on GitHub ↗
(arrayVal reflect.Value)

Source from the content-addressed store, hash-verified

462}
463
464func (m *MarshalBSUPContext) encodeArray(arrayVal reflect.Value) (super.Type, error) {
465 m.Builder.BeginContainer()
466 arrayLen := arrayVal.Len()
467 types := make([]super.Type, 0, arrayLen)
468 for i := range arrayLen {
469 item := arrayVal.Index(i)
470 typ, err := m.encodeValue(item)
471 if err != nil {
472 return nil, err
473 }
474 types = append(types, typ)
475 }
476 uniqueTypes := super.UniqueTypes(slices.Clone(types))
477 var innerType super.Type
478 switch len(uniqueTypes) {
479 case 0:
480 // if slice was empty, look up the type without a value
481 var err error
482 innerType, err = m.lookupType(arrayVal.Type().Elem())
483 if err != nil {
484 return nil, err
485 }
486 case 1:
487 innerType = types[0]
488 default:
489 unionType := m.Context.LookupTypeUnion(uniqueTypes)
490 // Convert each container element to the union type.
491 m.Builder.TransformContainer(func(bytes scode.Bytes) scode.Bytes {
492 var b scode.Builder
493 for i, it := 0, bytes.Iter(); !it.Done(); i++ {
494 super.BuildUnion(&b, unionType.TagOf(types[i]), it.Next())
495 }
496 return b.Bytes()
497 })
498 innerType = unionType
499 }
500 m.Builder.EndContainer()
501 return m.Context.LookupTypeArray(innerType), nil
502}
503
504func (m *MarshalBSUPContext) lookupType(t reflect.Type) (super.Type, error) {
505 var typ super.Type

Callers 1

encodeAnyMethod · 0.95

Calls 15

encodeValueMethod · 0.95
lookupTypeMethod · 0.95
BytesMethod · 0.95
UniqueTypesFunction · 0.92
BuildUnionFunction · 0.92
BeginContainerMethod · 0.80
IndexMethod · 0.80
LookupTypeUnionMethod · 0.80
TransformContainerMethod · 0.80
IterMethod · 0.80
TagOfMethod · 0.80
EndContainerMethod · 0.80

Tested by

no test coverage detected