(arrayVal reflect.Value)
| 462 | } |
| 463 | |
| 464 | func (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 | |
| 504 | func (m *MarshalBSUPContext) lookupType(t reflect.Type) (super.Type, error) { |
| 505 | var typ super.Type |
no test coverage detected